blib/lib/Text/SimpleTable/AutoWidth.pm

filter: Statementstmt 97.1% 69 / 71 Branchbran 92.3% 24 / 26 Conditioncond 54.5% 6 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 87.3% 110 / 126 CC 23 SCAR 27.5
CC
23
Cov
92%
draw
22.8
row
2.0
__ANON__
1.1
CRAP 15.6
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Text::SimpleTable::AutoWidth;
5 2 $Text::SimpleTable::AutoWidth::VERSION = '0.09';
6 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Text::SimpleTable::AutoWidth - Simple eyecandy ASCII tables with auto-width selection
9  
10  
11 2 has 'fixed_width' => ( is => 'rw', default => 0 ); # isa => 'Int'
12 2 has 'max_width' => ( is => 'rw', default => 0 ); # isa => 'Int'
13  
14 2 has 'captions' => ( is => 'rw' ); # isa => 'ArrayRef[Str]'
15 2 has 'rows' => ( is => 'rw' ); # isa => 'ArrayRef[ArrayRef[Str]]'
16  
17 2 our $WIDTH_LIMIT = 200; # default maximum width
18  
19  
20 6 ▶sub row {
Subroutine row: called CC=2 SCAR=6.9
Pod documented
21 6 my ( $self, @texts ) = @_;
22  
23 6 ▶ if ( $self->rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rows) { } 2 4 6
24 2 push( @{ $self->rows }, [@texts] );
25 }
26 else {
27 4 $self->rows( [ [@texts] ] );
28 }
29  
30 6 return $self;
31 }
32  
33  
34 2 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Text::SimpleTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 4 ▶sub draw {
Subroutine draw: called CC=22 SCAR=31.3
Pod documented
38 4 my $self = shift;
39  
40 # count of columns will be same as count of captions, or same
41 # as count of columns in first row, if there is no captions
42 my $columns =
43 1 ▶ ( $self->captions && @{ $self->captions } )
Condition33%1/3
$self->rows and @{$self->rows->[0];}
!ll&&!rl&&r
003
MC/DC0%0/2
$self->rows and @{$self->rows->[0];}
$self->rows @{$self->rows->[0];}
Truth table33%1/3
A and B
A= $self->rowsB= @{$self->rows->[0];}
ABresult
0X0
100
111
44 4 ▶ || ( $self->rows && @{ $self->rows->[0] } )
Condition62%5/8
$self->captions && @{$self->captions;} || $self->rows && @{$self->rows->[0];} || 0
l!l
40
$self->captions and @{$self->captions;} or $self->rows and @{$self->rows->[0];}
l!l&&r!l&&!r
130
$self->captions and @{$self->captions;}
!ll&&!rl&&r
301
MC/DC0%0/3
$self->captions && @{$self->captions;} || $self->rows && @{$self->rows->[0];} || 0
$self->captions @{$self->captions;} $self->rows and @{$self->rows->[0];}
Truth table40%2/5
A and B or C
A= $self->captionsB= @{$self->captions;}C= $self->rows and @{$self->rows->[0];}
ABCresult
11X1
0X11
1011
0X00
1000
45 || 0;
46  
47 4 ▶ return unless $columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $columns 0 4 4
48  
49 # table will not be wider than limits
50 4 ▶ my $limit =
Branch: 2 branches
BranchTrueFalseTotal
$self->max_width ? : 1 3 4
$self->fixed_width ? : 1 2 3
51 $self->max_width ? $self->max_width
52 : $self->fixed_width ? $self->fixed_width
53 : $WIDTH_LIMIT;
54  
55 # by default, each column should have at least 2 symbols:
56 # one informative and one for '-' (if we'll need to wrap)
57 4 my @max_width = (2) x $columns;
58  
59 # calculate max width of each column
60 4 ▶ for my $row ( ( $self->captions ? $self->captions : () ), @{ $self->rows } ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->captions ? : 1 3 4
61 7 my @row_width = map { length } @$row;
62 7 ▶ $#row_width = $columns - 1 if $#row_width >= $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $#row_width >= $columns 1 6 7
63  
64 # find new width
65 # we will do this in two passes
66 7 my @new_width = @max_width;
67  
68 # first pass:
69 # find new width for all columns, that we can
70 # make wider without need to wrap anything
71 7 for my $idx ( 0 .. $#row_width ) {
72 14 ▶ if ( $max_width[$idx] < $row_width[$idx] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_width[$idx] < $row_width[$idx]) 12 2 14
73 12 $new_width[$idx] = $row_width[$idx];
74  
75 # check for limits
76 my $total = $columns + 1 # for each '|'
77 + $columns * 2 # for spaces around each value
78 12 ▶ + List::Util::reduce { $a + $b } @new_width;
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 # restore old value, if new value will lead to wrap
81 12 ▶ $new_width[$idx] = $max_width[$idx]
Branch: 1 branch
BranchTrueFalseTotal
if $total > $limit 1 11 12
82 if $total > $limit;
83 }
84 }
85  
86 # second pass:
87 # find new width for all columns, that we can
88 # make wider and need to wrap something
89 7 for my $idx ( 0 .. $#row_width ) {
90 14 ▶ if ( $new_width[$idx] < $row_width[$idx] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_width[$idx] < $row_width[$idx]) 1 13 14
91 my $total = $columns + 1 # for each '|'
92 + $columns * 2 # for spaces around each value
93 0 ▶ + List::Util::reduce { $a + $b } @new_width;
Subroutine __ANON__: not called CC=1 SCAR=1.2
94  
95 1 $new_width[$idx] += $limit - $total;
96 1 last;
97 }
98 }
99  
100 # save new result
101 7 @max_width = @new_width;
102  
103 # check for limits
104 my $total = $columns + 1 # for each '|'
105 + $columns * 2 # for spaces around each value
106 7 ▶ + List::Util::reduce { $a + $b } @max_width;
Subroutine __ANON__: called CC=1 SCAR=0.0
107  
108 7 ▶ last if $total >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $total >= $limit 1 6 7
109 }
110  
111 # check for fixed_width
112 4 ▶ if ( $self->fixed_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fixed_width) 1 3 4
113 my $total = $columns + 1 # for each '|'
114 + $columns * 2 # for spaces around each value
115 0 ▶ + List::Util::reduce { $a + $b } @max_width;
Subroutine __ANON__: not called CC=1 SCAR=1.2
116  
117 1 ▶ $max_width[-1] += $self->fixed_width - $total
Branch: 1 branch
BranchTrueFalseTotal
unless $total == $self->fixed_width 1 0 1
118 unless $total == $self->fixed_width;
119 }
120  
121 # prepare drawer
122 4 my @params = @max_width;
123  
124 4 ▶ if ( $self->captions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->captions) 1 3 4
125 1 my $idx = 0;
126 1 for (@params) {
127 3 $_ = [ $_, $self->captions->[ $idx++ ] ];
128 }
129 }
130 4 my $tab = Text::SimpleTable->new(@params);
131  
132 # put rows into drawer
133 4 $tab->row(@$_) for @{ $self->rows };
134  
135 4 return $tab->draw();
136 }
137  
138 2 __PACKAGE__->meta->make_immutable();
139  
140  
141 2 1; # End of Text::SimpleTable::AutoWidth
142  
143 __END__
↑ Summary