-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabularize
executable file
·307 lines (240 loc) · 6.56 KB
/
tabularize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env perl
=pod
=head1 NAME
tabularize - Takes TAB-delimited lines of text and outputs formatted table.
=head1 SYNOPSIS
I<COMMAND> | tabularize [I<OPTIONS>]
=head1 OPTIONS
=over 4
=item -a, --ascii
7-bit ascii borders
=item -u, --unicode
borders with nice graphical chars
=item -H, --no-horizontal
no horizontal lines in the output
=item -M, --no-margins
no margins, ie. no right-most and left-most vertical borders
=item -p, --padding I<NUM>
add space padding in cells.
I<NUM> is how many spaces.
=item -v, --output-vertical-separator I<CHAR>
vertical separator character(s) in the output
=item -r, --align-right I<NUM>
align these columns (0-indexed) to the right,
others are auto-detected and if they seem to hold mostly numeric data,
then aligned to the right;
otherwise to the left.
this option is repeatable.
=item -l, --align-left I<NUM>
similar to --align-right option
=back
=head1 ENVIRONMENT
=over 4
=item PAGER
If B<$PAGER> is set and standard output is a terminal
and the resulting table is wider than the terminal,
then pipe the table through B<$PAGER>.
=back
=head1 SEE ALSO
column(1), untabularize(1)
=cut
use Data::Dumper;
use Date::Parse;
use DateTime::Format::Strptime;
use Encode qw/decode encode decode_utf8 encode_utf8/;
use Getopt::Long qw/:config no_ignore_case bundling no_getopt_compat no_auto_abbrev require_order/;
use IPC::Run qw/run/;
use List::MoreUtils qw/all any none zip/;
use Pod::Usage;
use Term::Size;
no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
sub set_ascii
{
$verticalBorder = '|';
$verticalBar = '|';
$horizontalBar = '-';
$topleftCorner = '+';
$toprightCorner = '+';
$bottomleftCorner = '+';
$bottomrightCorner = '+';
$leftCross = '+';
$middleCross = '+';
$rightCross = '+';
$topCross = '+';
$bottomCross = '+';
}
sub set_unicode
{
$verticalBorder = '│';
$verticalBar = '│';
$horizontalBar = '─';
$topleftCorner = '┌';
$toprightCorner = '┐';
$bottomleftCorner = '└';
$bottomrightCorner = '┘';
$leftCross = '├';
$middleCross = '┼';
$rightCross = '┤';
$topCross = '┬';
$bottomCross = '┴';
}
set_unicode;
use constant { ALIGN_LEFT => 1, ALIGN_RIGHT =>2, };
$OptHorizontal = 1;
$OptMargins = 1;
$OptPadding = 0;
@columnsAlignment = ();
GetOptions(
'a|ascii' => \&set_ascii,
'u|unicode' => \&set_unicode,
'H|no-horizontal' => sub { $OptHorizontal = 0; },
'M|no-margins' => sub { $OptMargins = 0; },
'p|padding=i' => \$OptPadding,
'v|output-vertical-separator=s' => sub {
my ($getopt_obj, $param) = @_;
$verticalBorder = $param;
$verticalBar = $param;
$topleftCorner = $param;
$toprightCorner = $param;
$bottomleftCorner = $param;
$bottomrightCorner = $param;
$leftCross = $param;
$middleCross = $param;
$rightCross = $param;
$topCross = $param;
$bottomCross = $param;
},
'r|align-right=i@' => sub {
my ($getopt_obj, $param) = @_;
$columnsAlignment[$param] = ALIGN_RIGHT;
},
'l|align-left=i@' => sub {
my ($getopt_obj, $param) = @_;
$columnsAlignment[$param] = ALIGN_LEFT;
},
'help' => sub { pod2usage(-exitval=>0, -verbose=>99); },
'<>' => sub { unshift @ARGV, @_[0]; die '!FINISH'; },
) or pod2usage(-exitval=>2, -verbose=>99);
sub is_numeric
{
# consider a cell's value numeric if it has only
# - optional sign prefix,
# - numbers,
# - and optionally a common thousands- and/or fraction separator.
# I don't like the anglo-saxon ".123" notation, missing leading zero before the fraction separator.
local $_ = shift;
/^[+-]?\d+((?'thousands_sep'[,. ])\d+(\g{thousands_sep}\d+)*)?([.,]\d+(\g{thousands_sep}\d+)*)?$/ and return 1;
return 0;
}
# compute the width of each column
@Table = ();
@columnsWidth = ();
@numericals_by_col = ();
@non_numericals_by_col = ();
while(<STDIN>)
{
chomp;
my @cells = split /\t/;
for my $idx (0 .. $#cells)
{
my $cell = $cells[$idx] || '';
my $width = length $cell; # TODO multibyte chars?
$columnsWidth[$idx] = $width if $columnsWidth[$idx] < $width;
# guess if this cell has numerical content (skip 1st line as it's likely a header)
if($. > 0 and not defined $columnsAlignment[$idx])
{
is_numeric($cell) ? ($numericals_by_col[$idx]++) : ($non_numericals_by_col[$idx]++);
}
}
push @Table, \@cells;
}
if(not @Table)
{
exit;
}
# compose the format string
if(not $OptMargins)
{
$verticalBorder = '';
$topleftCorner = '';
$toprightCorner = '';
$bottomleftCorner = '';
$bottomrightCorner = '';
$leftCross = '';
$rightCross = '';
}
for my $idx (0 .. $#columnsWidth)
{
if(not defined $columnsAlignment[$idx])
{
if($numericals_by_col[$idx] >= $non_numericals_by_col[$idx])
{
$columnAlignment[$idx] = ALIGN_RIGHT;
}
else
{
$columnAlignment[$idx] = ALIGN_LEFT;
}
}
$full_table_width += $idx == 0 ? length($verticalBorder) : length($verticalBar);
$full_table_width += $columnsWidth[$idx] + 2*$OptPadding;
}
$full_table_width += length($verticalBorder);
$n_cols = scalar @columnsWidth;
@verticalBars = ($verticalBar) x ($n_cols-1);
if($OptHorizontal)
{
$gridlineTop = $topleftCorner . join($topCross, map {$horizontalBar x ($_+2*$OptPadding)} @columnsWidth) . $toprightCorner;
$gridlineInner = $leftCross . join($middleCross, map {$horizontalBar x ($_+2*$OptPadding)} @columnsWidth) . $rightCross;
$gridlineBottom = $bottomleftCorner . join($bottomCross, map {$horizontalBar x ($_+2*$OptPadding)} @columnsWidth) . $bottomrightCorner;
}
$line_format = '%s'.
join('%s',
map {(' 'x$OptPadding).'%'.$_.'s'.(' 'x$OptPadding)}
map {($columnAlignment[$_] == ALIGN_RIGHT ? 1 : -1) * $columnsWidth[$_]} 0 .. $#columnsWidth
).'%s';
sub print_table
{
my $fh = shift;
my $row_num = 0;
for my $row (@Table)
{
$row_num++;
if($OptHorizontal)
{
print {$fh} ($row_num == 1 ? $gridlineTop : $gridlineInner) . "\n";
}
my @cells = map {$row->[$_] || ''} 0 .. $#columnsWidth;
my @cells_and_inner_borders = zip @cells, @verticalBars;
delete $cells_and_inner_borders[-1];
printf {$fh} $line_format."\n", $verticalBorder, @cells_and_inner_borders, $verticalBorder;
}
if($OptHorizontal)
{
print {$fh} $gridlineBottom . "\n";
}
}
# display the rendered table
if(-t 1 and $ENV{PAGER})
{
my ($terminal_cols, $terminal_rows) = Term::Size::chars(*STDOUT);
if($terminal_cols <= $full_table_width)
{
my ($p_read, $p_write);
pipe($p_read, $p_write) or die "$0: pipe: $!\n";
my $pid = fork;
die "$0: fork: $!\n" if not defined $pid;
if($pid == 0)
{
close $p_read;
print_table($p_write);
close $p_write;
exit 0;
}
open \*STDIN, '<&', $p_read;
close $p_write;
exec {$ENV{PAGER}} [$ENV{PAGER}] or warn "$0: exec: $!\n";
exit 127;
}
}
print_table(\*STDOUT);