-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.sh
135 lines (113 loc) · 3.27 KB
/
util.sh
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
#!/bin/bash -e
function index-of {
local needle key
local -i i i2 step start
local -n haystack ret
needle=$1
haystack=$2
ret=$3
start=${4:-0}
step=${5:-1}
i2=-1
for (( i=start; i<${#haystack[@]}; i+=step )); do
i2=$(( i2 + 1 ))
key="${haystack[i]}"
if [[ "$key" == "$needle" ]]; then
ret=$i2
return 0
fi
done
# shellcheck disable=SC2034
ret=-1
return 0
}
function debug-line {
if [[ "$DEBUG" == "1" ]]; then
local line i c
line="$1"
i="$2"
c="${3:-^}"
printf '%s\n' "$line"
if ! test -z "$i"; then
printf "% $((i + 1))s%s\n" "$c"
fi
fi
}
function debug-line-single {
local old line
line="$1"
if ! test -z "$DEBUG_LINE" && grep -q "$DEBUG_LINE" <<< "$line"; then
old=$DEBUG
DEBUG=1
debug-line "$@"
DEBUG=$old
fi
}
function debug-printf {
if [[ "$DEBUG" == "1" ]]; then
# shellcheck disable=SC2059
printf "$@"
fi
}
function debug-line-printf {
local old line
line="$1"
shift
if ! test -z "$DEBUG_LINE" && grep -q "$DEBUG_LINE" <<< "$line"; then
old=$DEBUG
DEBUG=1
debug-printf "$@"
DEBUG=$old
fi
}
function print-table-header {
# shellcheck disable=SC2154
if ! declare -p column_spec 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "column_spec" variable must be declared as an array before invoking print-table-header\n' >&2
return 1
fi
local work line header column width i
for ((i = 0; i < ${#column_spec[@]}; i += 2 )); do
column="${column_spec[i]}"
width="${column_spec[i+1]}"
# shellcheck disable=SC2046 disable=SC1083 disable=SC2175
printf -v work -- '%0.1s' $(eval echo "-"{0..$((width + 1))});
line+="+$work"
# shellcheck disable=SC2046 disable=SC1083 disable=SC2175
printf -v work -- "| % ${width}s " "$column"
header+="$work"
done
printf -- '%s+\n' "$line"
printf -- '%s|\n' "$header"
printf -- '%s+\n' "$line"
}
function print-table-row {
# shellcheck disable=SC2154
if ! declare -p column_spec 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "column_spec" variable must be declared as an array before invoking print-table-row\n' >&2
return 1
fi
local work row column width i
for (( i = 0; i < ${#column_spec[@]}; i += 2 )); do
column="${column_spec[i]}"
width="${column_spec[i+1]}"
# shellcheck disable=SC2046 disable=SC1083 disable=SC2175
printf -v work -- "| % ${width}s " "$column"
row+="$work"
done
printf -- '%s|\n' "$row"
}
function print-table-hr {
if ! declare -p column_spec 2> /dev/null | grep -q 'declare \-a'; then
printf 'The "column_spec" variable must be declared as an array before invoking print-table-hr\n' >&2
return 1
fi
local work line width i
for ((i = 0; i < ${#column_spec[@]}; i += 2 )); do
width="${column_spec[i+1]}"
# shellcheck disable=SC2046 disable=SC1083 disable=SC2175
printf -v work -- '%0.1s' $(eval echo "-"{0..$((width + 1))});
line+="+$work"
done
printf -- '%s+\n' "$line"
}