lib/App/ansicolumn/Util.pm

filter: Statementstmt 97.9% 144 / 147 Branchbran 85.4% 53 / 62 Conditioncond 79.0% 34 / 43 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 97.3% 37 / 38 Podpod 0.0% 0 / 23 totaltotal 84.0% 284 / 338 CC 50 SCAR 18.0
CC
50
Cov
92%
space_layout
18.0
do_fillup
6.3
do_space_layout
6.0
CRAP 6.0
« blib/lib/App/ansicolumn/Command.pm ↑ Summary lib/App/ansicolumn/Border.pm »
1 package App::ansicolumn;
2  
3 297 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 297 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 297 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # defined in App::ansicolumn
8 297 our($GROUP_SEP, $GROUP_FIRST, $GROUP_MARK);
9  
10 ######################################################################
11 # Object interface
12 ######################################################################
13  
14 2215 ▶sub get_border {
Subroutine get_border: called CC=2 SCAR=6.9
Pod undocumented
15 2215 ▶ my $border = shift->{BORDER} or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $border = +(shift())->{"BORDER"} or return "" 1121 1094 2215
16 1121 $border->get(@_);
17 }
18  
19 446 ▶sub border_width {
Subroutine border_width: called CC=1 SCAR=0.0
Pod undocumented
20 297 ▶ use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 446 my $obj = shift;
22 446 sum map length($obj->get_border($_)), @_;
23 }
24  
25 297 ▶use Text::ANSI::Fold qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 297 my %lb_flag = (
28 '' => LINEBREAK_NONE,
29 none => LINEBREAK_NONE,
30 runin => LINEBREAK_RUNIN,
31 runout => LINEBREAK_RUNOUT,
32 all => LINEBREAK_ALL,
33 );
34  
35 100 ▶sub lb_flag {
Subroutine lb_flag: called CC=1 SCAR=0.0
Pod undocumented
36 100 $lb_flag{shift->linebreak};
37 }
38  
39 227 ▶sub runin_margin {
Subroutine runin_margin: called CC=2 SCAR=6.9
Pod undocumented
40 227 my $obj = shift;
41 227 ▶ if ($lb_flag{$obj->linebreak} & LINEBREAK_RUNIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lb_flag{$obj->linebreak} & 1) { } 49 178 227
42 49 $obj->runin;
43 } else {
44 178 0;
45 }
46 }
47  
48 58 ▶sub term_size {
Subroutine term_size: called CC=2 SCAR=6.9
Pod undocumented
49 58 ▶ @{ shift->{TERM_SIZE} //= [ terminal_size() ] };
Condition100%2/2
+(shift())->{"TERM_SIZE"} //= [terminal_size()]
l!l
256
MC/DC100%1/1
+(shift())->{"TERM_SIZE"} //= [terminal_size()]
+(shift())->{"TERM_SIZE"}
Truth table100%2/2
A
A= +(shift())->{"TERM_SIZE"}
Aresult
11
00
50 }
51  
52 58 ▶sub term_width {
Subroutine term_width: called CC=1 SCAR=0.0
Pod undocumented
53 58 (shift->term_size)[0];
54 }
55  
56 0 ▶sub term_height {
Subroutine term_height: not called CC=1 SCAR=6.9
Pod undocumented
57 0 (shift->term_size)[1];
58 }
59  
60 331 ▶sub get_width {
Subroutine get_width: called CC=2 SCAR=6.9
Pod undocumented
61 331 my $obj = shift;
62 331 ▶ $obj->width || $obj->term_width;
Branch: 1 branch
BranchTrueFalseTotal
$obj->width or $obj->term_width 279 52 331
63 }
64  
65 820 ▶sub effective_height {
Subroutine effective_height: called CC=1 SCAR=0.0
Pod undocumented
66 820 my $obj = shift;
67 820 $obj->height - $obj->border_height;
68 }
69  
70 100 ▶sub foldobj {
Subroutine foldobj: called CC=2 SCAR=6.9
Pod undocumented
71 100 my $obj = shift;
72 100 my $width = shift;
73 297 ▶ use Text::ANSI::Fold;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 100 my $fold = Text::ANSI::Fold->new(
75 width => $width,
76 margin => $obj->runin_margin,
77 boundary => $obj->boundary,
78 linebreak => $obj->lb_flag,
79 runin => $obj->runin,
80 runout => $obj->runout,
81 ambiguous => $obj->ambiguous,
82 padchar => $obj->padchar,
83 padding => 1,
84 );
85 100 ▶ if ($obj->discard_el) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->discard_el) 100 0 100
86 100 $fold->configure(discard => [ 'EL' ] );
87 }
88 100 $fold;
89 }
90  
91 93 ▶sub foldsub {
Subroutine foldsub: called CC=3 SCAR=11.1
Pod undocumented
92 93 my $obj = shift;
93 93 my $width = shift;
94 93 my $fold = $obj->foldobj($width);
95 93 ▶ if ((my $ls = $obj->linestyle) eq 'truncate') {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $ls = $obj->linestyle) eq 'truncate') { } 2 91 93
elsif ($ls eq 'wrap') { } 91 0 91
96 2 ▶ sub { ($fold->fold($_[0]))[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
97 } elsif ($ls eq 'wrap') {
98 91 ▶ sub { $fold->text($_[0])->chops };
Subroutine __ANON__: called CC=1 SCAR=0.0
99 } else {
100 0 undef;
101 }
102 }
103  
104 105 ▶sub set_layout {
Subroutine set_layout: called CC=2 SCAR=6.9
Pod undocumented
105 105 my $obj = shift;
106 105 my($dp, @part) = @_;
107 105 $obj->do_space_layout($dp, @part)
108 ->do_fillup($dp);
109 105 ▶ strip_group_marks($dp) if $obj->intact;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->intact 12 93 105
110 105 return $obj;
111 }
112  
113 # The marks of the lines of a group (see prepare) are taken out after
114 # the layout, with --intact, where they are made.
115 12 ▶sub strip_group_marks {
Subroutine strip_group_marks: called CC=2 SCAR=6.9
Pod undocumented
116 12 my $dp = shift;
117 12 s/^$GROUP_MARK// for @$dp;
118 }
119  
120 105 ▶sub do_space_layout {
Subroutine do_space_layout: called CC=6 SCAR=18.0
Pod undocumented
121 105 my $obj = shift;
122 105 my($dp, @part) = @_;
123 105 ▶ my $height = $obj->effective_height || die;
Condition50%1/2
$obj->effective_height || die()
l!l
1050
MC/DC0%0/1
$obj->effective_height || die()
$obj->effective_height
Truth table50%1/2
A
A= $obj->effective_height
Aresult
11
00
124 105 ▶ return if $height <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $height <= 0 0 105 105
125 # The parts are what formfeeds divide, and each begins a pane.
126 # Lay out each part by itself, so that moving lines within a part
127 # never carries the break into a pane already laid out.
128 105 @$dp = ();
129 105 for my $i (keys @part) {
130 131 $obj->space_layout($part[$i], $height);
131 131 push @$dp, @{$part[$i]};
132 # every part but the last ends at the bottom of a pane, and
133 # takes one even without a line: an empty page
134 131 ▶ if ($i < $#part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#part) 26 105 131
135 26 ▶ push @$dp, $obj->fillup_str if @{$part[$i]} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$part[$i];} == 0 6 20 26
136 26 _fillup($dp, $height, $obj->fillup_str);
137 }
138 }
139 105 return $obj;
140 }
141  
142 131 ▶sub space_layout {
Subroutine space_layout: called CC=18 SCAR=28.9
Pod undocumented
143 131 my $obj = shift;
144 131 my($dp, $height) = @_;
145 131 ▶ for (my $page = 0; (my $top = $page * $height) < @$dp; $page++) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $top = $page * $height) < @$dp) 350 131 481
146 350 my $moved;
147 350 ▶ if ($height >= 4 and $top > 2 and !$obj->isolation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height >= 4 and $top > 2 and not $obj->isolation) 16 334 350
Condition100%6/6
$height >= 4 and $top > 2 and not $obj->isolation
!ll&&!rl&&r
2765816
$height >= 4 and $top > 2
!ll&&!rl&&r
2136374
MC/DC100%3/3
$height >= 4 and $top > 2 and not $obj->isolation
$height >= 4 $top > 2 not $obj->isolation
Truth table100%4/4
A and B and not(C)
A= $height >= 4B= $top > 2C= not $obj->isolation
ABCresult
0XX0
10X0
1100
1111
148 16 ▶ if ($dp->[$top - 2] !~ /\S/ and
Branch: 1 branch
BranchTrueFalseTotal
if (not $$dp[$top - 2] =~ /\S/u and $$dp[$top - 1] =~ /\S/u) 6 10 16
Condition100%3/3
not $$dp[$top - 2] =~ /\S/u and $$dp[$top - 1] =~ /\S/u
!ll&&!rl&&r
916
MC/DC100%2/2
not $$dp[$top - 2] =~ /\S/u and $$dp[$top - 1] =~ /\S/u
not $$dp[$top - 2] =~ /\S/u $$dp[$top - 1] =~ /\S/u
Truth table100%3/3
not(A) and B
A= not $$dp[$top - 2] =~ /\S/uB= $$dp[$top - 1] =~ /\S/u
ABresult
0X0
100
111
149 $dp->[$top - 1] =~ /\S/
150 ) {
151 6 splice @$dp, $top - 1, 0, '';
152 6 $moved = 1;
153 }
154 }
155 350 ▶ if (not $moved and not $obj->white_space) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($moved or $obj->white_space) 43 307 350
Condition100%3/3
$moved or $obj->white_space
l!l&&r!l&&!r
630143
MC/DC100%2/2
$moved or $obj->white_space
$moved $obj->white_space
Truth table100%3/3
A or B
A= $movedB= $obj->white_space
ABresult
1X1
011
000
156 43 ▶ while ($top < @$dp and $dp->[$top] !~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top < @$dp and not $dp->[$top] =~ /\S/u) 9 43 52
Condition66%2/3
$top < @$dp and not $dp->[$top] =~ /\S/u
!ll&&!rl&&r
0439
MC/DC50%1/2
$top < @$dp and not $dp->[$top] =~ /\S/u
$top < @$dp not $dp->[$top] =~ /\S/u
Truth table66%2/3
A and not(B)
A= $top < @$dpB= not $dp->[$top] =~ /\S/u
ABresult
0X0
100
111
157 9 splice @$dp, $top, 1;
158 }
159 }
160 # A group which goes over the bottom of this pane, and is not
161 # higher than a pane, goes to the top of the next: empty lines
162 # are put before it.
163 350 my $bottom = $top + $height;
164 350 ▶ for (my $i = $top + 1; $i < $bottom and $i < @$dp; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bottom and $i < @$dp) 1624 345 1969
Condition100%3/3
$i < $bottom and $i < @$dp
!ll&&!rl&&r
285601624
MC/DC100%2/2
$i < $bottom and $i < @$dp
$i < $bottom $i < @$dp
Truth table100%3/3
A and B
A= $i < $bottomB= $i < @$dp
ABresult
0X0
100
111
165 1624 ▶ $dp->[$i] =~ /^$GROUP_FIRST/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$dp->[$i] =~ /^$App::ansicolumn::GROUP_FIRST/u or next 7 1617 1624
166 7 my $end = $i;
167 7 ▶ $end++ while $end < $#$dp and $dp->[$end + 1] =~ /^$GROUP_SEP/;
Branch: 1 branch
BranchTrueFalseTotal
$end < $dp->$#* and $$dp[$end + 1] =~ /^$App::ansicolumn::GROUP_SEP/u and do { ++$end } 14 7 21
Condition66%2/3
$end < $dp->$#* and $$dp[$end + 1] =~ /^$App::ansicolumn::GROUP_SEP/u
!ll&&!rl&&r
0714
MC/DC50%1/2
$end < $dp->$#* and $$dp[$end + 1] =~ /^$App::ansicolumn::GROUP_SEP/u
$end < $dp->$#* $$dp[$end + 1] =~ /^$App::ansicolumn::GROUP_SEP/u
Truth table66%2/3
A and B
A= $end < $dp->$#*B= $$dp[$end + 1] =~ /^$App::ansicolumn::GROUP_SEP/u
ABresult
0X0
100
111
168 7 ▶ if ($end >= $bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end >= $bottom) 5 2 7
169 5 ▶ splice @$dp, $i, 0, ('') x ($bottom - $i) if $end - $i < $height;
Branch: 1 branch
BranchTrueFalseTotal
if $end - $i < $height 4 1 5
170 5 last;
171 }
172 2 $i = $end;
173 }
174 }
175 131 return $obj;
176 }
177  
178 68 ▶sub _fillup {
Subroutine _fillup: called CC=2 SCAR=6.9
179 68 my($dp, $len, $str) = @_;
180 68 ▶ if (my $remmant = @$dp % $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remmant = @$dp % $len) 46 22 68
181 46 push @$dp, ($str) x ($len - $remmant);
182 }
183 }
184  
185 105 ▶sub do_fillup {
Subroutine do_fillup: called CC=6 SCAR=18.5
Pod undocumented
186 105 my $obj = shift;
187 105 my $dp = shift;
188 105 ▶ my $line = $obj->effective_height || die;
Condition50%1/2
$obj->effective_height || die()
l!l
1050
MC/DC0%0/1
$obj->effective_height || die()
$obj->effective_height
Truth table50%1/2
A
A= $obj->effective_height
Aresult
11
00
189 105 ▶ defined $obj->fillup and $obj->fillup !~ /^(?:no|none)$/
Branch: 1 branch
BranchTrueFalseTotal
defined $obj->fillup and not $obj->fillup =~ /^(?:no|none)$/u or return 42 63 105
Condition66%2/3
defined $obj->fillup and not $obj->fillup =~ /^(?:no|none)$/u
!ll&&!rl&&r
63042
MC/DC50%1/2
defined $obj->fillup and not $obj->fillup =~ /^(?:no|none)$/u
defined $obj->fillup not $obj->fillup =~ /^(?:no|none)$/u
Truth table66%2/3
A and not(B)
A= defined $obj->fillupB= not $obj->fillup =~ /^(?:no|none)$/u
ABresult
0X0
100
111
190 or return;
191 42 ▶ $obj->{fillup} ||= 'pane';
Condition50%1/2
$obj->{"fillup"} ||= "pane"
l!l
420
MC/DC0%0/1
$obj->{"fillup"} ||= "pane"
$obj->{"fillup"}
Truth table50%1/2
A
A= $obj->{"fillup"}
Aresult
11
00
192 42 ▶ $line *= $obj->panes if $obj->fillup eq 'page';
Branch: 1 branch
BranchTrueFalseTotal
if $obj->fillup eq "page" 0 42 42
193 42 _fillup $dp, $line, $obj->fillup_str;
194 42 return $obj;
195 }
196  
197 ######################################################################
198  
199 161 ▶sub newlist {
Subroutine newlist: called CC=5 SCAR=16.2
Pod undocumented
200 161 my %arg = (count => 0);
201 161 ▶ while (@_ and not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not ref $_[0]) 322 161 483
Condition66%2/3
@_ and not ref $_[0]
!ll&&!rl&&r
0161322
MC/DC50%1/2
@_ and not ref $_[0]
@_ not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_B= not ref $_[0]
ABresult
0X0
100
111
202 322 my($name, $value) = splice(@_, 0, 2);
203 322 $arg{$name} = $value;
204 }
205 161 my @list = ($arg{default}) x $arg{count};
206 161 ▶ while (my($index, $value) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 322 161 483
207 322 ▶ $index = [ $index ] if not ref $index;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $index 0 322 322
208 322 @list[@$index] = ($value) x @$index;
209 }
210 161 @list;
211 }
212  
213 440 ▶sub div {
Subroutine div: called CC=1 SCAR=0.0
Pod undocumented
214 297 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 440 my($a, $b) = @_;
216 440 ($a + $b - 1) / $b;
217 }
218  
219 303 ▶sub roundup ($$;$) {
Subroutine roundup: called CC=3 SCAR=11.0
Pod undocumented
220 297 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 303 my($a, $b, $c) = @_;
222 303 ▶ return $a if $b == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $b == 0 0 303 303
223 303 ▶ div($a + ($c // 0), $b) * $b;
Condition100%2/2
$c // 0
l!l
24279
MC/DC100%1/1
$c // 0
$c
Truth table100%2/2
A
A= $c
Aresult
11
00
224 }
225  
226 56 ▶sub terminal_size {
Subroutine terminal_size: called CC=3 SCAR=11.4
Pod undocumented
227 297 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 56 my @default = (80, 24);
229 56 my @size;
230 56 ▶ if (open my $tty, ">", "/dev/tty") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $tty, ">", "/dev/tty") 0 56 56
231 # Term::ReadKey 2.31 on macOS 10.15 has a bug in argument handling
232 # and the latest version 2.38 fails to install.
233 # This code should work on both versions.
234 0 @size = GetTerminalSize $tty, $tty;
235 }
236 56 ▶ @size ? @size : @default;
Branch: 1 branch
BranchTrueFalseTotal
@size ? : 0 56 56
237 }
238  
239 379 ▶sub xpose {
Subroutine xpose: called CC=1 SCAR=0.0
Pod undocumented
240 297 ▶ use List::Util 1.56 qw(zip);
Subroutine BEGIN: called CC=1 SCAR=0.0
241 379 map { [ grep { defined } @$_ ] } zip @_;
242 }
243  
244 1 ▶sub insert_space {
Subroutine insert_space: called CC=1 SCAR=0.0
Pod undocumented
245 297 ▶ use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
246 1 ▶ map { @$_ } reduce {
Subroutine __ANON__: called CC=4 SCAR=14.2
247 5 ▶ [ @$a, (@$a && $a->[-1] ne '' && $b ne '' ? '' : ()), $b ]
Branch: 1 branch
BranchTrueFalseTotal
@$a && $App::ansicolumn::a->[-1] ne '' && $b ne '' ? : 4 1 5
Condition66%4/6
@$a and $App::ansicolumn::a->[-1] ne "" and $b ne ""
!ll&&!rl&&r
104
@$a and $App::ansicolumn::a->[-1] ne ""
!ll&&!rl&&r
104
MC/DC33%1/3
@$a and $App::ansicolumn::a->[-1] ne "" and $b ne ""
@$a $App::ansicolumn::a->[-1] ne "" $b ne ""
Truth table50%2/4
A and B and C
A= @$aB= $App::ansicolumn::a->[-1] ne ""C= $b ne ""
ABCresult
0XX0
10X0
1100
1111
248 1 } [], @_;
249 }
250  
251 295 ▶sub decode_argv {
Subroutine decode_argv: called CC=2 SCAR=7.2
Pod undocumented
252 map {
253 295 ▶ utf8::is_utf8($_) ? $_ : decode('utf8', $_);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($_) ? : 0 1931 1931
254 }
255 @_;
256 }
257  
258 297 1;
« blib/lib/App/ansicolumn/Command.pm ↑ Summary lib/App/ansicolumn/Border.pm »