blib/lib/Text/TabularDisplay.pm

filter: Statementstmt 90.9% 110 / 121 Branchbran 82.1% 46 / 56 Conditioncond 70.0% 14 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 93.3% 14 / 15 Podpod 88.8% 8 / 9 totaltotal 84.9% 198 / 233 CC 41 SCAR 18.2
CC
41
Cov
86%
paginate
20.0
_format_line
8.0
render
7.0
CRAP 6.2
↑ Summary
1 package Text::TabularDisplay;
2  
3 # -------------------------------------------------------------------
4 # Text::TabularDisplay - Display text in formatted table output
5 # Copyright (C) 2004-2014 darren chamberlain <darren@cpan.org>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; version 2.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301 USA
20 # -------------------------------------------------------------------
21  
22 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 17 $VERSION = "1.38";
27  
28 # ---======================= Public Methods ======================---
29  
30 # -------------------------------------------------------------------
31 # new([@columns])
32 #
33 # Instantiate a new Text::TabularDisplay instance. Optionally takes
34 # column names, which are passed to the columns method.
35 # -------------------------------------------------------------------
36 18 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
37 18 my $class = shift;
38 18 ▶ return $class->clone
Branch: 1 branch
BranchTrueFalseTotal
if ref $class and UNIVERSAL::isa($class, __PACKAGE__) 0 18 18
Condition33%1/3
ref $class and UNIVERSAL::isa($class, __PACKAGE__)
!ll&&!rl&&r
1800
MC/DC0%0/2
ref $class and UNIVERSAL::isa($class, __PACKAGE__)
ref $class UNIVERSAL::isa($class, __PACKAGE__)
Truth table33%1/3
A and B
A= ref $classB= UNIVERSAL::isa($class, __PACKAGE__)
ABresult
0X0
100
111
39 if ref $class && UNIVERSAL::isa($class, __PACKAGE__);
40  
41 18 my $self = bless {
42 _COLUMNS => [ ],
43 _DATA => [ ],
44 _LENGTHS => [ ],
45 _SIZE => 0,
46 } => $class;
47  
48 18 ▶ $self->columns(@_) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 9 18
49  
50 18 return $self;
51 }
52  
53 # -------------------------------------------------------------------
54 # clone
55 #
56 # Clones a Text::TabluarDisplay instance.
57 # -------------------------------------------------------------------
58 1 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
59 1 my $self = shift;
60 1 ▶ my $class = ref $self || return $self->new;
Condition50%1/2
ref $self || (return $self->new)
l!l
10
MC/DC0%0/1
ref $self || (return $self->new)
ref $self
Truth table50%1/2
A
A= ref $self
Aresult
11
00
61 1 my $clone = $class->new($self->columns);
62  
63 1 for (@{ $self->{ _DATA } }) {
64 3 $clone->add(@$_);
65 }
66  
67 1 return $clone;
68 }
69  
70 # -------------------------------------------------------------------
71 # columns([@columns])
72 #
73 # Returns a list of column names in list context, but returns the
74 # number of columns in scalar context.
75 # -------------------------------------------------------------------
76 54 ▶sub columns {
Subroutine columns: called CC=6 SCAR=17.9
Pod documented
77 54 my $self = shift;
78 54 my @columns;
79  
80 54 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 21 33 54
81 21 my $cnum = $self->{ _SIZE };
82 21 ▶ if ($cnum > scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnum > scalar @_) 1 20 21
83 1 ▶ push @_, ""
Branch: 1 branch
BranchTrueFalseTotal
$self->columns > scalar @_ and do { push @_, "" } 1 1 2
84 while ($self->columns > scalar @_);
85 }
86  
87 21 @{ $self->{ _COLUMNS } } = ();
88 21 $self->_add($self->{ _COLUMNS }, $self->{ _LENGTHS }, \$self->{ _SIZE }, [ @_ ]);
89 }
90 54 ▶ @columns = @{ $self->{ _COLUMNS }->[0] || [ ]};
Condition100%2/2
$self->{'_COLUMNS'}[0] || []
l!l
504
MC/DC100%1/1
$self->{'_COLUMNS'}[0] || []
$self->{'_COLUMNS'}[0]
Truth table100%2/2
A
A= $self->{'_COLUMNS'}[0]
Aresult
11
00
91  
92 54 ▶ return wantarray ? @columns : scalar @columns;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 27 27 54
93 }
94  
95 # -------------------------------------------------------------------
96 # add(@data)
97 #
98 # Adds a row to the instance. Returns $self, for chaining:
99 # $self->add(@one)->add(@two)->add(@three);
100 # -------------------------------------------------------------------
101 82 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
102 82 my $self = shift;
103 82 ▶ my $add = UNIVERSAL::isa($_[0], 'ARRAY') ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'ARRAY') ? : 40 42 82
104  
105 82 ▶ if (@$add) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$add) 82 0 82
106 82 $self->_add($self->{ _DATA }, $self->{ _LENGTHS }, \$self->{ _SIZE }, $add);
107 }
108  
109 82 return $self;
110 }
111  
112 # -------------------------------------------------------------------
113 # render([$start, $end])
114 #
115 # Returns the data formatted as a table. By default, all rows are
116 # returned; if $start or $end are specified, then only those indexes
117 # are returned. Those are the start and end indexes!
118 # -------------------------------------------------------------------
119 26 ▶sub render {
Subroutine render: called CC=7 SCAR=19.5
Pod documented
120 26 my $self = shift;
121 26 ▶ my $start = shift || 0;
Condition100%2/2
shift() || 0
l!l
125
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
122 26 ▶ my $end = shift || $#{ $self->{ _DATA } };
Condition66%2/3
shift() || $#{$$self{'_DATA'};}
l!l&&r!l&&!r
2240
MC/DC0%0/2
shift() || $#{$$self{'_DATA'};}
shift() $#{$$self{'_DATA'};}
Truth table66%2/3
A || B
A= shift()B= $#{$$self{'_DATA'};}
ABresult
1X1
011
000
123 26 my $size = $self->{ _SIZE };
124 26 my ($bar, @columns, $datum, @text);
125  
126 $bar = join "+", "",
127 26 map( { "-" x ($_ + 2) } @{ $self->{ _LENGTHS } }),
128 "";
129  
130 26 push @text, $bar;
131 26 ▶ if (@columns = $self->columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@columns = $self->columns) 22 4 26
132 22 push @text, _format_line(\@columns, $self->{ _LENGTHS });
133 22 push @text, $bar;
134 }
135  
136 26 ▶ for (my $i = $start; $i <= $end; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 94 26 120
137 94 $datum = $self->{ _DATA }->[$i];
138 94 ▶ last unless defined $datum;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $datum 0 94 94
139  
140 # Pad the array if there are more elements in @columns
141 94 ▶ push @$datum, ""
Branch: 1 branch
BranchTrueFalseTotal
@$datum == $size or do { push @$datum, "" } 94 2 96
142 until (@$datum == $size);
143 94 push @text, _format_line($datum, $self->{ _LENGTHS });
144 }
145  
146 26 push @text, $bar;
147 26 return join "\n", @text;
148 }
149  
150 1 ▶sub items {
Subroutine items: called CC=1 SCAR=0.0
Pod documented
151 1 my $self = shift;
152 1 return scalar @{ $self->{ _DATA } };
153 }
154  
155 # ----------------------------------------------------------------------
156 # reset()
157 #
158 # Resets the instance.
159 # ----------------------------------------------------------------------
160 2 ▶sub reset {
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
161 2 my $self = shift;
162  
163 2 @{ $self->{ _COLUMNS } } = ();
164 2 @{ $self->{ _LENGTHS } } = ();
165 2 @{ $self->{ _DATA } } = ();
166  
167 2 ▶ $self->columns(@_) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
168  
169 2 return $self;
170 }
171  
172 # ----------------------------------------------------------------------
173 # populate(\@data)
174 #
175 # populate() takes a reference to an array of references to arrays,
176 # and calls add() repeatedly. Primarily for use with DBI's
177 # selectall_arrayref() method.
178 # ----------------------------------------------------------------------
179 5 ▶sub populate {
Subroutine populate: called CC=4 SCAR=14.0
Pod documented
180 5 my $self = shift;
181 5 ▶ (@_) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self 5 0 5
182 5 ▶ my $data = UNIVERSAL::isa($_[0], 'ARRAY') ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'ARRAY') ? : 5 0 5
183  
184 5 ▶ for (my $i = 0; $i <= $#$data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $data->$#*) 37 5 42
185 37 $self->add($data->[$i]);
186 }
187  
188 5 return $self;
189 }
190  
191  
192 # ----------------------------------------------------------------------
193 # paginate($items_per_page)
194 #
195 # Returns a list of rendered pages, with $items_per_page - 4 elements
196 # on each (operational overhead)
197 # ----------------------------------------------------------------------
198 0 ▶sub paginate {
Subroutine paginate: not called CC=4 SCAR=30.0
Pod undocumented
199 0 my $self = shift;
200 0 ▶ my $items_per_page = shift || 62;
Condition0%0/2
shift() || 62
l!l
00
MC/DC0%0/1
shift() || 62
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
201 0 my ($items, $pages, $current, @pages);
202  
203 0 $items = $self->items;
204 0 $pages = $items / $items_per_page;
205 0 ▶ $pages += 1 if $items % $items_per_page;
Branch: 1 branch
BranchTrueFalseTotal
if $items % $items_per_page 0 0 0
206  
207 0 ▶ for (my $i = 0; $i < $pages; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pages) 0 0 0
208 0 push @pages, $self->render($current, $items_per_page);
209 0 $current += $items_per_page;
210 }
211  
212 0 return @pages;
213 }
214  
215  
216 # ---====================== Private Methods ======================---
217  
218  
219 # -------------------------------------------------------------------
220 # _add(\@where, \@lengths, \@add)
221 #
222 # Adds @add to @where and modifies @lengths, as necessary
223 # -------------------------------------------------------------------
224 103 ▶sub _add {
Subroutine _add: called CC=5 SCAR=16.1
225 103 my $self = shift;
226 103 my ($where, $length, $size, $add) = @_;
227 103 my @data;
228  
229 103 ▶ $$size = scalar @$add
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$add > $$size 20 83 103
230 if (scalar @$add > $$size);
231  
232 103 ▶ for (my $i = 0; $i <= $#$add; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $add->$#*) 297 103 400
233 297 my $l = _column_length($add->[$i]);
234  
235 297 push @data, $add->[$i];
236 297 ▶ $length->[$i] = $l
Branch: 1 branch
BranchTrueFalseTotal
unless $length->[$i] and $length->[$i] > $l 147 150 297
Condition100%3/3
$length->[$i] and $length->[$i] > $l
!ll&&!rl&&r
5988150
MC/DC100%2/2
$length->[$i] and $length->[$i] > $l
$length->[$i] $length->[$i] > $l
Truth table100%3/3
A and B
A= $length->[$i]B= $length->[$i] > $l
ABresult
0X0
100
111
237 unless $length->[$i] && $length->[$i] > $l;
238 }
239 103 push @$where, \@data;
240 }
241  
242 # -------------------------------------------------------------------
243 # _format_line(\@columns, \@lengths)
244 #
245 # Returns a formatted line out of @columns; the size of $column[$i]
246 # is determined by $length[$i].
247 # -------------------------------------------------------------------
248 116 ▶sub _format_line {
Subroutine _format_line: called CC=8 SCAR=20.8
249 116 my ($columns, $lengths) = @_;
250  
251 116 my $height = 0;
252 116 my @col_lines;
253 116 for (@$columns) {
254 339 ▶ my @lines = split "\n", ((defined $_ && length $_) ? $_ : ' ');
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && length $_ ? : 328 11 339
Condition100%3/3
defined $_ and length $_
!ll&&!rl&&r
47328
MC/DC100%2/2
defined $_ and length $_
defined $_ length $_
Truth table100%3/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
255 339 ▶ $height = scalar @lines
Branch: 1 branch
BranchTrueFalseTotal
if $height < @lines 118 221 339
256 if $height < @lines;
257 339 push @col_lines, \@lines;
258 }
259  
260 116 my @lines;
261 116 for my $h (0 .. $height - 1 ) {
262 120 my @line;
263 120 ▶ for (my $i = 0; $i <= $#$columns; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $columns->$#*) 351 120 471
264 351 ▶ my $val = defined($col_lines[$i][$h]) ? $col_lines[$i][$h] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $col_lines[$i][$h] ? : 345 6 351
265 351 push @line, sprintf " %-" . $lengths->[$i] . "s ", $val;
266 }
267 120 push @lines, join '|', "", @line, "";
268 }
269  
270 116 return join "\n", @lines;
271 }
272  
273 # -------------------------------------------------------------------
274 # _column_length($str)
275 # -------------------------------------------------------------------
276 sub _column_length
277 297 ▶{
Subroutine _column_length: called CC=5 SCAR=16.2
278 297 my ($str) = @_;
279 297 ▶ $str = '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 4 293 297
280  
281 297 my $len = 0;
282 297 for (split "\n", $str) {
283 294 ▶ $len = length
Branch: 1 branch
BranchTrueFalseTotal
if $len < length $_ 292 2 294
284 if $len < length;
285 }
286  
287 # untaint $len
288 297 ▶ unless ($len =~ m|^(\d+)$|s)
Branch: 1 branch
BranchTrueFalseTotal
unless ($len =~ /^(\d+)$/s) 0 297 297
289 0 { die 'invalid length: ' . $len }
290 297 $len = $1;
291  
292 297 return $len;
293 }
294  
295 17 1;
296  
297 __END__
↑ Summary