-
Notifications
You must be signed in to change notification settings - Fork 1
/
tabularize
executable file
·332 lines (267 loc) · 5.89 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
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
#!/bin/bash
true <<EOF
=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 -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 deemed to contain numeric data,
only then aligned to the right.
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
=cut
EOF
TAB=$'\t'
IFS=''
set_ascii()
{
verticalBar='|'
horizontalBar='-'
topleftCorner='+'
toprightCorner='+'
bottomleftCorner='+'
bottomrightCorner='+'
leftCross='+'
middleCross='+'
rightCross='+'
topCross='+'
bottomCross='+'
}
set_unicode()
{
verticalBar='│'
horizontalBar='─'
topleftCorner='┌'
toprightCorner='┐'
bottomleftCorner='└'
bottomrightCorner='┘'
leftCross='├'
middleCross='┼'
rightCross='┤'
topCross='┬'
bottomCross='┴'
}
set_unicode
declare -a columnAlignment=()
horizontalLines=yes
margins=yes
while [ $# -gt 0 ]
do
case "$1" in
-a|--ascii)
set_ascii
;;
-u|--unicode)
set_unicode
;;
-H|--no-horizontal)
horizontalLines=''
;;
-M|--no-margins)
margins=''
;;
-r|--align-right)
shift
columnAlignment[$1]=right
;;
-l|--align-left)
shift
columnAlignment[$1]=left
;;
-v|--output-vertical-separator)
shift
verticalBar=$1
topleftCorner=$1
toprightCorner=$1
bottomleftCorner=$1
bottomrightCorner=$1
leftCross=$1
middleCross=$1
rightCross=$1
topCross=$1
bottomCross=$1
;;
-h|--help)
pod2text "$0"
exit
;;
-*)
echo "$0: unknown option: $1" >&2
exit -1
;;
esac
shift
done
explode()
{
local separator=$1
local string=$2
local new_string
declare -g -a EXPLODE_ARRAY=()
while [ -n "$string" ]
do
EXPLODE_ARRAY+=("${string%%$separator*}")
new_string=${string#*$separator}
if [ "$string" = "$new_string" ]
then
break
fi
string=$new_string
done
# return $EXPLODE_ARRAY
}
is_numeric_check_sep()
{
local thousands_sep=$2
local fraction_sep=$3
[[ $1 =~ ^[+-]?[0-9]+($thousands_sep[0-9]+)*($fraction_sep[0-9]+($thousands_sep[0-9]+)*)?$ ]]
}
is_numeric()
{
is_numeric_check_sep "$1" ',' . && return 0
is_numeric_check_sep "$1" ' ' , && return 0
is_numeric_check_sep "$1" '.' , && return 0
return 1
}
# compute width of each column
column_width=()
num_line=0
text=''
numericals_by_col=()
non_numericals_by_col=()
while read -r line
do
text="$text${text:+$'\n'}$line"
explode $'\t' "$line"
column=0
for cell in "${EXPLODE_ARRAY[@]}"
do
width=${#cell}
if [ -z "${column_width[$column]}" ] || [ $width -gt "${column_width[$column]}" ]
then
column_width[$column]=$width
fi
# guess if this cell has numerical content (skip 1st line as it's likely a header)
if [ $num_line -gt 0 -a -n "$cell" -a -z "${columnAlignment[$column]:-}" ]
then
if is_numeric "$cell"
then
let numericals_by_col[$column]++
else
let non_numericals_by_col[$column]++
fi
fi
let column++
done
let num_line++
done
if [ $num_line = 0 ]
then
# no input, no output
exit 0
fi
set -u
# compose the format string
table_width=0
cellFmt=''
topFmt=''
innerFmt=''
bottomFmt=''
segments=()
sequentColumn=''
for column in "${!column_width[@]}"
do
if [ -z "${columnAlignment[$column]:-}" ]
then
if [ ${numericals_by_col[$column]:-0} -ge ${non_numericals_by_col[$column]:-0} ]
then
columnAlignment[$column]=right
else
columnAlignment[$column]=left
fi
fi
table_width=$[table_width + (column == 0 ? (0${margins:+1} ? ${#verticalBar} : 0) : ${#verticalBar}) + ${column_width[$column]}]
cellFmt="${cellFmt:-${margins:+$verticalBar}}${sequentColumn:+$verticalBar}%*s"
topFmt="${topFmt:-${margins:+$topleftCorner}}${sequentColumn:+$topCross}%${column_width[$column]}s"
innerFmt="${innerFmt:-${margins:+$leftCross}}${sequentColumn:+$middleCross}%${column_width[$column]}s"
bottomFmt="${bottomFmt:-${margins:+$bottomleftCorner}}${sequentColumn:+$bottomCross}%${column_width[$column]}s"
segments+=('')
sequentColumn=yes
done
table_width=$[table_width + (0${margins:+1} ? ${#verticalBar} : 0)]
cellFmt="$cellFmt${margins:+$verticalBar}"
topFmt="$topFmt${margins:+$toprightCorner}"
innerFmt="$innerFmt${margins:+$rightCross}"
bottomFmt="$bottomFmt${margins:+$bottomrightCorner}"
topGrid=''
innerGrid=''
bottomGrid=''
if [ $horizontalLines ]
then
topGrid=`printf -- "$topFmt" "${segments[@]}"`
topGrid=${topGrid// /$horizontalBar}$'\n'
innerGrid=`printf -- "$innerFmt" "${segments[@]}"`
innerGrid=${innerGrid// /$horizontalBar}$'\n'
bottomGrid=`printf -- "$bottomFmt" "${segments[@]}"`
bottomGrid=${bottomGrid// /$horizontalBar}$'\n'
fi
print_table_g()
{
line_no=1
while read -r line
do
if [ $line_no = 1 ]
then
echo -n "$topGrid"
else
echo -n "$innerGrid"
fi
explode $'\t' "$line"
printf_args=()
for column in "${!column_width[@]}"
do
[ $column -lt ${#EXPLODE_ARRAY[@]} ] && cell=${EXPLODE_ARRAY[$column]} || cell=''
chars=${#cell}
bytes=`LANG=C; echo "${#cell}"`
swidth=$[ ${column_width[$column]} + ( $bytes - $chars ) ]
[ ${columnAlignment[$column]} = left ] && swidth=-$swidth
printf_args+=($swidth "$cell")
done
printf -- "$cellFmt\n" "${printf_args[@]}"
let line_no++
done <<<"$text"
echo -n "$bottomGrid"
}
if [ -t 1 -a -n "$PAGER" ]
then
terminal_cols=`tput cols`
if [ "${terminal_cols:-0}" -le $table_width ]
then
print_table_g | exec "$PAGER"
exit ${PIPESTATUS[1]}
fi
fi
print_table_g