Table.pm

filter: Statementstmt 92.1% 152 / 165 Branchbran 81.4% 70 / 86 Conditioncond 71.6% 48 / 67 MC/DCmcdc 40.5% 15 / 37 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 5 totaltotal 79.6% 297 / 373 CC 76 SCAR 32.6
CC
76
Cov
85%
render
35.0
regen_columns
31.8
init
16.3
CRAP 26.1
↑ Summary Table/HashBase.pm »
1 package Term::Table;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.018';
6  
7 4 ▶use Term::Table::Cell();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Term::Table::Util qw/term_size uni_length USE_GCS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use List::Util qw/max sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Term::Table::HashBase qw/rows _columns collapse max_width mark_tail sanitize show_header auto_columns no_collapse header allow_overflow pad/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub BORDER_SIZE() { 4 } # '| ' and ' |' borders
17 sub DIV_SIZE() { 3 } # ' | ' column delimiter
18 sub CELL_PAD_SIZE() { 2 } # space on either side of the |
19  
20 sub init {
21 16 ▶ my $self = shift;
Subroutine init: called CC=16 SCAR=27.9
Pod undocumented
22  
23 croak "You cannot have a table with no rows"
24 16 ▶ unless $self->{+ROWS} && @{$self->{+ROWS}};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rows"} and @{$self->{"rows"};} 0 16 16
Condition33%1/3
$self->{"rows"} and @{$self->{"rows"};}
!ll&&!rl&&r
0016
MC/DC0%0/2
$self->{"rows"} and @{$self->{"rows"};}
$self->{"rows"} @{$self->{"rows"};}
Truth table33%1/3
A and B
A= $self->{"rows"}B= @{$self->{"rows"};}
ABresult
0X0
100
111
25  
26 16 ▶ $self->{+MAX_WIDTH} ||= term_size();
Condition66%2/3
$self->{"max_width"} ||= term_size()
l!l&&r!l&&!r
1510
MC/DC0%0/2
$self->{"max_width"} ||= term_size()
$self->{"max_width"} term_size()
Truth table66%2/3
A ||= B
A= $self->{"max_width"}B= term_size()
ABresult
1X1
011
000
27 16 ▶ $self->{+NO_COLLAPSE} ||= {};
Condition100%2/2
$self->{"no_collapse"} ||= {}
l!l
115
MC/DC100%1/1
$self->{"no_collapse"} ||= {}
$self->{"no_collapse"}
Truth table100%2/2
A
A= $self->{"no_collapse"}
Aresult
11
00
28 16 ▶ if (ref($self->{+NO_COLLAPSE}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"no_collapse"} eq "ARRAY") 1 15 16
29 1 $self->{+NO_COLLAPSE} = {map { ($_ => 1) } @{$self->{+NO_COLLAPSE}}};
30 }
31  
32 16 ▶ if ($self->{+NO_COLLAPSE} && $self->{+HEADER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_collapse"} and $self->{"header"}) 12 4 16
Condition66%2/3
$self->{"no_collapse"} and $self->{"header"}
!ll&&!rl&&r
0412
MC/DC50%1/2
$self->{"no_collapse"} and $self->{"header"}
$self->{"no_collapse"} $self->{"header"}
Truth table66%2/3
A and B
A= $self->{"no_collapse"}B= $self->{"header"}
ABresult
0X0
100
111
33 12 my $header = $self->{+HEADER};
34 12 ▶ for(my $idx = 0; $idx < @$header; $idx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @$header) 45 12 57
35 45 ▶ $self->{+NO_COLLAPSE}->{$idx} ||= $self->{+NO_COLLAPSE}->{$header->[$idx]};
Condition66%2/3
$self->{"no_collapse"}{$idx} ||= $self->{"no_collapse"}{$header->[$idx]}
l!l&&r!l&&!r
0243
MC/DC50%1/2
$self->{"no_collapse"}{$idx} ||= $self->{"no_collapse"}{$header->[$idx]}
$self->{"no_collapse"}{$idx} $self->{"no_collapse"}{$header->[$idx]}
Truth table66%2/3
A ||= B
A= $self->{"no_collapse"}{$idx}B= $self->{"no_collapse"}{$header->[$idx]}
ABresult
1X1
011
000
36 }
37 }
38  
39 16 ▶ $self->{+PAD} = 4 unless defined $self->{+PAD};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pad"} 15 1 16
40  
41 16 ▶ $self->{+COLLAPSE} = 1 unless defined $self->{+COLLAPSE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"collapse"} 9 7 16
42 16 ▶ $self->{+SANITIZE} = 1 unless defined $self->{+SANITIZE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"sanitize"} 14 2 16
43 16 ▶ $self->{+MARK_TAIL} = 1 unless defined $self->{+MARK_TAIL};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"mark_tail"} 14 2 16
44  
45 16 ▶ if($self->{+HEADER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'header'}) { } 12 4 16
46 12 ▶ $self->{+SHOW_HEADER} = 1 unless defined $self->{+SHOW_HEADER};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"show_header"} 11 1 12
47 }
48 else {
49 4 $self->{+HEADER} = [];
50 4 $self->{+AUTO_COLUMNS} = 1;
51 4 $self->{+SHOW_HEADER} = 0;
52 }
53 }
54  
55 sub columns {
56 17 ▶ my $self = shift;
Subroutine columns: called CC=2 SCAR=6.9
Pod undocumented
57  
58 17 ▶ $self->regen_columns unless $self->{+_COLUMNS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_columns"} 16 1 17
59  
60 15 return $self->{+_COLUMNS};
61 }
62  
63 sub regen_columns {
64 16 ▶ my $self = shift;
Subroutine regen_columns: called CC=29 SCAR=34.6
Pod undocumented
65  
66 16 ▶ my $has_header = $self->{+SHOW_HEADER} && @{$self->{+HEADER}};
Condition66%2/3
$self->{'show_header'} && @{$self->{'header'};}
!ll&&!rl&&r
4012
MC/DC50%1/2
$self->{'show_header'} && @{$self->{'header'};}
$self->{'show_header'} @{$self->{'header'};}
Truth table66%2/3
A && B
A= $self->{'show_header'}B= @{$self->{'header'};}
ABresult
0X0
100
111
67 16 ▶ my %new_col = (width => 0, count => $has_header ? -1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$has_header ? : 12 4 16
68  
69 16 my $cols = [map { {%new_col} } @{$self->{+HEADER}}];
70 16 my @rows = @{$self->{+ROWS}};
71  
72 16 ▶ for my $row ($has_header ? ($self->{+HEADER}, @rows) : (@rows)) {
Branch: 1 branch
BranchTrueFalseTotal
$has_header ? : 12 4 16
73 38 for my $ci (0 .. max(@$cols - 1, @$row - 1)) {
74 137 ▶ $cols->[$ci] ||= {%new_col} if $self->{+AUTO_COLUMNS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_columns"} 11 126 137
Condition100%2/2
$cols->[$ci] ||= {%new_col}
l!l
47
MC/DC100%1/1
$cols->[$ci] ||= {%new_col}
$cols->[$ci]
Truth table100%2/2
A
A= $cols->[$ci]
Aresult
11
00
75 137 ▶ my $c = $cols->[$ci] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $c = $cols->[$ci] or next 0 137 137
76 137 ▶ $c->{idx} ||= $ci;
Condition100%3/3
$c->{"idx"} ||= $ci
l!l&&r!l&&!r
633638
MC/DC100%2/2
$c->{"idx"} ||= $ci
$c->{"idx"} $ci
Truth table100%3/3
A ||= B
A= $c->{"idx"}B= $ci
ABresult
1X1
011
000
77 137 ▶ $c->{rows} ||= [];
Condition100%2/2
$c->{"rows"} ||= []
l!l
8552
MC/DC100%1/1
$c->{"rows"} ||= []
$c->{"rows"}
Truth table100%2/2
A
A= $c->{"rows"}
Aresult
11
00
78  
79 137 my $r = $row->[$ci];
80 137 ▶ $r = Term::Table::Cell->new(value => $r)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($r) and $r->isa("Term::Table::Cell") || $r->isa("Term::Table::CellStack") || $r->isa("Term::Table::Spacer") 131 6 137
Condition55%5/9
builtin::blessed($r) and $r->isa("Term::Table::Cell") || $r->isa("Term::Table::CellStack") || $r->isa("Term::Table::Spacer")
!ll&&!rl&&r
13160
$r->isa("Term::Table::Cell") or $r->isa("Term::Table::CellStack") or $r->isa("Term::Table::Spacer")
l!l&&r!l&&!r
600
$r->isa("Term::Table::Cell") or $r->isa("Term::Table::CellStack")
l!l&&r!l&&!r
420
MC/DC0%0/4
builtin::blessed($r) and $r->isa("Term::Table::Cell") || $r->isa("Term::Table::CellStack") || $r->isa("Term::Table::Spacer")
builtin::blessed($r) $r->isa("Term::Table::Cell") $r->isa("Term::Table::CellStack") $r->isa("Term::Table::Spacer")
Truth table20%1/5
A and B or C or D
A= builtin::blessed($r)B= $r->isa("Term::Table::Cell")C= $r->isa("Term::Table::CellStack")D= $r->isa("Term::Table::Spacer")
ABCDresult
0XXX0
10000
11XX1
101X1
10011
81 unless blessed($r)
82 && ($r->isa('Term::Table::Cell')
83 || $r->isa('Term::Table::CellStack')
84 || $r->isa('Term::Table::Spacer'));
85  
86 137 ▶ $r->sanitize if $self->{+SANITIZE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sanitize"} 137 0 137
87 137 ▶ $r->mark_tail if $self->{+MARK_TAIL};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mark_tail"} 137 0 137
88  
89 137 my $rs = $r->width;
90 137 ▶ $c->{width} = $rs if $rs > $c->{width};
Branch: 1 branch
BranchTrueFalseTotal
if $rs > $c->{"width"} 87 50 137
91 137 ▶ $c->{count}++ if $rs;
Branch: 1 branch
BranchTrueFalseTotal
if $rs 125 12 137
92  
93 137 push @{$c->{rows}} => $r;
94 }
95 }
96  
97 # Remove any empty columns we can
98 45 ▶ @$cols = grep {$_->{count} > 0 || $self->{+NO_COLLAPSE}->{$_->{idx}}} @$cols
Branch: 1 branch
BranchTrueFalseTotal
$_->{"count"} > 0 or $self->{"no_collapse"}{$_->{"idx"}} 6 39 45
99 16 ▶ if $self->{+COLLAPSE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"collapse"} 12 4 16
100  
101 16 my $current = sum(map {$_->{width}} @$cols);
102 16 my $border = sum(BORDER_SIZE, $self->{+PAD}, DIV_SIZE * (@$cols - 1));
103 16 my $total = $current + $border;
104  
105 16 ▶ if ($total > $self->{+MAX_WIDTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > $self->{"max_width"}) 7 9 16
106 7 my $fair = ($self->{+MAX_WIDTH} - $border) / @$cols;
107 7 ▶ if ($fair < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fair < 1) 3 4 7
108 3 ▶ return $self->{+_COLUMNS} = $cols if $self->{+ALLOW_OVERFLOW};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_overflow"} 1 2 3
109 2 croak "Table is too large ($total including $self->{+PAD} padding) to fit into max-width ($self->{+MAX_WIDTH})";
110 }
111  
112 4 my $under = 0;
113 4 my @fix;
114 4 for my $c (@$cols) {
115 13 ▶ if ($c->{width} > $fair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'width'} > $fair) { } 13 0 13
116 13 push @fix => $c;
117 }
118 else {
119 0 $under += $c->{width};
120 }
121 }
122  
123 # Recalculate fairness
124 4 $fair = int(($self->{+MAX_WIDTH} - $border - $under) / @fix);
125 4 ▶ if ($fair < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fair < 1) 0 4 4
126 0 ▶ return $self->{+_COLUMNS} = $cols if $self->{+ALLOW_OVERFLOW};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_overflow"} 0 0 0
127 0 croak "Table is too large ($total including $self->{+PAD} padding) to fit into max-width ($self->{+MAX_WIDTH})";
128 }
129  
130 # Adjust over-long columns
131 4 $_->{width} = $fair for @fix;
132 }
133  
134 13 $self->{+_COLUMNS} = $cols;
135 }
136  
137 sub render {
138 17 ▶ my $self = shift;
Subroutine render: called CC=31 SCAR=35.6
Pod undocumented
139  
140 17 my $cols = $self->columns;
141 15 for my $col (@$cols) {
142 49 for my $cell (@{$col->{rows}}) {
143 132 $cell->reset;
144 }
145 }
146 15 my $width = sum(BORDER_SIZE, $self->{+PAD}, DIV_SIZE * @$cols, map { $_->{width} } @$cols);
147  
148 #<<< NO-TIDY
149 15 my $border = '+' . join('+', map { '-' x ($_->{width} + CELL_PAD_SIZE) } @$cols) . '+';
150 15 my $template = '|' . join('|', map { my $w = $_->{width} + CELL_PAD_SIZE; '%s' } @$cols) . '|';
151 15 my $spacer = '|' . join('|', map { ' ' x ($_->{width} + CELL_PAD_SIZE) } @$cols) . '|';
152 #>>>
153  
154 15 my @out = ($border);
155 15 my ($row, $split, $found) = (0, 0, 0);
156 15 while(1) {
157 149 my @row;
158  
159 149 my $is_spacer = 0;
160  
161 149 for my $col (@$cols) {
162 536 my $r = $col->{rows}->[$row];
163 536 ▶ unless($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 49 487 536
164 49 push @row => '';
165 49 next;
166 }
167  
168 487 my ($v, $vw);
169  
170 487 ▶ if ($r->isa('Term::Table::Cell')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($r->isa('Term::Table::Cell')) { } 449 38 487
elsif ($r->isa('Term::Table::CellStack')) { } 38 0 38
elsif ($r->isa('Term::Table::Spacer')) { } 0 0 0
171 449 my $lw = $r->border_left_width;
172 449 my $rw = $r->border_right_width;
173 449 $vw = $col->{width} - $lw - $rw;
174 449 $v = $r->break->next($vw);
175 }
176 elsif ($r->isa('Term::Table::CellStack')) {
177 38 ($v, $vw) = $r->break->next($col->{width});
178 }
179 elsif ($r->isa('Term::Table::Spacer')) {
180 0 $is_spacer = 1;
181 }
182  
183 487 ▶ if ($is_spacer) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_spacer) { } 0 487 487
elsif (defined $v) { } 249 238 487
184 0 last;
185 }
186 elsif (defined $v) {
187 249 $found++;
188 249 ▶ my $bcolor = $r->border_color || '';
Condition50%1/2
$r->border_color || ''
l!l
0249
MC/DC0%0/1
$r->border_color || ''
$r->border_color
Truth table50%1/2
A
A= $r->border_color
Aresult
11
00
189 249 ▶ my $vcolor = $r->value_color || '';
Condition50%1/2
$r->value_color || ''
l!l
0249
MC/DC0%0/1
$r->value_color || ''
$r->value_color
Truth table50%1/2
A
A= $r->value_color
Aresult
11
00
190 249 ▶ my $reset = $r->reset_color || '';
Condition50%1/2
$r->reset_color || ''
l!l
0249
MC/DC0%0/1
$r->reset_color || ''
$r->reset_color
Truth table50%1/2
A
A= $r->reset_color
Aresult
11
00
191  
192 249 ▶ if (my $need = $vw - uni_length($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $need = $vw - uni_length($v)) 0 249 249
193 0 $v .= ' ' x $need;
194 }
195  
196 249 my $rt = "${reset}${bcolor}\%s${reset} ${vcolor}\%s${reset} ${bcolor}\%s${reset}";
197 249 ▶ push @row => sprintf($rt, $r->border_left || '', $v, $r->border_right || '');
Condition100%4/4
$r->border_left || ''
l!l
16233
$r->border_right || ''
l!l
16233
MC/DC100%1/1
$r->border_left || ''
$r->border_left
MC/DC100%1/1
$r->border_right || ''
$r->border_right
Truth table100%2/2
A
A= $r->border_left
Aresult
11
00
Truth table100%2/2
A
A= $r->border_right
Aresult
11
00
198 }
199 else {
200 238 push @row => ' ' x ($col->{width} + 2);
201 }
202 }
203  
204 149 ▶ if (!grep {$_ && m/\S/} @row) {
Branch: 2 branches
BranchTrueFalseTotal
unless (grep {$_ and /\S/;} @row) 53 96 149
$_ and /\S/ 487 49 536
205 53 ▶ last unless $found || $is_spacer;
Branch: 1 branch
BranchTrueFalseTotal
unless $found or $is_spacer 15 38 53
Condition66%2/3
$found or $is_spacer
l!l&&r!l&&!r
38015
MC/DC50%1/2
$found or $is_spacer
$found $is_spacer
Truth table66%2/3
A or B
A= $foundB= $is_spacer
ABresult
1X1
011
000
206  
207 38 ▶ push @out => $border if $row == 0 && $self->{+SHOW_HEADER} && @{$self->{+HEADER}};
Branch: 1 branch
BranchTrueFalseTotal
if $row == 0 and $self->{"show_header"} and @{$self->{"header"};} 13 25 38
Condition83%5/6
$row == 0 and $self->{"show_header"} and @{$self->{"header"};}
!ll&&!rl&&r
25013
$row == 0 and $self->{"show_header"}
!ll&&!rl&&r
23213
MC/DC66%2/3
$row == 0 and $self->{"show_header"} and @{$self->{"header"};}
$row == 0 $self->{"show_header"} @{$self->{"header"};}
Truth table75%3/4
A and B and C
A= $row == 0B= $self->{"show_header"}C= @{$self->{"header"};}
ABCresult
0XX0
10X0
1100
1111
208 38 ▶ push @out => $spacer if $split > 1 || $is_spacer;
Branch: 1 branch
BranchTrueFalseTotal
if $split > 1 or $is_spacer 10 28 38
Condition66%2/3
$split > 1 or $is_spacer
l!l&&r!l&&!r
10028
MC/DC50%1/2
$split > 1 or $is_spacer
$split > 1 $is_spacer
Truth table66%2/3
A or B
A= $split > 1B= $is_spacer
ABresult
1X1
011
000
209  
210 38 $row++;
211 38 $split = 0;
212 38 $found = 0;
213  
214 38 next;
215 }
216  
217 96 ▶ if ($split == 1 && @out > 1 && $out[-2] ne $border && $out[-2] ne $spacer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split == 1 and @out > 1 and $out[-2] ne $border and $out[-2] ne $spacer) 0 96 96
Condition77%7/9
$split == 1 and @out > 1 and $out[-2] ne $border and $out[-2] ne $spacer
!ll&&!rl&&r
9240
$split == 1 and @out > 1 and $out[-2] ne $border
!ll&&!rl&&r
8664
$split == 1 and @out > 1
!ll&&!rl&&r
86010
MC/DC0%0/4
$split == 1 and @out > 1 and $out[-2] ne $border and $out[-2] ne $spacer
$split == 1 @out > 1 $out[-2] ne $border $out[-2] ne $spacer
Truth table60%3/5
A and B and C and D
A= $split == 1B= @out > 1C= $out[-2] ne $borderD= $out[-2] ne $spacer
ABCDresult
0XXX0
10XX0
110X0
11100
11111
218 0 my $last = pop @out;
219 0 push @out => ($spacer, $last);
220 }
221  
222 96 push @out => sprintf($template, @row);
223 96 $split++;
224 }
225  
226 15 ▶ pop @out while @out && $out[-1] eq $spacer;
Branch: 1 branch
BranchTrueFalseTotal
@out and $out[-1] eq $spacer and do { pop @out } 6 15 21
Condition66%2/3
@out and $out[-1] eq $spacer
!ll&&!rl&&r
0156
MC/DC50%1/2
@out and $out[-1] eq $spacer
@out $out[-1] eq $spacer
Truth table66%2/3
A and B
A= @outB= $out[-1] eq $spacer
ABresult
0X0
100
111
227  
228 15 unless (USE_GCS) {
229 for my $row (@out) {
230 next unless $row =~ m/[^\x00-\x7F]/;
231 unshift @out => "Unicode::GCString is not installed, table may not display all unicode characters properly";
232 last;
233 }
234 }
235  
236 15 return (@out, $border);
237 }
238  
239 sub display {
240 0 ▶ my $self = shift;
Subroutine display: not called CC=2 SCAR=17.9
Pod undocumented
241 0 my ($fh) = @_;
242  
243 0 my @parts = map "$_\n", $self->render;
244  
245 0 ▶ print $fh @parts if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
246 0 print @parts;
247 }
248  
249 4 1;
250  
251 __END__
↑ Summary Table/HashBase.pm »