line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Goo::TextTable; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# Nigel Hamilton |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright Nigel Hamilton 2005 |
7
|
|
|
|
|
|
|
# All Rights Reserved |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Author: Nigel Hamilton |
10
|
|
|
|
|
|
|
# Filename: Goo::TextTable.pm |
11
|
|
|
|
|
|
|
# Description: Create a simple fixed-width text table |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# Date Change |
14
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
15
|
|
|
|
|
|
|
# 08/11/2005 Created test file: TextTableTest.tpm |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
4571
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
8
|
use Goo::Object; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
22
|
1
|
|
|
1
|
|
6
|
use Text::FormatTable; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
1
|
|
7
|
use base qw(Goo::Object); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
413
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
############################################################################### |
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# add_row - add a row to the table |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
############################################################################### |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub add_row { |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
0
|
1
|
|
my ($this, @columns) = @_; |
36
|
|
|
|
|
|
|
|
37
|
0
|
0
|
|
|
|
|
unless ($this->{rows}) { |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# store an array |
40
|
0
|
|
|
|
|
|
$this->{rows} = (); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# remember the maximum column count |
44
|
0
|
0
|
|
|
|
|
if (scalar(@columns) > $this->{column_count}) { |
45
|
0
|
|
|
|
|
|
$this->{column_count} = scalar(@columns); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# for each column remember the maximum width |
49
|
0
|
|
|
|
|
|
foreach my $column (1 .. scalar(@columns)) { |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# work out the width of this column |
52
|
0
|
|
|
|
|
|
my $column_width = length($columns[ $column - 1 ]); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# unless there is a max column length aleady - record one |
55
|
0
|
0
|
|
|
|
|
unless (exists $this->{max_width}->{$column}) { |
56
|
0
|
|
|
|
|
|
$this->{max_width}->{$column} = $column_width; |
57
|
0
|
|
|
|
|
|
next; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# otherwise check if it exceeds the current maximum? |
61
|
0
|
0
|
|
|
|
|
if ($column_width > $this->{max_width}->{$column}) { |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# remember it |
64
|
0
|
|
|
|
|
|
$this->{max_width}->{$column} = $column_width; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# push the columns onto the rows |
70
|
0
|
|
|
|
|
|
push(@{ $this->{rows} }, \@columns); |
|
0
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
############################################################################### |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
# render - return a table |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
############################################################################### |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub render { |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
0
|
1
|
|
my ($this, $justified) = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# justified left, right or centered |
86
|
0
|
|
0
|
|
|
|
$justified = $justified || "l"; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $format = ""; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# contruct a format for each column |
91
|
0
|
|
|
|
|
|
foreach my $column (1 .. $this->{column_count}) { |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
$format .= "$this->{max_width}->{$column}l "; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my $table = Text::FormatTable->new($format); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# go through each row in the table |
100
|
0
|
|
|
|
|
|
foreach my $row (@{ $this->{rows} }) { |
|
0
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# we need to pad out the row if needs be |
103
|
0
|
|
|
|
|
|
$table->row(@$row); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
return $table->render(); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 NAME |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Goo::TextTable - Create a simple fixed-width text table |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SYNOPSIS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
use Goo::TextTable; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 METHODS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=over |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item add_row |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
add a row to the table |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item render |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
return a text table |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=back |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 AUTHOR |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Nigel Hamilton <nigel@trexy.com> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 SEE ALSO |
146
|
|
|
|
|
|
|
|