-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnate
executable file
·65 lines (59 loc) · 1.64 KB
/
columnate
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
#!/usr/bin/gawk -f
#
# columnate
# break input into columns by width of its longest word, fill to $COLUMNS
#
# todo:
# - shebang magic does not take multiple arguments on linux, so we run
# "gawk -f" but on osx we do not have gawk installed in the same
# location (ie it's from macports)
#
# https://github.com/smemsh/utilsh/
# https://spdx.org/licenses/GPL-2.0
# scott@smemsh.net
#
##############################################################################
BEGIN {
inter_cell_pad = 1
termwidth = ENVIRON["COLUMNS"]
if (!termwidth) exit EXIT_FAILURE
}
# todo: support parsing input on any whitespace, not just lines
# todo: also should support breaking on NUL chars in the input
# todo: support breaking by regex? allow direct input to RS awk variable?
#
{
len = length()
if (len > longest_word)
longest_word = len
records[NR]=$0
}
END {
# how many chars in each column incl at least one char
# padding per column for visual break between columns
#
cellwidth = longest_word + inter_cell_pad
# how many of said cells on each line
# ie, count this many and then emit newline
#
columns = int(termwidth / cellwidth)
# echo each word, emitting newline every 'columns', or
# a column separator, if not a newline
#
for (word = 1; word <= NR; word++) {
text = records[word]
printf("%-*s", longest_word, text)
remaining = int(word % columns)
if (!remaining)
printf("\n")
else for (i = 0; i < inter_cell_pad; i++)
printf(" ")
}
# if we happen to be at the end of a line, we won't
# want a double newline
# todo: there should be a way to do without this test?
# what is the edge case?
#
if (remaining)
printf("\n")
}