lib/App/ansicolumn/Table.pm

filter: Statementstmt 98.4% 126 / 128 Branchbran 86.5% 45 / 52 Conditioncond 78.9% 30 / 38 MC/DCmcdc 50.0% 12 / 24 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 7 totaltotal 86.3% 227 / 263 CC 51 SCAR 24.4
CC
51
Cov
92%
expand_table_rows
18.9
table_widths
15.0
fit_table_rows
9.0
CRAP 11.5
« lib/App/ansicolumn.pm ↑ Summary blib/lib/App/ansicolumn/Command.pm »
1 package App::ansicolumn;
2  
3 157 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 157 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 157 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 157 ▶use List::Util qw(max min sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ######################################################################
10 # Table mode: width allocation and cell wrapping
11 ######################################################################
12  
13 #
14 # table_widths(\@content, $budget, $overhead, \@fixed, $floor)
15 #
16 # Decide how wide the content of each table column may be.
17 #
18 # @content width of the widest cell in each column, as given
19 # $budget width available to all columns together: the -w width
20 # minus the separators; undef when no width is requested
21 # $overhead extra width every column costs for --item-format
22 # @fixed width already decided for a column (by --cell, or by a
23 # previous round of fit_table_rows); undef for free
24 # columns (the whole argument may be undef)
25 # $floor minimum width given to a free column
26 #
27 # Fixed columns get their fixed width. Free columns whose widest cell
28 # fits get that width. If that does not fit the budget, the free
29 # columns share it by the water level rule: find the largest L (not
30 # below $floor) such that every free column with min(content, L)
31 # fits the budget; columns narrower than L keep their width, the
32 # others are cut to L and will be wrapped. Dividing by L leaves a
33 # remainder smaller than the number of cut columns; it goes to the
34 # cut columns one column of width each, widest content first.
35 #
36 60 ▶sub table_widths {
Subroutine table_widths: called CC=15 SCAR=27.1
Pod undocumented
37 60 my($content, $budget, $overhead, $fixed, $floor) = @_;
38 60 my @target = @$content;
39 60 my @free;
40 60 for my $i (keys @target) {
41 135 ▶ if ($fixed and defined $fixed->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixed and defined $fixed->[$i]) { } 25 110 135
Condition100%3/3
$fixed and defined $fixed->[$i]
!ll&&!rl&&r
327825
MC/DC100%2/2
$fixed and defined $fixed->[$i]
$fixed defined $fixed->[$i]
Truth table100%3/3
A and B
A= $fixedB= defined $fixed->[$i]
ABresult
0X0
100
111
42 25 $target[$i] = $fixed->[$i];
43 } else {
44 110 push @free, $i;
45 }
46 }
47 60 ▶ return \@target unless defined $budget and @free;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $budget and @free 15 45 60
Condition100%3/3
defined $budget and @free
!ll&&!rl&&r
14145
MC/DC100%2/2
defined $budget and @free
defined $budget @free
Truth table100%3/3
A and B
A= defined $budgetB= @free
ABresult
0X0
100
111
48 45 my $remain = $budget;
49 $remain -= $target[$_] + $overhead
50 45 ▶ for grep { $fixed and defined $fixed->[$_] } keys @target;
Condition100%3/3
$fixed && defined $fixed->[$_]
!ll&&!rl&&r
30716
MC/DC100%2/2
$fixed && defined $fixed->[$_]
$fixed defined $fixed->[$_]
Truth table100%3/3
A && B
A= $fixedB= defined $fixed->[$_]
ABresult
0X0
100
111
51 1904 ▶ my $cost = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1904 my $level = shift;
53 1904 sum map { min($content->[$_], $level) + $overhead } @free;
54 45 };
55 45 ▶ return \@target if $cost->(max @$content) <= $remain;
Branch: 1 branch
BranchTrueFalseTotal
if &$cost(&max(@$content)) <= $remain 7 38 45
56 38 my $level = $floor;
57 38 ▶ for (my $l = max(@{$content}[@free]); $l > $floor; $l--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $floor) 1821 4 1825
58 1821 ▶ if ($cost->($l) <= $remain) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$cost($l) <= $remain) 34 1787 1821
59 34 $level = $l;
60 34 last;
61 }
62 }
63 38 $target[$_] = min($content->[$_], $level) for @free;
64 38 my $left = $remain - $cost->($level);
65 38 ▶ if ($left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 6 32 38
66 6 ▶ my @wrapped = sort { $content->[$b] <=> $content->[$a] or $a <=> $b }
Condition66%2/3
$content->[$b] <=> $content->[$a] || $a <=> $b
l!l&&r!l&&!r
330
MC/DC0%0/2
$content->[$b] <=> $content->[$a] || $a <=> $b
$content->[$b] <=> $content->[$a] $a <=> $b
Truth table66%2/3
A || B
A= $content->[$b] <=> $content->[$a]B= $a <=> $b
ABresult
1X1
011
000
67 6 grep { $content->[$_] > $level } @free;
68 6 $target[$_]++ for @wrapped[0 .. min($left, scalar @wrapped) - 1];
69 }
70 38 \@target;
71 }
72  
73 #
74 # parse_cell_spec($spec, \@content)
75 #
76 # Resolve --cell specification for table mode. Entries are separated
77 # by comma and the last entry is repeated for remaining columns.
78 #
79 # N fixed width N
80 # <N upper bound: min(content, N) (also <=N, =N, bare = or <)
81 # +N -N relative to the natural content width (minimum 1)
82 # 0 automatic (no constraint)
83 #
84 # Returns array reference with a width or undef for each column.
85 #
86 20 ▶sub parse_cell_spec {
Subroutine parse_cell_spec: called CC=8 SCAR=20.8
Pod undocumented
87 20 my($spec, $content) = @_;
88 20 my @entry = split /,+/, $spec;
89 20 my @fixed;
90 20 for my $i (keys @$content) {
91 38 ▶ my $e = $entry[$i] // $entry[-1];
Condition66%2/3
$entry[$i] // $entry[-1]
l!l&&r!l&&!r
3260
MC/DC0%0/2
$entry[$i] // $entry[-1]
$entry[$i] $entry[-1]
Truth table66%2/3
A // B
A= $entry[$i]B= $entry[-1]
ABresult
1X1
011
000
92 38 my $w;
93 38 ▶ if ($e eq q(0)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($e eq '0') { } 9 29 38
elsif ($e =~ /^\d+$/u) { } 19 10 29
elsif ($e =~ /^[-+]\d+$/u) { } 2 8 10
elsif ($e =~ /^(?:<=|[<=])(\d*)$/u) { } 7 1 8
94 9 $w = undef;
95 }
96 elsif ($e =~ /^\d+$/) {
97 19 $w = $e;
98 }
99 elsif ($e =~ /^[-+]\d+$/) {
100 2 $w = max(1, $content->[$i] + $e);
101 }
102 elsif ($e =~ /^(?:<=|[<=])(\d*)$/) {
103 7 ▶ $w = $1 ? min($content->[$i], $1) : $content->[$i];
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 6 1 7
104 }
105 else {
106 1 die "Invalid number: $spec\n";
107 }
108 37 push @fixed, $w;
109 }
110 19 \@fixed;
111 }
112  
113 157 ▶use Text::ANSI::Fold qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 157 ▶use Text::ANSI::Fold::Util qw(ansi_width);
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 #
117 # table_fold($width)
118 #
119 # Text::ANSI::Fold object used to wrap table cells. Word boundary
120 # and full line-break (kinsoku) processing are on unless the user
121 # chose otherwise. Run-in characters need room, so the run-in width
122 # is reserved as margin (clamped below the width; Text::ANSI::Fold
123 # refuses margin >= width). table_margin($width) gives that margin;
124 # cells are folded at $width - margin.
125 #
126 169 ▶sub table_lb {
Subroutine table_lb: called CC=2 SCAR=7.2
Pod undocumented
127 169 my $obj = shift;
128 169 ▶ $obj->linebreak ? $obj->lb_flag : LINEBREAK_ALL;
Branch: 1 branch
BranchTrueFalseTotal
$obj->linebreak ? : 0 169 169
129 }
130  
131 125 ▶sub table_margin {
Subroutine table_margin: called CC=2 SCAR=7.2
Pod undocumented
132 125 my($obj, $width) = @_;
133 125 ▶ ($obj->table_lb & LINEBREAK_RUNIN) ? max(0, min($obj->runin, $width - 1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$obj->table_lb & 1 ? : 125 0 125
134 }
135  
136 44 ▶sub table_fold {
Subroutine table_fold: called CC=3 SCAR=11.2
Pod undocumented
137 44 my($obj, $width) = @_;
138 44 my $lb = $obj->table_lb;
139 44 my $margin = $obj->table_margin($width);
140 44 ▶ my $fold = Text::ANSI::Fold->new(
Condition50%1/2
$obj->boundary || 'word'
l!l
044
MC/DC0%0/1
$obj->boundary || 'word'
$obj->boundary
Truth table50%1/2
A
A= $obj->boundary
Aresult
11
00
141 width => $width,
142 margin => $margin,
143 boundary => ($obj->boundary || 'word'),
144 linebreak => $lb,
145 runin => $obj->runin,
146 runout => $obj->runout,
147 ambiguous => $obj->ambiguous,
148 padchar => $obj->padchar,
149 padding => 0,
150 );
151 44 ▶ $fold->configure(discard => [ 'EL' ]) if $obj->discard_el;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->discard_el 44 0 44
152 44 $fold;
153 }
154  
155 #
156 # expand_table_rows(\@rows, \@rw, \@target, \@content, \@continued, \@ow)
157 #
158 # Wrap cells wider than their target width and expand each logical
159 # row into as many physical rows as its tallest cell. Missing cell
160 # lines are filled with empty strings. Ragged rows stay ragged.
161 # \@rw holds the width of every cell of \@rows, and \@ow is set to
162 # the width of every cell of the rows returned, so that no cell is
163 # measured twice.
164 # When \@continued is given, $continued[row][col] is set for every
165 # cell that is the second or later line of a wrapped cell.
166 #
167 # In a column that has a cell wider than the target (its widest cell,
168 # $content[col], is wider), every cell wider than the target minus the
169 # run-in margin is wrapped, as lines are in panes, so that a cell
170 # fitting in the margin is not left longer than the wrapped lines.
171 #
172 47 ▶sub expand_table_rows {
Subroutine expand_table_rows: called CC=18 SCAR=29.4
Pod undocumented
173 47 my($obj, $rows, $rw, $target, $content, $continued, $ow) = @_;
174 47 ▶ @$continued = () if $continued;
Branch: 1 branch
BranchTrueFalseTotal
if $continued 47 0 47
175 47 @$ow = ();
176 my @limit = map {
177 47 my($i, $w) = ($_, $target->[$_]);
178 95 ▶ !defined $w ? undef :
Branch: 2 branches
BranchTrueFalseTotal
!defined($w) ? : 0 95 95
$content->[$i] > $w ? : 50 45 95
179 $content->[$i] > $w ? $w - $obj->table_margin($w) : $w;
180 } keys @$target;
181 47 my %fold;
182 my @out;
183 47 for my $r (keys @$rows) {
184 121 my $row = $rows->[$r];
185 121 my @widths;
186 my @cells = map {
187 121 my($cell, $w, $cw) = ($row->[$_], $target->[$_], $rw->[$r][$_]);
188 245 ▶ if (defined $w and $cw > $limit[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w and $cw > $limit[$_]) { } 71 174 245
Condition66%2/3
defined $w and $cw > $limit[$_]
!ll&&!rl&&r
017471
MC/DC50%1/2
defined $w and $cw > $limit[$_]
defined $w $cw > $limit[$_]
Truth table66%2/3
A and B
A= defined $wB= $cw > $limit[$_]
ABresult
0X0
100
111
189 71 ▶ my @chops = ($fold{$w} //= $obj->table_fold($w))->text($cell)->chops(length => \my @length);
Condition66%2/3
$fold{$w} //= $obj->table_fold($w)
l!l&&r!l&&!r
27440
MC/DC0%0/2
$fold{$w} //= $obj->table_fold($w)
$fold{$w} $obj->table_fold($w)
Truth table66%2/3
A //= B
A= $fold{$w}B= $obj->table_fold($w)
ABresult
1X1
011
000
190 71 for my $k (keys @chops) {
191 # trailing spaces are removed, and so is their width
192 297 ▶ $chops[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$chops[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/u or next 85 212 297
193 85 my $sp = $1;
194 85 ▶ $length[$k] = $sp =~ /^ +$/ ? $length[$k] - length $sp : ansi_width($chops[$k]);
Branch: 1 branch
BranchTrueFalseTotal
$sp =~ /^ +$/u ? : 85 0 85
195 }
196 0 ▶ @chops or do { @chops = (''); @length = (0) };
Branch: 1 branch
BranchTrueFalseTotal
@chops or do { @chops = ""; @length = 0 } 71 0 71
197 71 $widths[$_] = \@length;
198 71 \@chops;
199 } else {
200 174 $widths[$_] = [ $cw ];
201 174 [ $cell ];
202 }
203 } keys @$row;
204 121 my $height = max(1, map { int @$_ } @cells);
205 121 for my $k (0 .. $height - 1) {
206 320 ▶ push @out, [ map { $_->[$k] // '' } @cells ];
Condition100%2/2
$_->[$k] // ""
l!l
466184
MC/DC100%1/1
$_->[$k] // ""
$_->[$k]
Truth table100%2/2
A
A= $_->[$k]
Aresult
11
00
207 320 ▶ push @$ow, [ map { $_->[$k] // 0 } @widths ];
Condition100%2/2
$_->[$k] // 0
l!l
471179
MC/DC100%1/1
$_->[$k] // 0
$_->[$k]
Truth table100%2/2
A
A= $_->[$k]
Aresult
11
00
208 320 ▶ next unless $continued and $k;
Branch: 1 branch
BranchTrueFalseTotal
unless $continued and $k 121 199 320
Condition66%2/3
$continued and $k
!ll&&!rl&&r
0121199
MC/DC50%1/2
$continued and $k
$continued $k
Truth table66%2/3
A and B
A= $continuedB= $k
ABresult
0X0
100
111
209 199 $continued->[$#out][$_] = 1 for grep { defined $cells[$_][$k] } keys @cells;
210 }
211 }
212 47 @out;
213 }
214  
215 #
216 # fit_table_rows(\@rows, \@rw, \@content, $budget, $overhead, \@fixed, $floor, \@continued, \@ow)
217 #
218 # Wrap the table into the budget. Each round:
219 #
220 # 1. table_widths() decides the width of every column.
221 # 2. expand_table_rows() wraps the cells to those widths.
222 # 3. Measure how wide each wrapped column actually came out.
223 # 4. A wrapped column that left two or more columns of its width
224 # unused (its lines broke at word or kinsoku boundaries before
225 # the limit) is fixed at the measured width. Its wrapped lines
226 # are kept as they are.
227 # 5. If any column was fixed, go back to 1: the fixed column now
228 # costs only what it uses, so the remaining wrapped columns get
229 # more. Otherwise stop.
230 #
231 # One column short is not "unused": a double-width character cannot
232 # fill an odd width. Each round fixes at least one more column or
233 # stops, so the loop ends within (number of columns + 1) rounds.
234 #
235 # Returns the expanded rows. \@rw, \@continued and \@ow are passed
236 # to expand_table_rows(); \@continued and \@ow describe the rows
237 # returned.
238 #
239 44 ▶sub fit_table_rows {
Subroutine fit_table_rows: called CC=9 SCAR=22.0
Pod undocumented
240 44 my($obj, $rows, $rw, $content, $budget, $overhead, $fixed, $floor, $continued, $ow) = @_;
241 44 ▶ my @fix = $fixed ? @$fixed : ();
Branch: 1 branch
BranchTrueFalseTotal
$fixed ? : 13 31 44
242 44 my @wrap; # width each column was wrapped at
243 my @out;
244 44 while (1) {
245 47 my $target = table_widths($content, $budget, $overhead, \@fix, $floor);
246 47 ▶ my @width = map { $wrap[$_] // $target->[$_] } keys @$content;
Condition66%2/3
$wrap[$_] // $target->[$_]
l!l&&r!l&&!r
4910
MC/DC0%0/2
$wrap[$_] // $target->[$_]
$wrap[$_] $target->[$_]
Truth table66%2/3
A // B
A= $wrap[$_]B= $target->[$_]
ABresult
1X1
011
000
247 47 @out = $obj->expand_table_rows($rows, $rw, \@width, $content, $continued, $ow);
248 47 my $changed = 0;
249 47 for my $i (keys @$content) {
250 95 ▶ next if defined $fix[$i] or $content->[$i] <= $target->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $fix[$i] or $content->[$i] <= $target->[$i] 64 31 95
Condition100%3/3
defined $fix[$i] or $content->[$i] <= $target->[$i]
l!l&&r!l&&!r
224231
MC/DC100%2/2
defined $fix[$i] or $content->[$i] <= $target->[$i]
defined $fix[$i] $content->[$i] <= $target->[$i]
Truth table100%3/3
A or B
A= defined $fix[$i]B= $content->[$i] <= $target->[$i]
ABresult
1X1
011
000
251 31 my $usable = $target->[$i] - $obj->table_margin($target->[$i]);
252 31 ▶ my $measured = max 0, map { $_->[$i] // 0 } @$ow;
Condition50%1/2
$_->[$i] // 0
l!l
1580
MC/DC0%0/1
$_->[$i] // 0
$_->[$i]
Truth table50%1/2
A
A= $_->[$i]
Aresult
11
00
253 31 ▶ next if $measured >= $usable - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $measured >= $usable - 1 27 4 31
254 4 $fix[$i] = $measured;
255 4 $wrap[$i] = $target->[$i];
256 4 $changed++;
257 }
258 47 ▶ last unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 44 3 47
259 }
260 44 @out;
261 }
262  
263 157 1;
« lib/App/ansicolumn.pm ↑ Summary blib/lib/App/ansicolumn/Command.pm »