FormatTable.pm

filter: Statementstmt 88.9% 218 / 245 Branchbran 76.7% 66 / 86 Conditioncond 68.6% 35 / 51 MC/DCmcdc 41.3% 12 / 29 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 81.7% 359 / 439 CC 77 SCAR 24.7
CC
77
Cov
84%
_render_data
23.8
_calculate_widths
19.1
_render_rule
10.9
CRAP 11.8
↑ Summary example.pl »
1 package Text::FormatTable;
2  
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $VERSION = '1.03';
9  
10 =head1 NAME
11  
12 Text::FormatTable - Format text tables
13  
14 =head1 SYNOPSIS
15  
16 my $table = Text::FormatTable->new('r|l');
17 $table->head('a', 'b');
18 $table->rule('=');
19 $table->row('c', 'd');
20 print $table->render(20);
21  
22 =head1 DESCRIPTION
23  
24 Text::FormatTable renders simple tables as text. You pass to the constructor
25 (I<new>) a table format specification similar to LaTeX (e.g. C<r|l|5l|R|20L>) and you
26 call methods to fill the table data and insert rules. After the data is filled,
27 you call the I<render> method and the table gets formatted as text.
28  
29 Methods:
30  
31 =over 4
32  
33 =cut
34  
35 # Remove ANSI color sequences when calculating length
36 sub _uncolorized_length($)
37 130 ▶{
Subroutine _uncolorized_length: called CC=1 SCAR=0.0
38 130 my $str = shift;
39 130 $str =~ s/\e \[ [^m]* m//xmsg;
40 130 return length $str;
41 }
42  
43 # minimal width of $1 if word-wrapped
44 sub _min_width($)
45 23 ▶{
Subroutine _min_width: called CC=5 SCAR=16.1
46 23 my $str = shift;
47 23 my $min;
48 23 for my $s (split(/\s+/,$str)) {
49 37 my $l = _uncolorized_length $s;
50 37 ▶ $min = $l if not defined $min or $l > $min;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $l > $min 26 11 37
Condition100%3/3
not defined $min or $l > $min
l!l&&r!l&&!r
22411
MC/DC100%2/2
not defined $min or $l > $min
not defined $min $l > $min
Truth table100%3/3
not(A) or B
A= not defined $minB= $l > $min
ABresult
1X1
011
000
51 }
52 23 ▶ return $min ? $min : 1;
Branch: 1 branch
BranchTrueFalseTotal
$min ? : 22 1 23
53 }
54  
55 # width of $1 if not word-wrapped
56 sub _max_width($)
57 23 ▶{
Subroutine _max_width: called CC=2 SCAR=6.9
58 23 my $str = shift;
59 23 my $len = _uncolorized_length $str;
60 23 ▶ return $len ? $len : 1;
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 22 1 23
61 }
62  
63 sub _max($$)
64 46 ▶{
Subroutine _max: called CC=4 SCAR=13.9
65 46 my ($a,$b) = @_;
66 46 ▶ return $a if defined $a and (not defined $b or $a >= $b);
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and !defined($b) || $a >= $b 16 30 46
Condition83%5/6
defined $a and !defined($b) || $a >= $b
!ll&&!rl&&r
22816
not defined $b or $a >= $b
l!l&&r!l&&!r
0168
MC/DC66%2/3
defined $a and !defined($b) || $a >= $b
defined $a not defined $b $a >= $b
Truth table75%3/4
A and not(B) or C
A= defined $aB= not defined $bC= $a >= $b
ABCresult
0XX0
1000
11X1
1011
67 30 return $b;
68 }
69  
70 # word-wrap multi-line $2 with width $1
71 sub _wrap($$)
72 23 ▶{
Subroutine _wrap: called CC=2 SCAR=6.9
73 23 my ($width, $text) = @_;
74 23 my @lines = split(/\n/, $text);
75 23 my @w = ();
76 23 for my $l (@lines) {
77 22 push @w, @{_wrap_line($width, $l)};
78 }
79 23 return \@w;
80 }
81  
82 sub _wrap_line($$)
83 22 ▶{
Subroutine _wrap_line: called CC=5 SCAR=22.8
84 22 my ($width, $text) = @_;
85 22 my $width_m1 = $width-1;
86 22 my @t = ($text);
87 22 while(1) {
88 35 my $t = pop @t;
89 35 my $l = _uncolorized_length $t;
90 35 ▶ if($l <= $width){
Branch: 3 branches
BranchTrueFalseTotal
if ($l <= $width) { } 22 13 35
elsif ($t =~ /^(.{0,$width_m1}\S)\s+(\S.*?)$/) { } 13 0 13
elsif ($t =~ /(.{$width,}?\S)\s+(\S.*?)$/) { } 0 0 0
91 # last line is ok => done
92 22 push @t, $t;
93 22 return \@t;
94 }
95 elsif($t =~ /^(.{0,$width_m1}\S)\s+(\S.*?)$/) {
96 # farest space < width
97 13 push @t, $1;
98 13 push @t, $2;
99 }
100 elsif($t =~ /(.{$width,}?\S)\s+(\S.*?)$/) {
101 # nearest space > width
102 0 ▶ if ( _uncolorized_length $1 > $width_m1 )
Branch: 1 branch
BranchTrueFalseTotal
if (&_uncolorized_length($1) > $width_m1) { } 0 0 0
103 {
104 # hard hyphanation
105 0 my $left = substr($1,0,$width);
106 0 my $right= substr($1,$width);
107  
108 0 push @t, $left;
109 0 push @t, $right;
110 0 push @t, $2;
111 }
112 else
113 {
114 0 push @t, $1;
115 0 push @t, $2;
116 }
117 }
118 else {
119 # hard hyphanation
120 0 my $left = substr($t,0,$width);
121 0 my $right= substr($t,$width);
122  
123 0 push @t, $left;
124 0 push @t, $right;
125 0 return \@t;
126 }
127 }
128 0 return \@t;
129 }
130  
131 # render left-box $2 with width $1
132 sub _l_box($$)
133 20 ▶{
Subroutine _l_box: called CC=1 SCAR=0.0
134 20 my ($width, $text) = @_;
135 20 my $lines = _wrap($width, $text);
136 20 map { $_ .= ' 'x($width-_uncolorized_length($_)) } @$lines;
137 20 return $lines;
138 }
139  
140 # render right-box $2 with width $1
141 sub _r_box($$)
142 3 ▶{
Subroutine _r_box: called CC=1 SCAR=0.0
143 3 my ($width, $text) = @_;
144 3 my $lines = _wrap($width, $text);
145 3 map { $_ = (' 'x($width-_uncolorized_length($_)).$_) } @$lines;
146 3 return $lines;
147 }
148  
149 # Algorithm of:
150 # http://ei5nazha.yz.yamagata-u.ac.jp/~aito/w3m/eng/STORY.html
151  
152 sub _distribution_f($)
153 10 ▶{
Subroutine _distribution_f: called CC=1 SCAR=0.0
154 10 my $max_width = shift;
155 10 return log($max_width);
156 }
157  
158 sub _calculate_widths($$)
159 4 ▶{
Subroutine _calculate_widths: called CC=19 SCAR=29.5
160 4 my ($self, $width) = @_;
161 4 my @widths = ();
162 # calculate min and max widths for each column
163 4 for my $r (@{$self->{data}})
164 {
165 13 ▶ $r->[0] eq 'data' or $r->[0] eq 'head' or next;
Branch: 1 branch
BranchTrueFalseTotal
$r->[0] eq "data" or $r->[0] eq "head" or next 8 5 13
Condition100%3/3
$r->[0] eq "data" or $r->[0] eq "head"
l!l&&r!l&&!r
445
MC/DC100%2/2
$r->[0] eq "data" or $r->[0] eq "head"
$r->[0] eq "data" $r->[0] eq "head"
Truth table100%3/3
A or B
A= $r->[0] eq "data"B= $r->[0] eq "head"
ABresult
1X1
011
000
166 8 my $cn=0;
167 8 my ($max, $min) = (0,0);
168
169 8 for my $c (@{$r->[1]}) {
170
171 23 ▶ if ( $self->{fixed_widths}[$cn] )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fixed_widths'}[$cn]) { } 0 23 23
172 {
173 # fixed width
174 0 $widths[$cn][0] = $self->{fixed_widths}[$cn];
175 0 $widths[$cn][1] = $self->{fixed_widths}[$cn];
176 }
177 else
178 {
179 23 $widths[$cn][0] = _max($widths[$cn][0], _min_width $c);
180 23 $widths[$cn][1] = _max($widths[$cn][1], _max_width $c);
181 }
182 23 $cn++;
183 }
184 }
185  
186 # calculate total min and max width
187 4 my ($total_min, $total_max) = (0,0);
188 4 for my $c (@widths) {
189 11 $total_min += $c->[0];
190 11 $total_max += $c->[1];
191 }
192 # extra space
193 19 ▶ my $extra_width += scalar grep {$_->[0] eq '|' or $_->[0] eq ' '}
Condition100%3/3
$_->[0] eq '|' || $_->[0] eq ' '
l!l&&r!l&&!r
1711
MC/DC100%2/2
$_->[0] eq '|' || $_->[0] eq ' '
$_->[0] eq '|' $_->[0] eq ' '
Truth table100%3/3
A || B
A= $_->[0] eq '|'B= $_->[0] eq ' '
ABresult
1X1
011
000
194 4 (@{$self->{format}});
195 4 $total_min += $extra_width;
196 4 $total_max += $extra_width;
197  
198 # if total_max <= screen width => use max as width
199 4 ▶ if($total_max <= $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_max <= $width) { } 3 1 4
200 3 my $cn = 0;
201 3 for my $c (@widths) {
202 8 $self->{widths}[$cn]=$c->[1];
203 8 $cn++;
204 }
205 3 $self->{total_width} = $total_max;
206 }
207 else {
208 1 my @dist_width;
209 1 ITERATION: while(1) {
210 2 my $total_f = 0.0;
211 2 my $fixed_width = 0;
212 2 my $remaining=0;
213 2 for my $c (@widths) {
214 6 ▶ if(defined $c->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->[2]) { } 1 5 6
215 1 $fixed_width += $c->[2];
216 }
217 else {
218 5 $total_f += _distribution_f($c->[1]);
219 5 $remaining++;
220 }
221 }
222 2 my $available_width = $width-$extra_width-$fixed_width;
223 # enlarge width if it isn't enough
224 2 ▶ if($available_width < $remaining*5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($available_width < $remaining * 5) 1 1 2
225 1 $available_width = $remaining*5;
226 1 $width = $extra_width+$fixed_width+$available_width;
227 }
228 2 my $cn=-1;
229 2 COLUMN: for my $c (@widths) {
230 6 $cn++;
231 6 ▶ next COLUMN if defined $c->[2]; # skip fixed-widths
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->[2] 1 5 6
232 5 my $w = _distribution_f($c->[1]) * $available_width / $total_f;
233 5 ▶ if($c->[0] > $w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[0] > $w) 1 4 5
234 1 $c->[2] = $c->[0];
235 1 next ITERATION;
236 }
237 4 ▶ if($c->[1] < $w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[1] < $w) 0 4 4
238 0 $c->[2] = $c->[1];
239 0 next ITERATION;
240 }
241 4 $dist_width[$cn] = int($w);
242 }
243 1 last;
244 }
245 1 my $cn = 0;
246 1 for my $c (@widths) {
247 3 ▶ $self->{widths}[$cn]=defined $c->[2] ? $c->[2] : $dist_width[$cn];
Branch: 1 branch
BranchTrueFalseTotal
defined $c->[2] ? : 1 2 3
248 3 $cn++;
249 }
250 }
251 }
252  
253 sub _render_rule($$)
254 5 ▶{
Subroutine _render_rule: called CC=10 SCAR=23.9
255 5 my ($self, $char) = @_;
256 5 my $out = '';
257 5 my ($col,$data_col) = (0,0);
258 5 for my $c (@{$self->{format}}) {
259 28 ▶ if($c->[0] eq '|') {
Branch: 3 branches
BranchTrueFalseTotal
if ($c->[0] eq '|') { } 3 25 28
elsif ($c->[0] eq ' ') { } 10 15 25
elsif ($c->[0] eq 'l' or $c->[0] eq 'L' or $c->[0] eq 'r' or $c->[0] eq 'R') { } 15 0 15
260 3 ▶ if ($char eq '-') { $out .= '+' }
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '-') { } 1 2 3
elsif ($char eq ' ') { } 0 2 2
261 0 elsif($char eq ' ') { $out .= '|' }
262 2 else { $out .= $char }
263 }
264 elsif($c->[0] eq ' ') {
265 10 $out .= $char;
266 }
267 ▶ elsif( $c->[0] eq 'l'
Condition55%5/9
$c->[0] eq "l" or $c->[0] eq "L" or $c->[0] eq "r" or $c->[0] eq "R"
l!l&&r!l&&!r
1500
$c->[0] eq "l" or $c->[0] eq "L" or $c->[0] eq "r"
l!l&&r!l&&!r
1230
$c->[0] eq "l" or $c->[0] eq "L"
l!l&&r!l&&!r
1203
MC/DC0%0/4
$c->[0] eq "l" or $c->[0] eq "L" or $c->[0] eq "r" or $c->[0] eq "R"
$c->[0] eq "l" $c->[0] eq "L" $c->[0] eq "r" $c->[0] eq "R"
Truth table40%2/5
A or B or C or D
A= $c->[0] eq "l"B= $c->[0] eq "L"C= $c->[0] eq "r"D= $c->[0] eq "R"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
268 or $c->[0] eq 'L'
269 or $c->[0] eq 'r'
270 or $c->[0] eq 'R'
271 ) {
272 15 $out .= ($char)x($self->{widths}[$data_col]);
273 15 $data_col++;
274 }
275 28 $col++;
276 }
277 5 return $out."\n";
278 }
279  
280 sub _render_data($$)
281 8 ▶{
Subroutine _render_data: called CC=19 SCAR=31.7
282 8 my ($self,$data) = @_;
283  
284 8 my @rdata; # rendered data
285  
286 # render every column and find out number of lines
287 8 my ($col, $data_col) = (0,0);
288 8 my $lines=0;
289 8 my @rows_in_column;
290 8 for my $c (@{$self->{format}}) {
291 41 ▶ if( ($c->[0] eq 'l') or ($c->[0] eq 'L') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->[0] eq 'l' or $c->[0] eq 'L') { } 20 21 41
elsif ($c->[0] eq 'r' or $c->[0] eq 'R') { } 3 18 21
Condition66%2/3
$c->[0] eq "l" or $c->[0] eq "L"
l!l&&r!l&&!r
20021
MC/DC50%1/2
$c->[0] eq "l" or $c->[0] eq "L"
$c->[0] eq "l" $c->[0] eq "L"
Truth table66%2/3
A or B
A= $c->[0] eq "l"B= $c->[0] eq "L"
ABresult
1X1
011
000
292 20 my $lb = _l_box($self->{widths}[$data_col], $data->[$data_col]);
293 20 $rdata[$data_col] = $lb;
294 20 my $l = scalar @$lb ;
295 20 ▶ $lines = $l if $lines < $l;
Branch: 1 branch
BranchTrueFalseTotal
if $lines < $l 5 15 20
296 20 $rows_in_column[$data_col] = $l;
297 20 $data_col++;
298 }
299 ▶ elsif( ($c->[0] eq 'r') or ($c->[0] eq 'R' ) ) {
Condition66%2/3
$c->[0] eq "r" or $c->[0] eq "R"
l!l&&r!l&&!r
3018
MC/DC50%1/2
$c->[0] eq "r" or $c->[0] eq "R"
$c->[0] eq "r" $c->[0] eq "R"
Truth table66%2/3
A or B
A= $c->[0] eq "r"B= $c->[0] eq "R"
ABresult
1X1
011
000
300 3 my $rb = _r_box($self->{widths}[$data_col], $data->[$data_col]);
301 3 $rdata[$data_col] = $rb;
302 3 my $l = scalar @$rb ;
303 3 ▶ $lines = $l if $lines < $l;
Branch: 1 branch
BranchTrueFalseTotal
if $lines < $l 3 0 3
304 3 $rows_in_column[$data_col] = $l ;
305 3 $data_col++;
306 }
307 41 $col++;
308 }
309  
310 # render each line
311 8 my $out = '';
312 8 for my $l (0..($lines-1)) {
313 16 my ($col, $data_col) = (0,0);
314 16 for my $c (@{$self->{format}}) {
315 89 ▶ if($c->[0] eq '|') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->[0] eq '|') { } 11 78 89
elsif ($c->[0] eq ' ') { } 31 47 78
elsif ($c->[0] eq 'L' or $c->[0] eq 'R') { } 0 47 47
elsif ($c->[0] eq 'l' or $c->[0] eq 'r') { } 47 0 47
316 11 $out .= '|';
317 }
318 elsif($c->[0] eq ' ') {
319 31 $out .= ' ';
320 }
321 ▶ elsif( $c->[0] eq 'L' or $c->[0] eq 'R')
Condition33%1/3
$c->[0] eq "L" or $c->[0] eq "R"
l!l&&r!l&&!r
0047
MC/DC0%0/2
$c->[0] eq "L" or $c->[0] eq "R"
$c->[0] eq "L" $c->[0] eq "R"
Truth table33%1/3
A or B
A= $c->[0] eq "L"B= $c->[0] eq "R"
ABresult
1X1
011
000
322 {
323 # bottom align
324 0 my $start_print = $lines - $rows_in_column[$data_col];
325
326 0 ▶ if ( defined $rdata[$data_col][$l-$start_print]
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rdata[$data_col][$l - $start_print] and $l >= $start_print) { } 0 0 0
Condition0%0/3
defined $rdata[$data_col][$l - $start_print] and $l >= $start_print
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rdata[$data_col][$l - $start_print] and $l >= $start_print
defined $rdata[$data_col][$l - $start_print] $l >= $start_print
Truth table0%0/3
A and B
A= defined $rdata[$data_col][$l - $start_print]B= $l >= $start_print
ABresult
0X0
100
111
327 and $l >= $start_print
328 )
329 {
330 0 $out .= $rdata[$data_col][$l-$start_print];
331 }
332 else
333 {
334 0 $out .= ' 'x($self->{widths}[$data_col]);
335 }
336 0 $data_col++;
337 }
338 ▶ elsif($c->[0] eq 'l' or $c->[0] eq 'r') {
Condition66%2/3
$c->[0] eq "l" or $c->[0] eq "r"
l!l&&r!l&&!r
36110
MC/DC0%0/2
$c->[0] eq "l" or $c->[0] eq "r"
$c->[0] eq "l" $c->[0] eq "r"
Truth table66%2/3
A or B
A= $c->[0] eq "l"B= $c->[0] eq "r"
ABresult
1X1
011
000
339 # top align
340 47 ▶ if(defined $rdata[$data_col][$l]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rdata[$data_col][$l]) { } 35 12 47
341 35 $out .= $rdata[$data_col][$l];
342 }
343 else {
344 12 $out .= ' 'x($self->{widths}[$data_col]);
345 }
346 47 $data_col++;
347 }
348 89 $col++;
349 }
350 16 $out .= "\n";
351 }
352 8 return $out;
353 }
354  
355 sub _parse_format($$)
356 4 ▶{
Subroutine _parse_format: called CC=9 SCAR=22.7
357 4 my ($self, $format) = @_;
358 4 my @f = split(//, $format);
359 4 my @format = ();
360 4 my @width = ();
361
362 4 my ($col,$data_col) = (0,0);
363 4 my $wid;
364 4 for my $f (@f) {
365 19 ▶ if ( $f =~ /(\d+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /(\d+)/) 0 19 19
366 {
367 0 $wid .= $f;
368 0 next;
369 }
370 19 ▶ if($f eq 'l' or $f eq 'L' or $f eq 'r' or $f eq 'R') {
Branch: 2 branches
BranchTrueFalseTotal
if ($f eq 'l' or $f eq 'L' or $f eq 'r' or $f eq 'R') { } 11 8 19
elsif ($f eq '|' or $f eq ' ') { } 8 0 8
Condition77%7/9
$f eq "l" or $f eq "L" or $f eq "r" or $f eq "R"
l!l&&r!l&&!r
1108
$f eq "l" or $f eq "L" or $f eq "r"
l!l&&r!l&&!r
1018
$f eq "l" or $f eq "L"
l!l&&r!l&&!r
1009
MC/DC50%2/4
$f eq "l" or $f eq "L" or $f eq "r" or $f eq "R"
$f eq "l" $f eq "L" $f eq "r" $f eq "R"
Truth table60%3/5
A or B or C or D
A= $f eq "l"B= $f eq "L"C= $f eq "r"D= $f eq "R"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
371 11 $format[$col] = [$f, $data_col];
372 11 $width[$data_col] = $wid;
373 11 $wid = undef;
374 11 $data_col++;
375 }
376 ▶ elsif($f eq '|' or $f eq ' ') {
Condition66%2/3
$f eq "|" or $f eq " "
l!l&&r!l&&!r
170
MC/DC0%0/2
$f eq "|" or $f eq " "
$f eq "|" $f eq " "
Truth table66%2/3
A or B
A= $f eq "|"B= $f eq " "
ABresult
1X1
011
000
377 8 $format[$col] = [$f];
378 }
379 else {
380 0 croak "unknown column format: $f";
381 }
382 19 $col++;
383 }
384 4 $self->{format}=\@format;
385 4 $self->{fixed_widths}=\@width;
386 4 $self->{col}=$col;
387 4 $self->{data_col}=$data_col;
388 }
389  
390 =item B<new>(I<$format>)
391  
392 Create a Text::FormatTable object, the format of each column is specified as a
393 character of the $format string. The following formats are defined:
394  
395 =over 4
396  
397 =item l
398  
399 Left-justified top aligned word-wrapped text.
400  
401 =item L
402  
403 Left-justified bottom aligned word-wrapped text.
404  
405 =item r
406  
407 Right-justified top aligned word-wrapped text.
408  
409 =item R
410  
411 Right-justified bottom aligned word-wrapped text.
412  
413 =item 10R, 20r, 15L, 12l,
414  
415 Number is fixed width of the column.
416 Justified and aligned word-wrapped text (see above).
417  
418 =item ' '
419  
420 A space.
421  
422 =item |
423  
424 Column separator.
425  
426 =back
427  
428 =cut
429  
430 sub new($$)
431 4 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
432 4 my ($class, $format) = @_;
433 4 ▶ croak "new() requires one argument: format" unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 4 4
434 4 my $self = { col => '0', row => '0', data => [] };
435 4 bless $self, $class;
436 4 $self->_parse_format($format);
437 4 return $self;
438 }
439  
440 # remove head and trail space
441 sub _preprocess_row_data($$)
442 8 ▶{
Subroutine _preprocess_row_data: called CC=2 SCAR=6.9
443 8 my ($self,$data) = @_;
444 8 my $cn = 0;
445 8 for my $c (0..($#$data)) {
446 23 $data->[$c] =~ s/^\s+//m;
447 23 $data->[$c] =~ s/\s+$//m;
448 }
449 }
450  
451 =item B<head>(I<$col1>, I<$col2>, ...)
452  
453 Add a header row using $col1, $col2, etc. as cell contents. Note that, at the
454 moment, header rows are treated like normal rows.
455  
456 =cut
457  
458 sub head($@)
459 4 ▶{
Subroutine head: called CC=2 SCAR=7.0
Pod documented
460 4 my ($self, @data) = @_;
461 scalar @data == $self->{data_col} or
462 4 ▶ croak "number of columns must be $self->{data_col}";
Branch: 1 branch
BranchTrueFalseTotal
scalar @data == $self->{"data_col"} or croak(qq[number of columns must be $self->{"data_col"}]) 4 0 4
463 4 $self->_preprocess_row_data(\@data);
464 4 $self->{data}[$self->{row}++] = ['head', \@data];
465 }
466  
467 =item B<row>(I<$col1>, I<$col2>, ...)
468  
469 Add a row with $col1, $col2, etc. as cell contents.
470  
471 =cut
472  
473 sub row($@)
474 4 ▶{
Subroutine row: called CC=2 SCAR=7.0
Pod documented
475 4 my ($self, @data) = @_;
476 scalar @data == $self->{data_col} or
477 4 ▶ croak "number of columns must be $self->{data_col}";
Branch: 1 branch
BranchTrueFalseTotal
scalar @data == $self->{"data_col"} or croak(qq[number of columns must be $self->{"data_col"}]) 4 0 4
478
479 4 $self->_preprocess_row_data(\@data);
480 4 $self->{data}[$self->{row}++] = ['data', \@data];
481 }
482  
483 =item B<rule>([I<$char>])
484  
485 Add an horizontal rule. If $char is specified it will be used as character to
486 draw the rule, otherwise '-' will be used.
487  
488 =cut
489  
490 sub rule($$)
491 5 ▶{
Subroutine rule: called CC=2 SCAR=6.9
Pod documented
492 5 my ($self, $char) = @_;
493 5 ▶ $char = '-' unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 4 5
494 5 $self->{data}[$self->{row}++] = ['rule', $char];
495 }
496  
497 =item B<render>([I<$screen_width>])
498  
499 Return the rendered table formatted with $screen_width or 79 if it is not
500 specified.
501  
502 =cut
503  
504 sub render($$)
505 4 ▶{
Subroutine render: called CC=6 SCAR=17.9
Pod documented
506 4 my ($self, $width) = @_;
507
508 4 ▶ $width = 79 unless defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 3 1 4
509 4 $self->_calculate_widths($width);
510
511 4 my $out = '';
512 4 for my $r (@{$self->{data}}) {
513 13 ▶ if($r->[0] eq 'rule') {
Branch: 3 branches
BranchTrueFalseTotal
if ($r->[0] eq 'rule') { } 5 8 13
elsif ($r->[0] eq 'head') { } 4 4 8
elsif ($r->[0] eq 'data') { } 4 0 4
514 5 $out .= $self->_render_rule($r->[1]);
515 }
516 elsif($r->[0] eq 'head') {
517 4 $out .= $self->_render_data($r->[1]);
518 }
519 elsif($r->[0] eq 'data') {
520 4 $out .= $self->_render_data($r->[1]);
521 }
522 }
523 4 return $out;
524 }
525  
526 1 1;
527  
528 =back
529  
530 =head1 SEE ALSO
531  
532 Text::ASCIITable
533  
534 =head1 COPYRIGHT
535  
536 Copyright (c) 2001-2004 Swiss Federal Institute of Technology, Zurich.
537 (c) 2009 Trey Harris
538 All Rights Reserved.
539  
540 This module is free software; you can redistribute it and/or
541 modify it under the same terms as Perl itself.
542  
543 =head1 CODE REPOSITORY
544  
545 Git - http://github.com/treyharris/Text-FormatTable/tree/master
546  
547 =head1 AUTHOR
548  
549 S<David Schweikert <dws@ee.ethz.ch>>
550  
551 Maintained by S<Trey Harris <treyharris@gmail.com>>
552  
553 Fixed column width and bottom alignment written by
554 S<Veselin Slavov <vslavov@creditreform.bg>>
555  
556 =cut
557  
558 # vi: et sw=4
↑ Summary example.pl »