lib/App/ansicolumn/Document.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 8 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 10 totaltotal 83.3% 75 / 90 CC 8 SCAR 6.1
CC
8
Cov
97%
new
4.0
width
2.0
span
2.0
CRAP 1.8
« lib/App/ansicolumn/Border.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 141 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 141 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 141 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 141 ▶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 163 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod undocumented
31 163 my $class = shift;
32 163 my %arg = @_;
33 163 ▶ 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)] }
l!l&&r!l&&!r
201430
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)] }
ABresult
1X1
011
000
34 143 ▶ my $text = $arg{text} // '';
Condition50%1/2
$arg{"text"} // ""
l!l
1430
MC/DC0%0/1
$arg{"text"} // ""
$arg{"text"}
Truth table50%1/2
A
A= $arg{"text"}
Aresult
11
00
35 183 ▶ [ map { [ split /(?!\f)\R/ ] }
Branch: 1 branch
BranchTrueFalseTotal
$arg{'formfeed'} ? : 64 79 143
36 143 $arg{formfeed} ? _split_formfeed($text) : $text ];
37 };
38 bless {
39 name => $arg{name},
40 163 parts => $parts,
41 width => 0,
42 span => undef,
43 }, $class;
44 }
45  
46 112 ▶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 245 ▶sub width {
Subroutine width: called CC=2 SCAR=6.9
Pod undocumented
50 245 my $obj = shift;
51 245 ▶ @_ ? ($obj->{width} = shift) : $obj->{width};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 154 91 245
52 }
53  
54 # the width of the pane, when it has one of its own
55 96 ▶sub span {
Subroutine span: called CC=2 SCAR=6.9
Pod undocumented
56 96 my $obj = shift;
57 96 ▶ @_ ? ($obj->{span} = shift) : $obj->{span};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 94 96
58 }
59  
60 # the parts, each a reference to the list of its lines
61 488 ▶sub parts { @{$_[0]->{parts}} }
Subroutine parts: called CC=1 SCAR=0.0
Pod undocumented
62  
63 # all the lines, as a copy
64 214 ▶sub lines { map { @$_ } $_[0]->parts }
Subroutine lines: called CC=1 SCAR=0.0
Pod undocumented
65  
66 62 ▶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 152 ▶sub filter {
Subroutine filter: called CC=2 SCAR=6.9
Pod undocumented
70 152 my $obj = shift;
71 152 my $code = shift;
72 152 @$_ = $code->(@$_) for $obj->parts;
73 152 $obj;
74 }
75  
76 # replace every line with what the code gives for it, within its part
77 152 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod undocumented
78 152 my $obj = shift;
79 152 my $code = shift;
80 152 ▶ $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 64 ▶sub _split_formfeed {
Subroutine _split_formfeed: called CC=2 SCAR=6.9
101 64 my $text = shift;
102 64 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 64 $text =~ s/(?:$sgr)*\f(?:\e\[0*m)*/\f/g;
111 64 my @part = split /\f/, $text;
112 64 ▶ @part ? @part : '';
Branch: 1 branch
BranchTrueFalseTotal
@part ? : 63 1 64
113 }
114  
115 141 1;
« lib/App/ansicolumn/Border.pm ↑ Summary lib/App/ansicolumn/default.pm »