forked from jdpedersen1/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VTable
executable file
·464 lines (406 loc) · 15.3 KB
/
VTable
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/perl
#===============================================================================
#
# FILE: tab
#
# USAGE: "tab" or "untab"
#
# DESCRIPTION: This will turn comma seperated input from stdin into a text table.
# It can also then convert it back if program is invoked as "untab".
#
# As well as being used on the command line, tab/untab can be
# used from within vi, and can work on tables that are commented
# out with # or //.
#
# OPTIONS:
# -------------------------------------------------------------------------
# | Command/Option | Purpose |
# |----------------|------------------------------------------------------|
# | tab | Reads from stdin and tabulates comma seperated input |
# | tab <-t> | Tabulates input and assumes first row are titles |
# | tab <-h> | Prints this help |
# | tab <-nb> | Tabulates without a border |
# | tab <-fw X> | Wrap fields greater than X big don't break words |
# | tab <-fs X> | Wrap fields greater than X big and break words |
# | tab <-vp X> | Vertically pad table by X lines |
# | tab <-hp X> | Horizontally pad fields by X chars |
# | tab <-b X> | Tabulates with a border made from char X |
# |----------------|------------------------------------------------------|
# | untab | Reads from stdin and untabulates table input |
# | untab <-b X> | Untabulate a table with border char X |
# | untab <-nb> | Untabulate a borderless table |
# -------------------------------------------------------------------------
#
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Ben Staniford (BTS), <ben.staniford@g nospam mai l.com>
# COMPANY:
# VERSION: 1.0
# CREATED: 23/08/07 11:53:19 BST
# REVISION: ---
#===============================================================================
# TODO
# 1. Make tab and untab keep existing indentation including inside comments
# 2. Store the comment regexp only once in a global variable
# 3. Allow facility to set the delimiter on the command line
use strict;
use warnings;
#Default values (Normally set from cmd line)
my $HPADDING=1; #How much horizontal padding
my $VPADDING=0; #How much vertical padding
my $VBORDER="|"; #What is our vertical border?
my $HBORDER="-"; #What is our horizontal border/divider?
my $wrapped_line_vpad=1; #Should we vertically pad fields that have been word wrapped?
my $break_words_on_wrap=1; #Should we break words when wrapping
my $field_wrap_boundary=0; #How big should a field be before we wrap it?
#Globals
my @max_field_sizes=();
my $max_col_count=0;
my $comment_char="";
my $titles=0;
#Funcs
sub tabulate();
sub get_fields($);
sub print_header_footer();
sub add_padding($);
sub untabulate();
sub add_field_empty_space($$);
sub print_usage();
sub wrap_oversized_fields(@);
sub print_table_divider($);
#No STDERR under any circumstances
open (STDERR, ">/dev/null");
#Arguments
my $args = join ' ', @ARGV;
if ($args =~ /-t/) { $titles=1; }
if ($args =~ /-nb/) { $VBORDER=""; $HBORDER=""; $HPADDING=2;}
if ($args =~ /-b\s+(\S)/) { $VBORDER=$1; $HBORDER=$1; $HPADDING=1;}
if ($args =~ /-fs\s+(\S+)/) { $field_wrap_boundary=$1; $break_words_on_wrap=0;}
if ($args =~ /-fw\s+(\S+)/) { $field_wrap_boundary=$1; $break_words_on_wrap=1;}
if ($args =~ /-vp\s+(\S+)/) { $VPADDING=$1}
if ($args =~ /-hp\s+(\S+)/) { $HPADDING=$1}
elsif ($args =~ /-h/) { print_usage(); exit 0; }
#If we're invoked as "untab", i.e. via symlink, do the inverse of normal behavior
#Note, untab uses most of the same program arguments above.
chomp(my $PROGRAM_NAME = `basename $0`);
if ($PROGRAM_NAME eq "untab") {
untabulate();
} else {
tabulate();
}
exit 0;
# ------------------------------------------------------------------
# | Name | Purpose | Parameters |
# |--------------|------------------------------------|------------|
# | tabulate() | Main function that tabulates stdin | |
# ------------------------------------------------------------------
sub tabulate() {
#Step 1, load the data into a list
my @table=<STDIN>;
#Step 2, If we have field length restrictions, reorder the table as
#needed. Note, this can't be untabbed.
if ($field_wrap_boundary != 0) {
@table = wrap_oversized_fields(@table);
}
#Step 3, calculate the number of rows and columns from the input as well as the
#maximum field size for each column. Also, work out if this table is in a comment.
for my $line (@table) {
chomp $line;
my @fields = get_fields($line);
my $counter=0;
#Work out if the data is inside a comment
if ($counter==0 && $line=~/^(\#|\/\/)/) {
$comment_char=$1;
}
$line =~ s/^$comment_char//;
for my $field (@fields) {
if (!defined $max_field_sizes[$counter]) {
$max_field_sizes[$counter] = 0;
}
if ($max_field_sizes[$counter] < length($field)) {
$max_field_sizes[$counter] = length($field);
}
$counter++;
}
if ($counter > $max_col_count) {
$max_col_count=$counter;
}
}
#Step 4, print out the table
print_header_footer();
my $lcounter=0;
for my $line (@table) {
chomp $line;
my @fields = get_fields($line);
if ($comment_char ne "") {
print "$comment_char ";
}
if ($VBORDER ne "") {
print $VBORDER.add_padding(" ");
}
$lcounter++;
my $counter=0;
#Print fields
for my $field (@fields) {
print "$field".add_field_empty_space(length($field), $counter).add_padding(" ");
print $VBORDER.add_padding(" ");
$counter++;
}
#Print any empty fields (if they exist)
if ($counter < $max_col_count) {
for (my $i=0;$i<($max_col_count - $counter);$i++) {
print add_field_empty_space(0, $counter+$i).add_padding(" ");
print $VBORDER.add_padding(" ");
}
}
print "\n";
if ($VPADDING==1) {
print_table_divider(" ");
}
if ($titles && $lcounter==1) {
#print_header_footer();
print_table_divider($HBORDER);
}
}
print_header_footer();
}
# -----------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# |-----------------------|------------------------|--------------------------|
# | print_table_divider() | Print out a divider in | Character divider should |
# | | the table | made from |
# -----------------------------------------------------------------------------
sub print_table_divider($) {
my $divider_char = shift;
if ($divider_char eq $HBORDER && $HBORDER eq "") {
return;
}
if ($comment_char ne "") {
print "$comment_char ";
}
for my $size (@max_field_sizes) {
print $VBORDER.add_padding($divider_char);
for (my $i=0;$i<$size;$i++) {
print $divider_char;
}
print "".add_padding($divider_char);
}
print $VBORDER."\n";
}
# ----------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# ----------------------------------------------------------------------------
# | print_header_footer() | Print out the tables header/footer | |
# ----------------------------------------------------------------------------
sub print_header_footer() {
my $divider_char = $HBORDER;
if ($divider_char eq $HBORDER && $HBORDER eq "") {
return;
}
if ($comment_char ne "") {
print "$comment_char ";
}
for my $size (@max_field_sizes) {
print $HBORDER.add_padding($divider_char);
for (my $i=0;$i<$size;$i++) {
print $divider_char;
}
print "".add_padding($divider_char);
}
print $HBORDER."\n";
}
# ------------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# ------------------------------------------------------------------------------
# | add_field_empty_space() | Print out the field spacer | Field Length (int) |
# | | | Field Number (int) |
# ------------------------------------------------------------------------------
sub add_field_empty_space($$) {
my $ret="";
my $field_length=shift;
my $field_number=shift;
my $empty_space_size=$max_field_sizes[$field_number] - $field_length;
for (my $i=0;$i<$empty_space_size;$i++) {
$ret.=" ";
}
return $ret;
}
# -----------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# |---------------|------------------------------|----------------------------|
# | add_padding | Print out the padding string | Padding character (string) |
# -----------------------------------------------------------------------------
sub add_padding($) {
my $padding_char = shift;
my $ret="";
for (my $i=0;$i<$HPADDING;$i++) {
$ret.=$padding_char;
}
return $ret;
}
# -----------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# |--------------|--------------------------------------|---------------------|
# | get_fields | Extract a list of fields from a line | Input line (string) |
# -----------------------------------------------------------------------------
sub get_fields($) {
my $line=shift;
my @fields = split ',',$line;
my @ret=();
for my $field (@fields) {
$field =~ s/^\s*//;
$field =~ s/\s*$//;
push @ret, $field;
}
return @ret;
}
# -----------------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# |----------------|---------------------------------------------------|------------|
# | untabulate() | Perform the inverse function and untabulate stdin | |
# -----------------------------------------------------------------------------------
sub untabulate() {
my $counter=0;
while (<STDIN>) {
chomp;
#Work out if the data is inside a comment
if ($counter==0 && $_=~/^(\#|\/\/)/) {
$comment_char=$1;
}
#Handle a borderless table specifically
if ($HBORDER eq "" && $VBORDER eq "") {
s/\s{2,200}/,/g;
s/,$//;
#This is a table with a border
} else {
my $hb_regexp="\\$HBORDER";
my $vb_regexp="\\$VBORDER";
s/^$hb_regexp*$//g;
s/^(?:$hb_regexp|$vb_regexp)*$//;
s/^$comment_char\s*$hb_regexp*$//g;
s/\s*$vb_regexp\s*/,/g;
}
s/^$comment_char\,/$comment_char/;
for (my $i=0;$i<20;$i++) {
s/^\,//;
s/\,$//;
}
s/,/, /g; #If you want spaces as the default after commas
if ($_ !~ /$comment_char\s/) {
s/$comment_char/$comment_char /;
}
if ($_ !~ /^\s*$/) {
print "$_\n";
}
$counter++;
}
}
# ----------------------------------------------------------------------------------------
# | Name | Purpose | Parameters |
# |---------------------------|------------------------------------------|---------------|
# | wrap_oversized_fields() | Wrap fields that are more than specified | List of lines |
# | | size. This works by rewriting the | |
# | | comma seperated data so that extra lines | |
# | | are made. For this reason this | |
# | | function cannot easily be undone by " | |
# | | untab" | |
# ----------------------------------------------------------------------------------------
sub wrap_oversized_fields(@) {
my @table=@_;
my @ret;
#Go through each line in the table
for my $line (@table) {
my @overflow_buffer=();
chomp $line;
my $lcounter=0;
#Work out if the data is inside a comment
if ($lcounter==0 && $line=~/^(\#|\/\/)/) {
$comment_char=$1;
}
$line =~ s/^$comment_char//;
my @fields = get_fields($line);
my @overflow_fields = ();
my $fcounter = 0;
#Go through fields in each line looking for fields that must be split
for my $field (@fields) {
if (length($field) > $field_wrap_boundary) {
my $temp;
#Wrap and preserve words
if ($break_words_on_wrap) {
while ($field =~ /^(.{0,$field_wrap_boundary}\b)/) {
$overflow_fields[$fcounter].=",$1";
$field=$';
}
#Wrap and split words
} else {
while ($temp = substr($field, 0, $field_wrap_boundary)) {
$temp =~ s/^\s*//;
$overflow_fields[$fcounter].=",$temp";
$field=substr($field, $field_wrap_boundary,length($field));
}
}
} else {
$overflow_fields[$fcounter]=$field;
}
$fcounter ++;
}
#Build the extra lines that must be put back into @table using the
#@overflow_fields table
$fcounter=0;
my $keep_processing=1;
while ($keep_processing) {
$keep_processing=0;
my $counter = 0;
for (@overflow_fields) {
my $field, my $remainder;
if( ($field, $remainder) = /^,(.*?)(,.*)$/) {
my $a=1;
} else {
$field=$_;
$field=~s/^,//;
}
if ($field ne "") { $keep_processing = 1; }
#Put any extra lines we make into the overflow buffer so that
#that can be added into our return result.
$overflow_buffer[$fcounter].=",$field";
$overflow_fields[$counter]=$remainder;
$counter++;
}
$fcounter++;
}
#Put the contents of the overflow buffer into our return result
for $line (@overflow_buffer) {
#print "OB: $line\n";
$line =~ s/^,//;
if ($line !~ /^,*$/) {
push @ret, $line;
} elsif ($wrapped_line_vpad==1) {
push @ret, $line;
}
}
$lcounter++;
}
return @ret;
}
sub print_usage() {
print <<END;
-------------------------------------------------------------------------
| Command/Option | Purpose |
|----------------|------------------------------------------------------|
| tab | Reads from stdin and tabulates comma seperated input |
| tab <-t> | Tabulates input and assumes first row are titles |
| tab <-h> | Prints this help |
| tab <-nb> | Tabulates without a border |
| tab <-fw X> | Wrap fields greater than X big don't break words |
| tab <-fs X> | Wrap fields greater than X big and break words |
| tab <-vp X> | Vertically pad table by X lines |
| tab <-hp X> | Horizontally pad fields by X chars |
| tab <-b X> | Tabulates with a border made from char X |
|----------------|------------------------------------------------------|
| untab | Reads from stdin and untabulates table input |
| untab <-b X> | Untabulate a table with border char X |
| untab <-nb> | Untabulate a borderless table |
-------------------------------------------------------------------------
END
}