line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Table::Tiny; |
2
|
|
|
|
|
|
|
$Text::Table::Tiny::VERSION = '1.02'; |
3
|
10
|
|
|
10
|
|
1032207
|
use 5.010; |
|
10
|
|
|
|
|
130
|
|
4
|
10
|
|
|
10
|
|
56
|
use strict; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
219
|
|
5
|
10
|
|
|
10
|
|
49
|
use warnings; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
263
|
|
6
|
10
|
|
|
10
|
|
6079
|
use utf8; |
|
10
|
|
|
|
|
151
|
|
|
10
|
|
|
|
|
56
|
|
7
|
10
|
|
|
10
|
|
4531
|
use parent 'Exporter'; |
|
10
|
|
|
|
|
3080
|
|
|
10
|
|
|
|
|
62
|
|
8
|
10
|
|
|
10
|
|
586
|
use Carp qw/ croak /; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
538
|
|
9
|
10
|
|
|
10
|
|
4977
|
use Ref::Util 0.202 qw/ is_arrayref /; |
|
10
|
|
|
|
|
17160
|
|
|
10
|
|
|
|
|
797
|
|
10
|
10
|
|
|
10
|
|
4639
|
use String::TtyLength 0.02 qw/ tty_width /; |
|
10
|
|
|
|
|
235263
|
|
|
10
|
|
|
|
|
13616
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ generate_table /; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Legacy package globals, that can be used to customise the look. |
15
|
|
|
|
|
|
|
# These are only used in the "classic" style. |
16
|
|
|
|
|
|
|
# I wish I could drop them, but I don't want to break anyone's code. |
17
|
|
|
|
|
|
|
our $COLUMN_SEPARATOR = '|'; |
18
|
|
|
|
|
|
|
our $ROW_SEPARATOR = '-'; |
19
|
|
|
|
|
|
|
our $CORNER_MARKER = '+'; |
20
|
|
|
|
|
|
|
our $HEADER_ROW_SEPARATOR = '='; |
21
|
|
|
|
|
|
|
our $HEADER_CORNER_MARKER = 'O'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my %arguments = ( |
24
|
|
|
|
|
|
|
rows => "the rows, including a possible header row, of the table", |
25
|
|
|
|
|
|
|
header_row => "if true, indicates that the first row is a header row", |
26
|
|
|
|
|
|
|
separate_rows => "if true, a separate rule will be drawn between each row", |
27
|
|
|
|
|
|
|
top_and_tail => "if true, miss out top and bottom edges of table", |
28
|
|
|
|
|
|
|
align => "either single alignment, or an array per of alignments per col", |
29
|
|
|
|
|
|
|
style => "styling of table, one of classic, boxrule, or norule", |
30
|
|
|
|
|
|
|
indent => "indent every row of the table a certain number of spaces", |
31
|
|
|
|
|
|
|
compact => "narrow columns (no space either side of content)", |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my %charsets = ( |
35
|
|
|
|
|
|
|
classic => { TLC => '+', TT => '+', TRC => '+', HR => '-', VR => '|', FHR => '=', LT => '+', RT => '+', FLT => 'O', FRT => 'O', HC => '+', FHC => 'O', BLC => '+', BT => '+', BRC => '+' }, |
36
|
|
|
|
|
|
|
boxrule => { TLC => '┌', TT => '┬', TRC => '┐', HR => '─', VR => '│', FHR => '═', LT => '├', RT => '┤', FLT => '╞', FRT => '╡', HC => '┼', FHC => '╪', BLC => '└', BT => '┴', BRC => '┘' }, |
37
|
|
|
|
|
|
|
norule => { TLC => ' ', TT => ' ', TRC => ' ', HR => ' ', VR => ' ', FHR => ' ', LT => ' ', RT => ' ', FLT => ' ', FRT => ' ', HC => ' ', FHC => ' ', BLC => ' ', BT => ' ', BRC => ' ' }, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub generate_table |
41
|
|
|
|
|
|
|
{ |
42
|
28
|
|
|
28
|
1
|
13844
|
my %param = @_; |
43
|
|
|
|
|
|
|
|
44
|
28
|
|
|
|
|
111
|
foreach my $arg (keys %param) { |
45
|
78
|
100
|
|
|
|
524
|
croak "unknown argument '$arg'" if not exists $arguments{$arg}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
27
|
100
|
|
|
|
299
|
my $rows = $param{rows} or croak "you must pass the 'rows' argument!"; |
49
|
26
|
|
|
|
|
70
|
my @rows = @$rows; |
50
|
26
|
|
|
|
|
68
|
my @widths = _calculate_widths($rows); |
51
|
|
|
|
|
|
|
|
52
|
26
|
|
100
|
|
|
137
|
$param{style} //= 'classic'; |
53
|
|
|
|
|
|
|
|
54
|
26
|
|
100
|
|
|
125
|
$param{indent} //= ''; |
55
|
26
|
100
|
|
|
|
85
|
$param{indent} = ' ' x $param{indent} if $param{indent} =~ /^[0-9]+$/; |
56
|
|
|
|
|
|
|
|
57
|
26
|
|
|
|
|
51
|
my $style = $param{style}; |
58
|
26
|
50
|
|
|
|
72
|
croak "unknown style '$style'" if not exists($charsets{ $style }); |
59
|
26
|
|
|
|
|
50
|
my $char = $charsets{$style}; |
60
|
|
|
|
|
|
|
|
61
|
26
|
100
|
|
|
|
73
|
if ($style eq 'classic') { |
62
|
22
|
|
|
|
|
121
|
$char->{TLC} = $char->{TRC} = $char->{TT} = $char->{LT} = $char->{RT} = $char->{HC} = $char->{BLC} = $char->{BT} = $char->{BRC} = $CORNER_MARKER; |
63
|
22
|
|
|
|
|
39
|
$char->{HR} = $ROW_SEPARATOR; |
64
|
22
|
|
|
|
|
45
|
$char->{VR} = $COLUMN_SEPARATOR; |
65
|
22
|
|
|
|
|
55
|
$char->{FLT} = $char->{FRT} = $char->{FHC} = $HEADER_CORNER_MARKER; |
66
|
22
|
|
|
|
|
43
|
$char->{FHR} = $HEADER_ROW_SEPARATOR; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
26
|
|
|
|
|
47
|
my $header; |
70
|
|
|
|
|
|
|
my @align; |
71
|
26
|
100
|
|
|
|
73
|
if (defined $param{align}) { |
72
|
|
|
|
|
|
|
@align = is_arrayref($param{align}) |
73
|
3
|
|
|
|
|
9
|
? @{ $param{align} } |
74
|
9
|
100
|
|
|
|
42
|
: ($param{align}) x int(@widths) |
75
|
|
|
|
|
|
|
; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
17
|
|
|
|
|
53
|
@align = ('l') x int(@widths); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
26
|
100
|
|
|
|
78
|
$header = shift @rows if $param{header_row}; |
82
|
|
|
|
|
|
|
|
83
|
26
|
|
|
|
|
83
|
my $table = _top_border(\%param, \@widths, $char) |
84
|
|
|
|
|
|
|
._header_row(\%param, $header, \@widths, \@align, $char) |
85
|
|
|
|
|
|
|
._header_rule(\%param, \@widths, $char) |
86
|
|
|
|
|
|
|
._body(\%param, \@rows, \@widths, \@align, $char) |
87
|
|
|
|
|
|
|
._bottom_border(\%param, \@widths, $char); |
88
|
26
|
|
|
|
|
90
|
chop($table); |
89
|
|
|
|
|
|
|
|
90
|
26
|
|
|
|
|
121
|
return $table; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _top_border |
94
|
|
|
|
|
|
|
{ |
95
|
26
|
|
|
26
|
|
64
|
my ($param, $widths, $char) = @_; |
96
|
|
|
|
|
|
|
|
97
|
26
|
100
|
|
|
|
77
|
return '' if $param->{top_and_tail}; |
98
|
22
|
|
|
|
|
77
|
return _rule_row($param, $widths, $char->{TLC}, $char->{HR}, $char->{TT}, $char->{TRC}); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _bottom_border |
102
|
|
|
|
|
|
|
{ |
103
|
26
|
|
|
26
|
|
73
|
my ($param, $widths, $char) = @_; |
104
|
|
|
|
|
|
|
|
105
|
26
|
100
|
|
|
|
79
|
return '' if $param->{top_and_tail}; |
106
|
22
|
|
|
|
|
69
|
return _rule_row($param, $widths, $char->{BLC}, $char->{HR}, $char->{BT}, $char->{BRC}); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _rule_row |
110
|
|
|
|
|
|
|
{ |
111
|
70
|
|
|
70
|
|
159
|
my ($param, $widths, $le, $hr, $cross, $re) = @_; |
112
|
70
|
50
|
|
|
|
148
|
my $pad = $param->{compact} ? '' : $hr; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return $param->{indent} |
115
|
|
|
|
|
|
|
.$le |
116
|
70
|
|
|
|
|
162
|
.join($cross, map { $pad.($hr x $_).$pad } @$widths) |
|
247
|
|
|
|
|
743
|
|
117
|
|
|
|
|
|
|
.$re |
118
|
|
|
|
|
|
|
."\n" |
119
|
|
|
|
|
|
|
; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _header_row |
123
|
|
|
|
|
|
|
{ |
124
|
26
|
|
|
26
|
|
62
|
my ($param, $row, $widths, $align, $char) = @_; |
125
|
26
|
100
|
|
|
|
99
|
return '' unless $param->{header_row}; |
126
|
|
|
|
|
|
|
|
127
|
17
|
|
|
|
|
46
|
return _text_row($param, $row, $widths, $align, $char); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub _header_rule |
131
|
|
|
|
|
|
|
{ |
132
|
26
|
|
|
26
|
|
59
|
my ($param, $widths, $char) = @_; |
133
|
26
|
100
|
|
|
|
87
|
return '' unless $param->{header_row}; |
134
|
17
|
100
|
|
|
|
55
|
my $fancy = $param->{separate_rows} ? 'F' : ''; |
135
|
|
|
|
|
|
|
|
136
|
17
|
|
|
|
|
91
|
return _rule_row($param, $widths, $char->{"${fancy}LT"}, $char->{"${fancy}HR"}, $char->{"${fancy}HC"}, $char->{"${fancy}RT"}); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _body |
140
|
|
|
|
|
|
|
{ |
141
|
26
|
|
|
26
|
|
58
|
my ($param, $rows, $widths, $align, $char) = @_; |
142
|
26
|
100
|
|
|
|
91
|
my $divider = $param->{separate_rows} ? _rule_row($param, $widths, $char->{LT}, $char->{HR}, $char->{HC}, $char->{RT}) : ''; |
143
|
|
|
|
|
|
|
|
144
|
26
|
|
|
|
|
73
|
return join($divider, map { _text_row($param, $_, $widths, $align, $char) } @$rows); |
|
69
|
|
|
|
|
134
|
|
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _text_row |
148
|
|
|
|
|
|
|
{ |
149
|
86
|
|
|
86
|
|
148
|
my ($param, $row, $widths, $align, $char) = @_; |
150
|
86
|
|
|
|
|
171
|
my @columns = @$row; |
151
|
86
|
|
|
|
|
188
|
my $text = $param->{indent}.$char->{VR}; |
152
|
|
|
|
|
|
|
|
153
|
86
|
|
|
|
|
194
|
for (my $i = 0; $i < @$widths; $i++) { |
154
|
299
|
|
100
|
|
|
910
|
$text .= _format_column($columns[$i] // '', $widths->[$i], $align->[$i] // 'l', $param, $char); |
|
|
|
50
|
|
|
|
|
155
|
299
|
|
|
|
|
3745
|
$text .= $char->{VR}; |
156
|
|
|
|
|
|
|
} |
157
|
86
|
|
|
|
|
142
|
$text .= "\n"; |
158
|
|
|
|
|
|
|
|
159
|
86
|
|
|
|
|
355
|
return $text; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _format_column |
163
|
|
|
|
|
|
|
{ |
164
|
299
|
|
|
299
|
|
534
|
my ($text, $width, $align, $param, $char) = @_; |
165
|
299
|
50
|
|
|
|
491
|
my $pad = $param->{compact} ? '' : ' '; |
166
|
|
|
|
|
|
|
|
167
|
299
|
100
|
100
|
|
|
1440
|
if ($align eq 'r' || $align eq 'right') { |
|
|
100
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
168
|
12
|
|
|
|
|
25
|
return $pad.' ' x ($width - tty_width($text)).$text.$pad; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
elsif ($align eq 'c' || $align eq 'center' || $align eq 'centre') { |
171
|
24
|
|
|
|
|
48
|
my $total_spaces = $width - tty_width($text); |
172
|
24
|
|
|
|
|
294
|
my $left_spaces = int($total_spaces / 2); |
173
|
24
|
|
|
|
|
34
|
my $right_spaces = $left_spaces; |
174
|
24
|
100
|
|
|
|
48
|
$right_spaces++ if $total_spaces % 2 == 1; |
175
|
24
|
|
|
|
|
78
|
return $pad.(' ' x $left_spaces).$text.(' ' x $right_spaces).$pad; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
else { |
178
|
263
|
|
|
|
|
578
|
return $pad.$text.' ' x ($width - tty_width($text)).$pad; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub _calculate_widths |
183
|
|
|
|
|
|
|
{ |
184
|
26
|
|
|
26
|
|
50
|
my $rows = shift; |
185
|
26
|
|
|
|
|
43
|
my @widths; |
186
|
26
|
|
|
|
|
55
|
foreach my $row (@$rows) { |
187
|
86
|
|
|
|
|
185
|
my @columns = @$row; |
188
|
86
|
|
|
|
|
197
|
for (my $i = 0; $i < @columns; $i++) { |
189
|
252
|
100
|
|
|
|
502
|
next unless defined($columns[$i]); |
190
|
|
|
|
|
|
|
|
191
|
205
|
|
|
|
|
412
|
my $width = tty_width($columns[$i]); |
192
|
|
|
|
|
|
|
|
193
|
205
|
100
|
100
|
|
|
17218
|
$widths[$i] = $width if !defined($widths[$i]) |
194
|
|
|
|
|
|
|
|| $width > $widths[$i]; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
26
|
|
|
|
|
74
|
return @widths; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# Back-compat: 'table' is an alias for 'generate_table', but isn't exported |
201
|
|
|
|
|
|
|
*table = \&generate_table; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
1; |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
__END__ |