lib/App/ansicolumn/Util.pm

filter: Statementstmt 93.9% 125 / 133 Branchbran 76.0% 38 / 50 Conditioncond 58.8% 20 / 34 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 89.1% 33 / 37 Podpod 0.0% 0 / 22 totaltotal 75.9% 224 / 295 CC 40 SCAR 15.6
CC
40
Cov
84%
__ANON__
20.0
space_layout
10.0
do_fillup
6.3
CRAP 4.8
« blib/lib/App/ansicolumn/Command.pm ↑ Summary lib/App/ansicolumn/Border.pm »
1 package App::ansicolumn;
2  
3 166 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 166 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 166 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 ######################################################################
8 # Object interface
9 ######################################################################
10  
11 1676 ▶sub get_border {
Subroutine get_border: called CC=2 SCAR=6.9
Pod undocumented
12 1676 ▶ my $border = shift->{BORDER} or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $border = +(shift())->{"BORDER"} or return "" 854 822 1676
13 854 $border->get(@_);
14 }
15  
16 349 ▶sub border_width {
Subroutine border_width: called CC=1 SCAR=0.0
Pod undocumented
17 166 ▶ use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 349 my $obj = shift;
19 349 sum map length($obj->get_border($_)), @_;
20 }
21  
22 166 ▶use Text::ANSI::Fold qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 166 my %lb_flag = (
25 '' => LINEBREAK_NONE,
26 none => LINEBREAK_NONE,
27 runin => LINEBREAK_RUNIN,
28 runout => LINEBREAK_RUNOUT,
29 all => LINEBREAK_ALL,
30 );
31  
32 80 ▶sub lb_flag {
Subroutine lb_flag: called CC=1 SCAR=0.0
Pod undocumented
33 80 $lb_flag{shift->linebreak};
34 }
35  
36 176 ▶sub runin_margin {
Subroutine runin_margin: called CC=2 SCAR=6.9
Pod undocumented
37 176 my $obj = shift;
38 176 ▶ if ($lb_flag{$obj->linebreak} & LINEBREAK_RUNIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lb_flag{$obj->linebreak} & 1) { } 38 138 176
39 38 $obj->runin;
40 } else {
41 138 0;
42 }
43 }
44  
45 33 ▶sub term_size {
Subroutine term_size: called CC=2 SCAR=7.2
Pod undocumented
46 33 ▶ @{ shift->{TERM_SIZE} //= [ terminal_size() ] };
Condition50%1/2
+(shift())->{"TERM_SIZE"} //= [terminal_size()]
l!l
033
MC/DC0%0/1
+(shift())->{"TERM_SIZE"} //= [terminal_size()]
+(shift())->{"TERM_SIZE"}
Truth table50%1/2
A
A= +(shift())->{"TERM_SIZE"}
Aresult
11
00
47 }
48  
49 33 ▶sub term_width {
Subroutine term_width: called CC=1 SCAR=0.0
Pod undocumented
50 33 (shift->term_size)[0];
51 }
52  
53 0 ▶sub term_height {
Subroutine term_height: not called CC=1 SCAR=6.9
Pod undocumented
54 0 (shift->term_size)[1];
55 }
56  
57 128 ▶sub get_width {
Subroutine get_width: called CC=2 SCAR=6.9
Pod undocumented
58 128 my $obj = shift;
59 128 ▶ $obj->width || $obj->term_width;
Branch: 1 branch
BranchTrueFalseTotal
$obj->width or $obj->term_width 95 33 128
60 }
61  
62 513 ▶sub effective_height {
Subroutine effective_height: called CC=1 SCAR=0.0
Pod undocumented
63 513 my $obj = shift;
64 513 $obj->height - $obj->border_height;
65 }
66  
67 80 ▶sub foldobj {
Subroutine foldobj: called CC=2 SCAR=6.9
Pod undocumented
68 80 my $obj = shift;
69 80 my $width = shift;
70 166 ▶ use Text::ANSI::Fold;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 80 my $fold = Text::ANSI::Fold->new(
72 width => $width,
73 margin => $obj->runin_margin,
74 boundary => $obj->boundary,
75 linebreak => $obj->lb_flag,
76 runin => $obj->runin,
77 runout => $obj->runout,
78 ambiguous => $obj->ambiguous,
79 padchar => $obj->padchar,
80 padding => 1,
81 );
82 80 ▶ if ($obj->discard_el) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->discard_el) 80 0 80
83 80 $fold->configure(discard => [ 'EL' ] );
84 }
85 80 $fold;
86 }
87  
88 80 ▶sub foldsub {
Subroutine foldsub: called CC=3 SCAR=12.6
Pod undocumented
89 80 my $obj = shift;
90 80 my $width = shift;
91 80 my $fold = $obj->foldobj($width);
92 80 ▶ if ((my $ls = $obj->linestyle) eq 'truncate') {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $ls = $obj->linestyle) eq 'truncate') { } 0 80 80
elsif ($ls eq 'wrap') { } 80 0 80
93 0 ▶ sub { ($fold->fold($_[0]))[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 } elsif ($ls eq 'wrap') {
95 80 ▶ sub { $fold->text($_[0])->chops };
Subroutine __ANON__: called CC=1 SCAR=0.0
96 } else {
97 0 undef;
98 }
99 }
100  
101 76 ▶sub set_layout {
Subroutine set_layout: called CC=1 SCAR=0.0
Pod undocumented
102 76 my $obj = shift;
103 76 my($dp, @part) = @_;
104 76 $obj->do_space_layout($dp, @part)
105 ->do_fillup($dp);
106 76 return $obj;
107 }
108  
109 76 ▶sub do_space_layout {
Subroutine do_space_layout: called CC=6 SCAR=18.0
Pod undocumented
110 76 my $obj = shift;
111 76 my($dp, @part) = @_;
112 76 ▶ my $height = $obj->effective_height || die;
Condition50%1/2
$obj->effective_height || die()
l!l
760
MC/DC0%0/1
$obj->effective_height || die()
$obj->effective_height
Truth table50%1/2
A
A= $obj->effective_height
Aresult
11
00
113 76 ▶ return if $height <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $height <= 0 0 76 76
114 # The parts are what formfeeds divide, and each begins a pane.
115 # Lay out each part by itself, so that moving lines within a part
116 # never carries the break into a pane already laid out.
117 76 @$dp = ();
118 76 for my $i (keys @part) {
119 102 $obj->space_layout($part[$i], $height);
120 102 push @$dp, @{$part[$i]};
121 # every part but the last ends at the bottom of a pane, and
122 # takes one even without a line: an empty page
123 102 ▶ if ($i < $#part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#part) 26 76 102
124 26 ▶ push @$dp, $obj->fillup_str if @{$part[$i]} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$part[$i];} == 0 6 20 26
125 26 _fillup($dp, $height, $obj->fillup_str);
126 }
127 }
128 76 return $obj;
129 }
130  
131 102 ▶sub space_layout {
Subroutine space_layout: called CC=10 SCAR=23.0
Pod undocumented
132 102 my $obj = shift;
133 102 my($dp, $height) = @_;
134 102 ▶ for (my $page = 0; (my $top = $page * $height) < @$dp; $page++) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $top = $page * $height) < @$dp) 166 102 268
135 166 ▶ if ($height >= 4 and $top > 2 and !$obj->isolation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height >= 4 and $top > 2 and not $obj->isolation) 14 152 166
Condition100%6/6
$height >= 4 and $top > 2 and not $obj->isolation
!ll&&!rl&&r
1232914
$height >= 4 and $top > 2
!ll&&!rl&&r
774643
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
136 14 ▶ if ($dp->[$top - 2] !~ /\S/ and
Branch: 1 branch
BranchTrueFalseTotal
if (not $$dp[$top - 2] =~ /\S/u and $$dp[$top - 1] =~ /\S/u) 5 9 14
Condition66%2/3
not $$dp[$top - 2] =~ /\S/u and $$dp[$top - 1] =~ /\S/u
!ll&&!rl&&r
905
MC/DC50%1/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 table66%2/3
not(A) and B
A= not $$dp[$top - 2] =~ /\S/uB= $$dp[$top - 1] =~ /\S/u
ABresult
0X0
100
111
137 $dp->[$top - 1] =~ /\S/
138 ) {
139 5 splice @$dp, $top - 1, 0, '';
140 5 next;
141 }
142 }
143 161 ▶ if (not $obj->white_space) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->white_space) 43 118 161
144 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
145 9 splice @$dp, $top, 1;
146 }
147 }
148 }
149 102 return $obj;
150 }
151  
152 55 ▶sub _fillup {
Subroutine _fillup: called CC=2 SCAR=6.9
153 55 my($dp, $len, $str) = @_;
154 55 ▶ if (my $remmant = @$dp % $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remmant = @$dp % $len) 41 14 55
155 41 push @$dp, ($str) x ($len - $remmant);
156 }
157 }
158  
159 76 ▶sub do_fillup {
Subroutine do_fillup: called CC=6 SCAR=18.5
Pod undocumented
160 76 my $obj = shift;
161 76 my $dp = shift;
162 76 ▶ my $line = $obj->effective_height || die;
Condition50%1/2
$obj->effective_height || die()
l!l
760
MC/DC0%0/1
$obj->effective_height || die()
$obj->effective_height
Truth table50%1/2
A
A= $obj->effective_height
Aresult
11
00
163 76 ▶ defined $obj->fillup and $obj->fillup !~ /^(?:no|none)$/
Branch: 1 branch
BranchTrueFalseTotal
defined $obj->fillup and not $obj->fillup =~ /^(?:no|none)$/u or return 29 47 76
Condition66%2/3
defined $obj->fillup and not $obj->fillup =~ /^(?:no|none)$/u
!ll&&!rl&&r
47029
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
164 or return;
165 29 ▶ $obj->{fillup} ||= 'pane';
Condition50%1/2
$obj->{"fillup"} ||= "pane"
l!l
290
MC/DC0%0/1
$obj->{"fillup"} ||= "pane"
$obj->{"fillup"}
Truth table50%1/2
A
A= $obj->{"fillup"}
Aresult
11
00
166 29 ▶ $line *= $obj->panes if $obj->fillup eq 'page';
Branch: 1 branch
BranchTrueFalseTotal
if $obj->fillup eq "page" 0 29 29
167 29 _fillup $dp, $line, $obj->fillup_str;
168 29 return $obj;
169 }
170  
171 ######################################################################
172  
173 61 ▶sub newlist {
Subroutine newlist: called CC=5 SCAR=16.2
Pod undocumented
174 61 my %arg = (count => 0);
175 61 ▶ while (@_ and not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not ref $_[0]) 122 61 183
Condition66%2/3
@_ and not ref $_[0]
!ll&&!rl&&r
061122
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
176 122 my($name, $value) = splice(@_, 0, 2);
177 122 $arg{$name} = $value;
178 }
179 61 my @list = ($arg{default}) x $arg{count};
180 61 ▶ while (my($index, $value) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 122 61 183
181 122 ▶ $index = [ $index ] if not ref $index;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $index 0 122 122
182 122 @list[@$index] = ($value) x @$index;
183 }
184 61 @list;
185 }
186  
187 352 ▶sub div {
Subroutine div: called CC=1 SCAR=0.0
Pod undocumented
188 166 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
189 352 my($a, $b) = @_;
190 352 ($a + $b - 1) / $b;
191 }
192  
193 234 ▶sub roundup ($$;$) {
Subroutine roundup: called CC=3 SCAR=11.0
Pod undocumented
194 166 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
195 234 my($a, $b, $c) = @_;
196 234 ▶ return $a if $b == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $b == 0 0 234 234
197 234 ▶ div($a + ($c // 0), $b) * $b;
Condition100%2/2
$c // 0
l!l
24210
MC/DC100%1/1
$c // 0
$c
Truth table100%2/2
A
A= $c
Aresult
11
00
198 }
199  
200 33 ▶sub terminal_size {
Subroutine terminal_size: called CC=3 SCAR=11.4
Pod undocumented
201 166 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 33 my @default = (80, 24);
203 33 my @size;
204 33 ▶ if (open my $tty, ">", "/dev/tty") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $tty, ">", "/dev/tty") 0 33 33
205 # Term::ReadKey 2.31 on macOS 10.15 has a bug in argument handling
206 # and the latest version 2.38 fails to install.
207 # This code should work on both versions.
208 0 @size = GetTerminalSize $tty, $tty;
209 }
210 33 ▶ @size ? @size : @default;
Branch: 1 branch
BranchTrueFalseTotal
@size ? : 0 33 33
211 }
212  
213 107 ▶sub xpose {
Subroutine xpose: called CC=1 SCAR=0.0
Pod undocumented
214 166 ▶ use List::Util 1.56 qw(zip);
Subroutine BEGIN: called CC=1 SCAR=0.0
215 107 map { [ grep { defined } @$_ ] } zip @_;
216 }
217  
218 0 ▶sub insert_space {
Subroutine insert_space: not called CC=1 SCAR=4.0
Pod undocumented
219 166 ▶ use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
220 0 ▶ map { @$_ } reduce {
Subroutine __ANON__: not called CC=4 SCAR=30.0
221 0 ▶ [ @$a, (@$a && $a->[-1] ne '' && $b ne '' ? '' : ()), $b ]
Branch: 1 branch
BranchTrueFalseTotal
@$a && $App::ansicolumn::a->[-1] ne '' && $b ne '' ? : 0 0 0
Condition0%0/6
@$a and $App::ansicolumn::a->[-1] ne "" and $b ne ""
!ll&&!rl&&r
000
@$a and $App::ansicolumn::a->[-1] ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
@$a and $App::ansicolumn::a->[-1] ne "" and $b ne ""
@$a $App::ansicolumn::a->[-1] ne "" $b ne ""
Truth table0%0/4
A and B and C
A= @$aB= $App::ansicolumn::a->[-1] ne ""C= $b ne ""
ABCresult
0XX0
10X0
1100
1111
222 0 } [], @_;
223 }
224  
225 164 ▶sub decode_argv {
Subroutine decode_argv: called CC=2 SCAR=7.2
Pod undocumented
226 map {
227 164 ▶ utf8::is_utf8($_) ? $_ : decode('utf8', $_);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($_) ? : 0 929 929
228 }
229 @_;
230 }
231  
232 166 1;
« blib/lib/App/ansicolumn/Command.pm ↑ Summary lib/App/ansicolumn/Border.pm »