| 1 | package App::ansicolumn::Document; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =encoding utf-8 | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | App::ansicolumn::Document - The text of a file as parts of lines | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | A document is the text of a file, kept as a list of parts, each of | ||||||||||||||||||||
| 12 | them a list of lines. The parts are what form feeds divide: there is | ||||||||||||||||||||
| 13 | one part when the text is not divided. This is the only place which | ||||||||||||||||||||
| 14 | knows of a form feed as a break, and of the parts as a structure. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 157 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 157 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 157 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 157 | ▶ | use List::Util qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # | ||||||||||||||||||||
| 24 | # new(name => NAME, text => TEXT, formfeed => BOOL) | ||||||||||||||||||||
| 25 | # new(name => NAME, parts => [ [ LINE, ... ], ... ]) | ||||||||||||||||||||
| 26 | # | ||||||||||||||||||||
| 27 | # With "formfeed", the text is divided at form feeds. Without it the | ||||||||||||||||||||
| 28 | # text is one part, and a form feed is left in the lines as it is. | ||||||||||||||||||||
| 29 | # | ||||||||||||||||||||
| 30 | 180 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||
| 31 | 180 | my $class = shift; | |||||||||||||||||||
| 32 | 180 | my %arg = @_; | |||||||||||||||||||
| 33 | 180 | ▶ | my $parts = $arg{parts} // do { | ||||||||||||||||||
|
Condition66%2/3
$arg{"parts"} // do {
my $text = $arg{"text"} // "";
[map({[split(/(?!\f)\R/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)]
}
MC/DC0%0/2
$arg{"parts"} // do {
my $text = $arg{"text"} // "";
[map({[split(/(?!\f)\R/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)]
}
$arg{"parts"}
do {
my $text = $arg{"text"} // "";
[map({[split(/(?!\f)\R/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)]
}
Truth table66%2/3
A // B
A= $arg{"parts"}B= do {
my $text = $arg{"text"} // "";
[map({[split(/(?!\f)\R/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)]
}
| |||||||||||||||||||||
| 34 | 160 | ▶ | my $text = $arg{text} // ''; | ||||||||||||||||||
|
Condition50%1/2
$arg{"text"} // ""
MC/DC0%0/1
$arg{"text"} // ""
$arg{"text"}
Truth table50%1/2
A
A= $arg{"text"}
| |||||||||||||||||||||
| 35 | 200 | ▶ | [ map { [ split /(?!\f)\R/ ] } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 160 | $arg{formfeed} ? _split_formfeed($text) : $text ]; | |||||||||||||||||||
| 37 | }; | ||||||||||||||||||||
| 38 | bless { | ||||||||||||||||||||
| 39 | name => $arg{name}, | ||||||||||||||||||||
| 40 | 180 | parts => $parts, | |||||||||||||||||||
| 41 | width => 0, | ||||||||||||||||||||
| 42 | span => undef, | ||||||||||||||||||||
| 43 | }, $class; | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 129 | ▶ | sub name { $_[0]->{name} } | ||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # the width of the longest line, which the reader sets | ||||||||||||||||||||
| 49 | 279 | ▶ | sub width { | ||||||||||||||||||
|
Subroutine
width: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 50 | 279 | my $obj = shift; | |||||||||||||||||||
| 51 | 279 | ▶ | @_ ? ($obj->{width} = shift) : $obj->{width}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # the width of the pane, when it has one of its own | ||||||||||||||||||||
| 55 | 116 | ▶ | sub span { | ||||||||||||||||||
|
Subroutine
span: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 56 | 116 | my $obj = shift; | |||||||||||||||||||
| 57 | 116 | ▶ | @_ ? ($obj->{span} = shift) : $obj->{span}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | # the parts, each a reference to the list of its lines | ||||||||||||||||||||
| 61 | 567 | ▶ | sub parts { @{$_[0]->{parts}} } | ||||||||||||||||||
|
Subroutine
parts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # all the lines, as a copy | ||||||||||||||||||||
| 64 | 231 | ▶ | sub lines { map { @$_ } $_[0]->parts } | ||||||||||||||||||
|
Subroutine
lines: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 76 | ▶ | sub is_empty { not any { @$_ } $_[0]->parts } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # replace the lines of every part with what the code gives for them | ||||||||||||||||||||
| 69 | 186 | ▶ | sub filter { | ||||||||||||||||||
|
Subroutine
filter: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 70 | 186 | my $obj = shift; | |||||||||||||||||||
| 71 | 186 | my $code = shift; | |||||||||||||||||||
| 72 | 186 | @$_ = $code->(@$_) for $obj->parts; | |||||||||||||||||||
| 73 | 186 | $obj; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # replace every line with what the code gives for it, within its part | ||||||||||||||||||||
| 77 | 186 | ▶ | sub replace { | ||||||||||||||||||
|
Subroutine
replace: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 78 | 186 | my $obj = shift; | |||||||||||||||||||
| 79 | 186 | my $code = shift; | |||||||||||||||||||
| 80 | 186 | ▶ | $obj->filter(sub { map { $code->($_) } @_ }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | # a document for each part | ||||||||||||||||||||
| 84 | 9 | ▶ | sub pages { | ||||||||||||||||||
|
Subroutine
pages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 85 | 9 | my $obj = shift; | |||||||||||||||||||
| 86 | 9 | map { ref($obj)->new(name => $obj->name, parts => [ $_ ]) } $obj->parts; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | # | ||||||||||||||||||||
| 90 | # Split text into parts at formfeeds. A formfeed is the end of the | ||||||||||||||||||||
| 91 | # part before it and the top of the next: what follows it, a newline | ||||||||||||||||||||
| 92 | # as well, belongs to the next part. No part is made after the last | ||||||||||||||||||||
| 93 | # formfeed when nothing follows it. | ||||||||||||||||||||
| 94 | # | ||||||||||||||||||||
| 95 | # The colors just before a formfeed, and a reset just after it, are | ||||||||||||||||||||
| 96 | # for the formfeed and go with it: bat gives a formfeed alone as | ||||||||||||||||||||
| 97 | # "\e[38;2;...m\f\e[0m". Colors are not carried over a formfeed, as | ||||||||||||||||||||
| 98 | # they are not over a newline. | ||||||||||||||||||||
| 99 | # | ||||||||||||||||||||
| 100 | 78 | ▶ | sub _split_formfeed { | ||||||||||||||||||
|
Subroutine
_split_formfeed: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 101 | 78 | my $text = shift; | |||||||||||||||||||
| 102 | 78 | state $sgr = qr/\e\[[\d;:]*m/; | |||||||||||||||||||
| 103 | # Remove the colors just before a formfeed and a reset just after | ||||||||||||||||||||
| 104 | # it. Without this, what bat gives leaves a line of a color alone | ||||||||||||||||||||
| 105 | # at the end of the part ("\e[38;2;...m"), which is counted as a | ||||||||||||||||||||
| 106 | # line, and a line of a reset alone at the top of the next part | ||||||||||||||||||||
| 107 | # ("\e[0m"), which --no-white-space does not take as a blank line: | ||||||||||||||||||||
| 108 | # every page of "nup bat" would begin a line lower than it should. | ||||||||||||||||||||
| 109 | # A color just after a formfeed is for the text and is left. | ||||||||||||||||||||
| 110 | 78 | $text =~ s/(?:$sgr)*\f(?:\e\[0*m)*/\f/g; | |||||||||||||||||||
| 111 | 78 | my @part = split /\f/, $text; | |||||||||||||||||||
| 112 | 78 | ▶ | @part ? @part : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 157 | 1; | |||||||||||||||||||