-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablewrap
executable file
·33 lines (32 loc) · 939 Bytes
/
tablewrap
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
#!/bin/bash
source /home/fabian/d/programs/bash_scripts/sane
# Mix of "column" and "fold": two-column table whose right side is properly aligned and also line wraps on spaces instead of in the middle of a word. Also works around escape character issues of coreutils ("fold", "wc" etc.).
# Prints further lines with indent 2 if the text can't fit in proper tabulated form.
# find longest left side
for((i=1; i<$#; i+=2)); do
curr=$(len "${!i}")
((l_len=curr>l_len?curr:l_len))
done
((l_len+=2))
r_len=$(($(tput cols)-l_len))
# set alignment width
tabs $l_len
# enough space in terminal for right side?
if ((l_len<$(tput cols))); then
while(($#>0)); do
# print left side
echo -n "$1"
shift
# print right side, folded and aligned
wrap $r_len "$1" | sed "s/^/\\t/g"
shift
done
else
# fallback to indentation 2
while(($#>0)); do
wrap $(($(tput cols)-2)) "$1 $2" | sed "1 ! s/^/ /g"
shift 2
done
fi
# reset tabs
tabs -8