lib/App/ansicolumn/Table.pm

filter: Statementstmt 100% 208 / 208 Branchbran 91.8% 79 / 86 Conditioncond 82.2% 51 / 62 MC/DCmcdc 57.8% 22 / 38 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 13 totaltotal 89.0% 383 / 430 CC 81 SCAR 22.5
CC
81
Cov
95%
expand_table_rows
16.0
table_widths
15.0
fit_table_rows
14.0
CRAP 9.5
« lib/App/ansicolumn.pm ↑ Summary blib/lib/App/ansicolumn/Command.pm »
1 package App::ansicolumn;
2  
3 253 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 253 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 253 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 253 ▶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 473 ▶sub table_widths {
Subroutine table_widths: called CC=15 SCAR=27.1
Pod undocumented
37 473 my($content, $budget, $overhead, $fixed, $floor) = @_;
38 473 my @target = @$content;
39 473 my @free;
40 473 for my $i (keys @target) {
41 1798 ▶ if ($fixed and defined $fixed->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixed and defined $fixed->[$i]) { } 161 1637 1798
Condition100%3/3
$fixed and defined $fixed->[$i]
!ll&&!rl&&r
321605161
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 161 $target[$i] = $fixed->[$i];
43 } else {
44 1637 push @free, $i;
45 }
46 }
47 473 ▶ return \@target unless defined $budget and @free;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $budget and @free 16 457 473
Condition100%3/3
defined $budget and @free
!ll&&!rl&&r
142457
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 457 my $remain = $budget;
49 $remain -= $target[$_] + $overhead
50 457 ▶ for grep { $fixed and defined $fixed->[$_] } keys @target;
Condition100%3/3
$fixed && defined $fixed->[$_]
!ll&&!rl&&r
301598140
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 17660 ▶ my $cost = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 17660 my $level = shift;
53 17660 sum map { min($content->[$_], $level) + $overhead } @free;
54 457 };
55 457 ▶ return \@target if $cost->(max @$content) <= $remain;
Branch: 1 branch
BranchTrueFalseTotal
if &$cost(&max(@$content)) <= $remain 45 412 457
56 412 my $level = $floor;
57 412 ▶ for (my $l = max(@{$content}[@free]); $l > $floor; $l--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $floor) 16791 4 16795
58 16791 ▶ if ($cost->($l) <= $remain) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$cost($l) <= $remain) 408 16383 16791
59 408 $level = $l;
60 408 last;
61 }
62 }
63 412 $target[$_] = min($content->[$_], $level) for @free;
64 412 my $left = $remain - $cost->($level);
65 412 ▶ if ($left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 201 211 412
66 845 ▶ my @wrapped = sort { $content->[$b] <=> $content->[$a] or $a <=> $b }
Condition66%2/3
$content->[$b] <=> $content->[$a] || $a <=> $b
l!l&&r!l&&!r
84230
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 201 grep { $content->[$_] > $level } @free;
68 201 $target[$_]++ for @wrapped[0 .. min($left, scalar @wrapped) - 1];
69 }
70 412 \@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 253 ▶use Text::ANSI::Fold qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 253 ▶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 3583 ▶sub table_lb {
Subroutine table_lb: called CC=2 SCAR=7.2
Pod undocumented
127 3583 my $obj = shift;
128 3583 ▶ $obj->linebreak ? $obj->lb_flag : LINEBREAK_ALL;
Branch: 1 branch
BranchTrueFalseTotal
$obj->linebreak ? : 0 3583 3583
129 }
130  
131 3189 ▶sub table_margin {
Subroutine table_margin: called CC=2 SCAR=7.2
Pod undocumented
132 3189 my($obj, $width) = @_;
133 3189 ▶ ($obj->table_lb & LINEBREAK_RUNIN) ? max(0, min($obj->runin, $width - 1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$obj->table_lb & 1 ? : 3189 0 3189
134 }
135  
136 394 ▶sub table_fold {
Subroutine table_fold: called CC=3 SCAR=11.2
Pod undocumented
137 394 my($obj, $width) = @_;
138 394 my $lb = $obj->table_lb;
139 394 my $margin = $obj->table_margin($width);
140 394 ▶ my $fold = Text::ANSI::Fold->new(
Condition50%1/2
$obj->boundary || 'word'
l!l
0394
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 394 ▶ $fold->configure(discard => [ 'EL' ]) if $obj->discard_el;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->discard_el 394 0 394
152 394 $fold;
153 }
154  
155 #
156 # cell_lines($cell)
157 #
158 # The lines of a table cell: a LINE SEPARATOR (U+2028) in a cell
159 # breaks it into lines. An empty cell has one empty line. The cell
160 # is divided by Text::ANSI::Fold, with no width, which ends a line at
161 # the separator: colors and links that go on over it are closed at
162 # the end of the line and opened again on the next.
163 #
164 7696 ▶sub cell_lines {
Subroutine cell_lines: called CC=4 SCAR=13.9
Pod undocumented
165 7696 my $cell = shift;
166 7696 ▶ return ($cell) if not defined $cell or $cell !~ /\x{2028}/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell and $cell =~ /\x{2028}/u 7662 34 7696
Condition100%3/3
defined $cell and $cell =~ /\x{2028}/u
!ll&&!rl&&r
18764434
MC/DC100%2/2
defined $cell and $cell =~ /\x{2028}/u
defined $cell $cell =~ /\x{2028}/u
Truth table100%3/3
A and B
A= defined $cellB= $cell =~ /\x{2028}/u
ABresult
0X0
100
111
167 34 state $fold = Text::ANSI::Fold->new(width => -1);
168 34 my @lines = map { s/\x{2028}\z//r } $fold->text($cell)->chops;
169 # a separator at the end leaves an empty last line
170 34 ▶ push @lines, '' if $cell =~ /\x{2028}(?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cell =~ /\x{2028}(?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*\z/u 2 32 34
171 34 @lines;
172 }
173  
174 #
175 # cell_width($cell)
176 #
177 # The width of a table cell: the width of its widest line.
178 #
179 1193 ▶sub cell_width {
Subroutine cell_width: called CC=2 SCAR=6.9
Pod undocumented
180 1193 ▶ max map { ansi_width($_ // '') } cell_lines(shift);
Condition100%2/2
$_ // ""
l!l
12034
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
181 }
182  
183 #
184 # expand_table_rows(\@rows, \@rw, \@target, \@content, \@continued, \@ow)
185 #
186 # Wrap cells wider than their target width and expand each logical
187 # row into as many physical rows as its tallest cell. A cell which
188 # has LINE SEPARATORs is broken into its lines first, and each line
189 # is wrapped on its own. Missing cell lines are filled with empty
190 # strings. Ragged rows stay ragged.
191 # \@rw holds the width of every cell of \@rows (of its widest line),
192 # and \@ow is set to the width of every cell of the rows returned, so
193 # that no cell is measured twice.
194 # When \@continued is given, $continued[row][col] is set for every
195 # cell that is the second or later line of a cell: 1 for a line of
196 # a wrapped cell, 2 for a line which starts after a LINE SEPARATOR.
197 # With an empty \@target, cells are only broken at LINE SEPARATORs.
198 #
199 # In a column that has a cell wider than the target (its widest cell,
200 # $content[col], is wider), every cell wider than the target minus the
201 # run-in margin is wrapped, as lines are in panes, so that a cell
202 # fitting in the margin is not left longer than the wrapped lines.
203 #
204 #
205 # cell_limit($width, $content): the width over which a cell is wrapped
206 # in a column of the target width whose widest cell is $content wide.
207 #
208 2213 ▶sub cell_limit {
Subroutine cell_limit: called CC=2 SCAR=6.9
Pod undocumented
209 2213 my($obj, $w, $content) = @_;
210 2213 ▶ $content > $w ? $w - $obj->table_margin($w) : $w;
Branch: 1 branch
BranchTrueFalseTotal
$content > $w ? : 1649 564 2213
211 }
212  
213 #
214 # wrap_cell_line($line, $width): a line of a cell wrapped at the width,
215 # as [ \@chops, \@widths ]. Trailing spaces are removed from the
216 # chops, and so is their width. fit_table_rows wraps the same cells at
217 # the same widths again and again, so the result is kept by the object.
218 #
219 4270 ▶sub wrap_cell_line {
Subroutine wrap_cell_line: called CC=7 SCAR=19.7
Pod undocumented
220 4270 my($obj, $line, $w) = @_;
221 4270 ▶ my $cache = $obj->{_cell_fold}{$w} //= { fold => $obj->table_fold($w), line => {} };
Condition100%2/2
$obj->{"_cell_fold"}{$w} //= {"fold", $obj->table_fold($w), "line", {}}
l!l
3876394
MC/DC100%1/1
$obj->{"_cell_fold"}{$w} //= {"fold", $obj->table_fold($w), "line", {}}
$obj->{"_cell_fold"}{$w}
Truth table100%2/2
A
A= $obj->{"_cell_fold"}{$w}
Aresult
11
00
222 4270 ▶ $cache->{line}{$line} //= do {
Condition66%2/3
$cache->{"line"}{$line} //= do { my(@c) = $cache->{"fold"}->text($line)->chops("length", \my @l); foreach my $k (keys @c) { $c[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/u or next; my $sp = $1; $l[$k] = $sp =~ /^ +$/u ? $l[$k] - length($sp) : ansi_width($c[$k]); } ; @c ? [\@c, \@l] : [[""], [0]] }
l!l&&r!l&&!r
225020200
MC/DC0%0/2
$cache->{"line"}{$line} //= do { my(@c) = $cache->{"fold"}->text($line)->chops("length", \my @l); foreach my $k (keys @c) { $c[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/u or next; my $sp = $1; $l[$k] = $sp =~ /^ +$/u ? $l[$k] - length($sp) : ansi_width($c[$k]); } ; @c ? [\@c, \@l] : [[""], [0]] }
$cache->{"line"}{$line} do { my(@c) = $cache->{"fold"}->text($line)->chops("length", \my @l); foreach my $k (keys @c) { $c[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/u or next; my $sp = $1; $l[$k] = $sp =~ /^ +$/u ? $l[$k] - length($sp) : ansi_width($c[$k]); } ; @c ? [\@c, \@l] : [[""], [0]] }
Truth table66%2/3
A //= B
A= $cache->{"line"}{$line}B= do { my(@c) = $cache->{"fold"}->text($line)->chops("length", \my @l); foreach my $k (keys @c) { $c[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/u or next; my $sp = $1; $l[$k] = $sp =~ /^ +$/u ? $l[$k] - length($sp) : ansi_width($c[$k]); } ; @c ? [\@c, \@l] : [[""], [0]] }
ABresult
1X1
011
000
223 2020 my @c = $cache->{fold}->text($line)->chops(length => \my @l);
224 2020 for my $k (keys @c) {
225 8866 ▶ $c[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$c[$k] =~ s/(\h+)((?:\e\[[\d;]*m|\e\]8;;[^\e]*\e\\)*)$/$2/u or next 1709 7157 8866
226 1709 my $sp = $1;
227 1709 ▶ $l[$k] = $sp =~ /^ +$/ ? $l[$k] - length $sp : ansi_width($c[$k]);
Branch: 1 branch
BranchTrueFalseTotal
$sp =~ /^ +$/u ? : 1709 0 1709
228 }
229 2020 ▶ @c ? [ \@c, \@l ] : [ [ '' ], [ 0 ] ];
Branch: 1 branch
BranchTrueFalseTotal
@c ? : 2020 0 2020
230 };
231 }
232  
233 #
234 # column_width(\@rows, \@rw, $col, $width, $content): how wide the
235 # column comes out when it is wrapped at the width, as
236 # expand_table_rows does, without making the rows.
237 #
238 55 ▶sub column_width {
Subroutine column_width: called CC=5 SCAR=16.2
Pod undocumented
239 55 my($obj, $rows, $rw, $i, $w, $content) = @_;
240 55 my $limit = $obj->cell_limit($w, $content);
241 max 0, map {
242 55 my $r = $_;
243 150 my @lines = cell_lines($rows->[$r][$i]);
244 map {
245 150 ▶ my $lw = @lines > 1 ? ansi_width($_ // '') : $rw->[$r][$i] // 0;
Branch: 1 branch
BranchTrueFalseTotal
@lines > 1 ? : 4 148 152
Condition50%2/4
$_ // ""
l!l
40
$rw->[$r][$i] // 0
l!l
1480
MC/DC0%0/1
$_ // ""
$_
MC/DC0%0/1
$rw->[$r][$i] // 0
$rw->[$r][$i]
Truth table50%1/2
A
A= $_
Aresult
11
00
Truth table50%1/2
A
A= $rw->[$r][$i]
Aresult
11
00
246 152 ▶ $lw > $limit ? max(@{$obj->wrap_cell_line($_, $w)->[1]}) : $lw;
Branch: 1 branch
BranchTrueFalseTotal
$lw > $limit ? : 121 31 152
247 } @lines;
248 } keys @$rows;
249 }
250  
251 577 ▶sub expand_table_rows {
Subroutine expand_table_rows: called CC=16 SCAR=27.7
Pod undocumented
252 577 my($obj, $rows, $rw, $target, $content, $continued, $ow) = @_;
253 577 ▶ @$continued = () if $continued;
Branch: 1 branch
BranchTrueFalseTotal
if $continued 117 460 577
254 577 @$ow = ();
255 my @limit = map {
256 577 my($i, $w) = ($_, $target->[$_]);
257 2158 ▶ defined $w ? $obj->cell_limit($w, $content->[$i]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $w ? : 2158 0 2158
258 } keys @$target;
259 577 my @out;
260 577 for my $r (keys @$rows) {
261 1641 my $row = $rows->[$r];
262 1641 my @widths;
263 my @kinds;
264 my @cells = map {
265 1641 my($cell, $w, $cw) = ($row->[$_], $target->[$_], $rw->[$r][$_]);
266 6353 my @lines = cell_lines($cell);
267 6353 my(@chops, @length, @kind);
268 6353 for my $n (keys @lines) {
269 6375 my $line = $lines[$n];
270 6375 ▶ my $lw = @lines > 1 ? ansi_width($line // '') : $cw;
Branch: 1 branch
BranchTrueFalseTotal
@lines > 1 ? : 42 6333 6375
Condition50%1/2
$line // ""
l!l
420
MC/DC0%0/1
$line // ""
$line
Truth table50%1/2
A
A= $line
Aresult
11
00
271 6375 my(@c, @l);
272 6375 ▶ if (defined $w and $lw > $limit[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w and $lw > $limit[$_]) { } 4149 2226 6375
Condition100%3/3
defined $w and $lw > $limit[$_]
!ll&&!rl&&r
4921774149
MC/DC100%2/2
defined $w and $lw > $limit[$_]
defined $w $lw > $limit[$_]
Truth table100%3/3
A and B
A= defined $wB= $lw > $limit[$_]
ABresult
0X0
100
111
273 4149 my $wrapped = $obj->wrap_cell_line($line, $w);
274 4149 @c = @{$wrapped->[0]};
275 4149 @l = @{$wrapped->[1]};
276 } else {
277 2226 @c = ($line);
278 2226 @l = ($lw);
279 }
280 6375 ▶ push @kind, ($n ? 2 : 0), (1) x $#c;
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 22 6353 6375
281 6375 push @chops, @c;
282 6375 push @length, @l;
283 }
284 6353 $widths[$_] = \@length;
285 6353 $kinds[$_] = \@kind;
286 6353 \@chops;
287 } keys @$row;
288 1641 my $height = max(1, map { int @$_ } @cells);
289 1641 for my $k (0 .. $height - 1) {
290 6787 ▶ push @out, [ map { $_->[$k] // '' } @cells ];
Condition100%2/2
$_->[$k] // ""
l!l
1866511534
MC/DC100%1/1
$_->[$k] // ""
$_->[$k]
Truth table100%2/2
A
A= $_->[$k]
Aresult
11
00
291 6787 ▶ push @$ow, [ map { $_->[$k] // 0 } @widths ];
Condition100%2/2
$_->[$k] // 0
l!l
1867911520
MC/DC100%1/1
$_->[$k] // 0
$_->[$k]
Truth table100%2/2
A
A= $_->[$k]
Aresult
11
00
292 6787 ▶ next unless $continued and $k;
Branch: 1 branch
BranchTrueFalseTotal
unless $continued and $k 5699 1088 6787
Condition100%3/3
$continued and $k
!ll&&!rl&&r
53823171088
MC/DC100%2/2
$continued and $k
$continued $k
Truth table100%3/3
A and B
A= $continuedB= $k
ABresult
0X0
100
111
293 1088 $continued->[$#out][$_] = $kinds[$_][$k] for grep { defined $cells[$_][$k] } keys @cells;
294 }
295 }
296 577 @out;
297 }
298  
299 #
300 # fit_table_rows(\@rows, \@rw, \@content, $budget, $overhead, \@fixed, $floor, \@continued, \@ow)
301 #
302 # Wrap the table into the budget. Each round:
303 #
304 # 1. table_widths() decides the width of every column.
305 # 2. expand_table_rows() wraps the cells to those widths.
306 # 3. Measure how wide each wrapped column actually came out.
307 # 4. A wrapped column that left two or more columns of its width
308 # unused (its lines broke at word or kinsoku boundaries before
309 # the limit) is fixed at the measured width. Its wrapped lines
310 # are kept as they are.
311 # 5. If any column was fixed, go back to 1: the fixed column now
312 # costs only what it uses, so the remaining wrapped columns get
313 # more. Otherwise stop.
314 #
315 # One column short is not "unused": a double-width character cannot
316 # fill an odd width. Each round fixes at least one more column or
317 # stops, so the loop ends within (number of columns + 1) rounds.
318 #
319 # Returns the expanded rows. \@rw, \@continued and \@ow are passed
320 # to expand_table_rows(); \@continued and \@ow describe the rows
321 # returned.
322 #
323 # The widths are decided on the target widths, but a wrapped column
324 # often comes out narrower than its target: the run-in margin is kept
325 # free in every wrapped column and is used only when a run-in
326 # character goes there, and lines break at word boundaries before the
327 # limit. So the table is fit again with the width left unused added
328 # to the budget, as long as the result still fits the real budget and
329 # comes out wider.
330 #
331 # A larger budget raises every wrapped column at once, and the table
332 # can come out several columns wider by one more column of budget,
333 # over the budget. What is still left is then given to the wrapped
334 # columns one by one, widest content first: a column is wrapped at a
335 # width larger by one, two, and so on up to what is left, the first
336 # that makes it wider within the budget is taken, and so again until
337 # none does. How wide a column comes out depends on its own width
338 # only, so a column is measured alone, and a column that cannot be
339 # widened cannot be later, with less left.
340 #
341 # Only the widths are looked for; the rows are made once at the end.
342 #
343 108 ▶sub fit_table_rows {
Subroutine fit_table_rows: called CC=14 SCAR=26.4
Pod undocumented
344 108 my($obj, $rows, $rw, $content, $budget, $overhead, $fixed, $floor, $continued, $ow) = @_;
345 108 ▶ my $is_fixed = sub { $fixed and defined $fixed->[$_[0]] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$fixed and defined $$fixed[$_[0]] 31 1659 1690
346 # the width of the table, from the widths of the columns
347 348 ▶ my $used = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
348 348 my $cw = shift;
349 348 ▶ sum map { ($is_fixed->($_) ? $fixed->[$_] : $cw->[$_]) + $overhead } keys @$content;
Branch: 1 branch
BranchTrueFalseTotal
&$is_fixed($_) ? : 20 1292 1312
350 108 };
351 # fit into a budget: the widths wrapped at, and those come out
352 348 ▶ my $fit = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
353 348 my $b = shift;
354 348 my(@w, @t);
355 348 $obj->fit_table_rounds($rows, $rw, $content, $b, $overhead, $fixed, $floor, undef, \@w, \@t);
356 348 ▶ my @cw = map { my $i = $_; max 0, map { $_->[$i] // 0 } @w } keys @$content;
Condition50%1/2
$_->[$i] // 0
l!l
196080
MC/DC0%0/1
$_->[$i] // 0
$_->[$i]
Truth table50%1/2
A
A= $_->[$i]
Aresult
11
00
357 348 { target => \@t, cw => \@cw, used => $used->(\@cw) };
358 108 };
359 108 my $best = $fit->($budget);
360 108 ▶ if (defined $budget) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $budget) 96 12 108
361 96 my $try = $budget;
362 # each round makes the table wider within the budget, or stops
363 96 ▶ while ((my $left = $budget - $best->{used}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $left = $budget - $best->{"used"}) > 0) 140 54 194
364 140 my $next;
365 140 ▶ for (my $t = $try + $left; $t > $try; $t--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t > $try) 240 31 271
366 240 my $r = $fit->($t);
367 240 ▶ next if $r->{used} > $budget;
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"used"} > $budget 131 109 240
368 109 ▶ ($next, $try) = ($r, $t) if $r->{used} > $best->{used};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"used"} > $best->{"used"} 98 11 109
369 109 last;
370 }
371 140 ▶ last unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 42 98 140
372 98 $best = $next;
373 }
374 96 my($t, $cw, $u) = @{$best}{qw(target cw used)};
375 96 ▶ for my $i (sort { $content->[$b] <=> $content->[$a] or $a <=> $b }
Condition66%2/3
$content->[$b] <=> $content->[$a] || $a <=> $b
l!l&&r!l&&!r
39850
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
376 378 grep { not $is_fixed->($_) } keys @$content) {
377 WIDEN:
378 376 ▶ while ((my $left = $budget - $u) > 0 and $content->[$i] > $t->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $left = $budget - $u) > 0 and $content->[$i] > $t->[$i]) 46 355 401
Condition100%3/3
(my $left = $budget - $u) > 0 and $content->[$i] > $t->[$i]
!ll&&!rl&&r
3243146
MC/DC100%2/2
(my $left = $budget - $u) > 0 and $content->[$i] > $t->[$i]
(my $left = $budget - $u) > 0 $content->[$i] > $t->[$i]
Truth table100%3/3
A and B
A= (my $left = $budget - $u) > 0B= $content->[$i] > $t->[$i]
ABresult
0X0
100
111
379 46 for my $d (1 .. $left) {
380 55 my $m = $obj->column_width($rows, $rw, $i, $t->[$i] + $d, $content->[$i]);
381 55 ▶ next if $m <= $cw->[$i] or $u + $m - $cw->[$i] > $budget;
Branch: 1 branch
BranchTrueFalseTotal
if $m <= $cw->[$i] or $u + $m - $cw->[$i] > $budget 30 25 55
Condition100%3/3
$m <= $cw->[$i] or $u + $m - $cw->[$i] > $budget
l!l&&r!l&&!r
102025
MC/DC100%2/2
$m <= $cw->[$i] or $u + $m - $cw->[$i] > $budget
$m <= $cw->[$i] $u + $m - $cw->[$i] > $budget
Truth table100%3/3
A or B
A= $m <= $cw->[$i]B= $u + $m - $cw->[$i] > $budget
ABresult
1X1
011
000
382 25 $u += $m - $cw->[$i];
383 25 $cw->[$i] = $m;
384 25 $t->[$i] += $d;
385 25 next WIDEN;
386 }
387 21 last;
388 }
389 }
390 }
391 108 $obj->expand_table_rows($rows, $rw, $best->{target}, $content, $continued, $ow);
392 }
393  
394 #
395 # fit_table_rounds(..., \@width): the rounds of fit_table_rows
396 # described above, for one budget. Arguments are those of
397 # fit_table_rows, and \@width is set to the widths the columns were
398 # wrapped at.
399 #
400 348 ▶sub fit_table_rounds {
Subroutine fit_table_rounds: called CC=10 SCAR=23.1
Pod undocumented
401 348 my($obj, $rows, $rw, $content, $budget, $overhead, $fixed, $floor, $continued, $ow, $wout) = @_;
402 348 ▶ my @fix = $fixed ? @$fixed : ();
Branch: 1 branch
BranchTrueFalseTotal
$fixed ? : 14 334 348
403 348 my @wrap; # width each column was wrapped at
404 my @out;
405 348 while (1) {
406 460 my $target = table_widths($content, $budget, $overhead, \@fix, $floor);
407 460 ▶ my @width = map { $wrap[$_] // $target->[$_] } keys @$content;
Condition66%2/3
$wrap[$_] // $target->[$_]
l!l&&r!l&&!r
13816200
MC/DC0%0/2
$wrap[$_] // $target->[$_]
$wrap[$_] $target->[$_]
Truth table66%2/3
A // B
A= $wrap[$_]B= $target->[$_]
ABresult
1X1
011
000
408 460 @out = $obj->expand_table_rows($rows, $rw, \@width, $content, $continued, $ow);
409 460 ▶ @$wout = @width if $wout;
Branch: 1 branch
BranchTrueFalseTotal
if $wout 460 0 460
410 460 my $changed = 0;
411 460 for my $i (keys @$content) {
412 1758 ▶ next if defined $fix[$i] or $content->[$i] <= $target->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $fix[$i] or $content->[$i] <= $target->[$i] 612 1146 1758
Condition100%3/3
defined $fix[$i] or $content->[$i] <= $target->[$i]
l!l&&r!l&&!r
1584541146
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
413 1146 my $usable = $target->[$i] - $obj->table_margin($target->[$i]);
414 1146 ▶ my $measured = max 0, map { $_->[$i] // 0 } @$ow;
Condition50%1/2
$_->[$i] // 0
l!l
174880
MC/DC0%0/1
$_->[$i] // 0
$_->[$i]
Truth table50%1/2
A
A= $_->[$i]
Aresult
11
00
415 1146 ▶ next if $measured >= $usable - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $measured >= $usable - 1 1032 114 1146
416 114 $fix[$i] = $measured;
417 114 $wrap[$i] = $target->[$i];
418 114 $changed++;
419 }
420 460 ▶ last unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 348 112 460
421 }
422 348 @out;
423 }
424  
425 253 1;
« lib/App/ansicolumn.pm ↑ Summary blib/lib/App/ansicolumn/Command.pm »