-
Notifications
You must be signed in to change notification settings - Fork 2
/
table
executable file
·79 lines (71 loc) · 2.78 KB
/
table
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
#!/usr/bin/gawk -E
#
# A small program to generate good looking tables from text data,
# powered by all the field splitting magic that awk provides. Type
# `table --help` for usage details.
#
# This is a user interface for the libtable.awk library, which can be
# used from other programs.
#
# Depends on ngetopt.awk for command line option parsing.
#
# Written by Joep van Delft, 2014, 2015, 2019
@include "ngetopt.awk"
@include "libtable"
@include "walkarray"
function display_version( p) {
split(ENVIRON["_"], p, "/")
return sprintf("%s version %s", p[length(p)], "v0.3.0")
}
BEGIN {
#opt_debug="y"
regopt("short=s; long=style; flag=style; has_arg=yes; vals=rst|psql|md|jira; desc=Select output style.")
regopt("short=d; long=debug; flag=debug; has_arg=no; desc=Output debugging stuff")
regopt("short=h; long=help; flag=help; has_arg=no; desc=Show usage information")
regopt("short=v; long=version; flag=version; has_arg=no; desc=Show version information")
regopt("short=F; long=field-separator; flag=FS; has_arg=yes; desc=Sets the field separator")
regopt("short=R; long=record-separator; flag=RS; has_arg=yes; desc=Sets the record separator")
regopt("short=H; long=no-header; flag=header; val=no; has_arg=no; desc=Disable header")
regopt("short=F; long=field-separator; flag=FS; has_arg=yes; desc=Sets the field separator")
regopt("short=T; long=title; flag=title; has_arg=yes; desc=Set argument as title")
regopt("long=rst; flag=style; val=rst; has_arg=no; desc=Style reStructuredText")
regopt("long=psql; flag=style; val=psql; has_arg=no; desc=Style Unicode psql")
regopt("long=md; flag=style; val=md; has_arg=no; desc=Style markdown")
regopt("long=jira; flag=style; val=jira; has_arg=no; desc=Style jira")
regopt("long=left-margin; flag=_table_left_margin; has_arg=yes; desc=Sets the left margin")
parseopt(opt)
if (version == "yes") {
_assert_exit = 0
print display_version()
exit
}
if (help == "yes") {
print display_version()
print usage()
_assert_exit = 0
exit
}
if ((style == "md" || style == "jira") && length(title)>0) {
printf("Title is not supported with style %s. Ignoring.\n", style)>"/dev/stderr"
}
offset = 0
}
{
# Storing the records in a two-dimensional array `contents'.
# Ignore empty lines, while respecting the demand for consecutive indexes:
if (NF == 0) {
offset++
next
}
# Some silly trick to convince `split' that `contents[NR]' really
# is an array.
contents[NR - offset]["a"]=1
split($0, contents[NR - offset])
delete contents[NR - offset]["a"]
}
END {
printf make_table(contents)
if (debug == "yes")
walk_array(contents, "contents")
}
# vim: ts=4 sw=4 sts=4 et