lib/App/ansicolumn/Document.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 10 / 10 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 10 totaltotal 83.8% 78 / 93 CC 9 SCAR 7.6
CC
9
Cov
97%
new
5.0
width
2.0
span
2.0
CRAP 2.1
« lib/App/ansicolumn/Util.pm ↑ Summary lib/App/ansicolumn/default.pm »
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 253 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 253 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 253 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 253 ▶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 # With "keep_ls", a LINE SEPARATOR (U+2028) does not end a line: the
30 # table mode takes it as a line break in a cell.
31 #
32 276 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
33 276 my $class = shift;
34 276 my %arg = @_;
35 276 ▶ my $parts = $arg{parts} // do {
Condition66%2/3
$arg{"parts"} // do { my $text = $arg{"text"} // ""; my $eol = $arg{"keep_ls"} ? qr/(?![\f\x{2028}])\R/u : qr/(?!\f)\R/u; [map({[split(/$eol/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)] }
l!l&&r!l&&!r
202560
MC/DC0%0/2
$arg{"parts"} // do { my $text = $arg{"text"} // ""; my $eol = $arg{"keep_ls"} ? qr/(?![\f\x{2028}])\R/u : qr/(?!\f)\R/u; [map({[split(/$eol/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)] }
$arg{"parts"} do { my $text = $arg{"text"} // ""; my $eol = $arg{"keep_ls"} ? qr/(?![\f\x{2028}])\R/u : qr/(?!\f)\R/u; [map({[split(/$eol/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)] }
Truth table66%2/3
A // B
A= $arg{"parts"}B= do { my $text = $arg{"text"} // ""; my $eol = $arg{"keep_ls"} ? qr/(?![\f\x{2028}])\R/u : qr/(?!\f)\R/u; [map({[split(/$eol/u, $_, 0)];} $arg{"formfeed"} ? _split_formfeed($text) : $text)] }
ABresult
1X1
011
000
36 256 ▶ my $text = $arg{text} // '';
Condition50%1/2
$arg{"text"} // ""
l!l
2560
MC/DC0%0/1
$arg{"text"} // ""
$arg{"text"}
Truth table50%1/2
A
A= $arg{"text"}
Aresult
11
00
37 256 ▶ my $eol = $arg{keep_ls} ? qr/(?![\f\x{2028}])\R/ : qr/(?!\f)\R/;
Branch: 1 branch
BranchTrueFalseTotal
$arg{'keep_ls'} ? : 138 118 256
38 296 ▶ [ map { [ split $eol ] }
Branch: 1 branch
BranchTrueFalseTotal
$arg{'formfeed'} ? : 97 159 256
39 256 $arg{formfeed} ? _split_formfeed($text) : $text ];
40 };
41 bless {
42 name => $arg{name},
43 276 parts => $parts,
44 width => 0,
45 span => undef,
46 }, $class;
47 }
48  
49 152 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
50  
51 # the width of the longest line, which the reader sets
52 396 ▶sub width {
Subroutine width: called CC=2 SCAR=6.9
Pod undocumented
53 396 my $obj = shift;
54 396 ▶ @_ ? ($obj->{width} = shift) : $obj->{width};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 267 129 396
55 }
56  
57 # the width of the pane, when it has one of its own
58 128 ▶sub span {
Subroutine span: called CC=2 SCAR=6.9
Pod undocumented
59 128 my $obj = shift;
60 128 ▶ @_ ? ($obj->{span} = shift) : $obj->{span};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 126 128
61 }
62  
63 # the parts, each a reference to the list of its lines
64 814 ▶sub parts { @{$_[0]->{parts}} }
Subroutine parts: called CC=1 SCAR=0.0
Pod undocumented
65  
66 # all the lines, as a copy
67 402 ▶sub lines { map { @$_ } $_[0]->parts }
Subroutine lines: called CC=1 SCAR=0.0
Pod undocumented
68  
69 95 ▶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
70  
71 # replace the lines of every part with what the code gives for them
72 224 ▶sub filter {
Subroutine filter: called CC=2 SCAR=6.9
Pod undocumented
73 224 my $obj = shift;
74 224 my $code = shift;
75 224 @$_ = $code->(@$_) for $obj->parts;
76 224 $obj;
77 }
78  
79 # replace every line with what the code gives for it, within its part
80 219 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod undocumented
81 219 my $obj = shift;
82 219 my $code = shift;
83 219 ▶ $obj->filter(sub { map { $code->($_) } @_ });
Subroutine __ANON__: called CC=1 SCAR=0.0
84 }
85  
86 # a document for each part
87 9 ▶sub pages {
Subroutine pages: called CC=1 SCAR=0.0
Pod undocumented
88 9 my $obj = shift;
89 9 map { ref($obj)->new(name => $obj->name, parts => [ $_ ]) } $obj->parts;
90 }
91  
92 #
93 # Split text into parts at formfeeds. A formfeed is the end of the
94 # part before it and the top of the next: what follows it, a newline
95 # as well, belongs to the next part. No part is made after the last
96 # formfeed when nothing follows it.
97 #
98 # The colors just before a formfeed, and a reset just after it, are
99 # for the formfeed and go with it: bat gives a formfeed alone as
100 # "\e[38;2;...m\f\e[0m". Colors are not carried over a formfeed, as
101 # they are not over a newline.
102 #
103 97 ▶sub _split_formfeed {
Subroutine _split_formfeed: called CC=2 SCAR=6.9
104 97 my $text = shift;
105 97 state $sgr = qr/\e\[[\d;:]*m/;
106 # Remove the colors just before a formfeed and a reset just after
107 # it. Without this, what bat gives leaves a line of a color alone
108 # at the end of the part ("\e[38;2;...m"), which is counted as a
109 # line, and a line of a reset alone at the top of the next part
110 # ("\e[0m"), which --no-white-space does not take as a blank line:
111 # every page of "nup bat" would begin a line lower than it should.
112 # A color just after a formfeed is for the text and is left.
113 97 $text =~ s/(?:$sgr)*\f(?:\e\[0*m)*/\f/g;
114 97 my @part = split /\f/, $text;
115 97 ▶ @part ? @part : '';
Branch: 1 branch
BranchTrueFalseTotal
@part ? : 96 1 97
116 }
117  
118 253 1;
« lib/App/ansicolumn/Util.pm ↑ Summary lib/App/ansicolumn/default.pm »