-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabom_lib.bash
226 lines (195 loc) · 3.38 KB
/
abom_lib.bash
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
get_term_size() {
shopt -s checkwinsize; (:;:)
}
trap 'get_term_size' WINCH
hide_cursor() {
printf '\e[?25l'
}
show_cursor() {
printf '\e[?25h'
}
get_cursor_pos() {
local pos
# shellcheck disable=SC2034
read -p $'\e[6n' -r -s -d R pos
# keysmash protection
local pos_regex="^[0-9]+.[0-9]+"
while true; do
if [[ ${pos:0:1} != $'\e' ]]; then
pos=${pos:1}
continue
fi
if [[ ! ${pos:2} =~ $pos_regex ]]; then
pos=${pos:1}
continue
fi
break
done
local len=$((${#pos} - 2))
echo "${pos:2:$len}"
}
get_line_from_pos() {
local pos=$1
echo "${pos%%;*}"
}
get_column_from_pos() {
local pos=$1
echo "${pos##*;}"
}
# shellcheck disable=SC2120
prev_line_start() {
local num=${1:-1}
printf '\e[%dF' "$num"
}
# shellcheck disable=SC2120
next_line_start() {
local num=${1:-1}
printf '\e[%dE' "$num"
}
# shellcheck disable=SC2120
prev_line() {
local num=${1:-1}
printf '\e[%dA' "$num"
}
# shellcheck disable=SC2120
next_line() {
local num=${1:-1}
printf '\e[%dB' "$num"
}
delete_to_eol() {
printf '\e[0K'
}
TUI_TAB=$(printf '\t')
TUI_NL=$(printf '\n')
TUI_ESC=$(printf '\e')
read_key() {
local c1
IFS= read -r -s -n 1 c1
if [[ $c1 != "$TUI_ESC" ]]; then
case $c1 in
$TUI_TAB)
echo "_tab"
;;
$TUI_NL)
echo "_nl"
;;
$'\177') # https://unix.stackexchange.com/a/244617
echo "_bs"
;;
"")
echo "_?"
;;
*)
echo "$c1"
;;
esac
else
local cc
local cc2
read -r -s -n 2 cc
read -s -r -n 1 -t 0.001 cc2
if [[ -n $cc2 ]]; then
cc="${cc}${cc2}"
fi
case $cc in
'[A')
echo "_up"
;;
'[B')
echo "_down"
;;
'[C')
echo "_right"
;;
'[D')
echo "_left"
;;
'[H')
echo "_home"
;;
'[F')
echo "_end"
;;
'[Z')
echo "_shift_tab"
;;
'[5~')
echo "_pgup"
;;
'[6~')
echo "_pgdn"
;;
'[2~')
echo "_ins"
;;
'[3~')
echo "_del"
;;
*)
echo "_?"
;;
esac
fi
}
TUI_LINES=
abom_init() {
if [[ -n $TUI_LINES ]]; then
return 1
fi
local lines=${1:-$LINES}
local init_content=$2
get_term_size
hide_cursor
TUI_LINES=$lines
local i
for (( i = 0; i < TUI_LINES; i++ )); do
echo
done
prev_line_start "$TUI_LINES"
abom_render "$init_content"
trap abom_close SIGINT SIGQUIT
return 0
}
abom_clear() {
if [[ -z $TUI_LINES ]]; then
return
fi
local i
for (( i = 0; i < TUI_LINES; i++ )); do
delete_to_eol
next_line_start
done
prev_line_start "$TUI_LINES"
}
abom_render() {
if [[ -z $TUI_LINES ]]; then
return 1
fi
local content=$1
abom_clear
local root_pos
root_pos=$(get_cursor_pos)
local root_line
root_line=$(get_line_from_pos "$root_pos")
printf "%s" "$content"
local curr_pos=
curr_pos=$(get_cursor_pos)
local diff_lines
diff_lines=$(( $(get_line_from_pos "${curr_pos}") - root_line ))
local i
for (( i = 0; i < TUI_LINES - diff_lines; i++ )); do
delete_to_eol
next_line_start
done
prev_line_start "$s"
return 0
}
abom_close() {
if [[ -n $TUI_LINES ]]; then
#abom_clear
printf '\e[%dB' "$TUI_LINES"
show_cursor
trap - SIGINT SIGQUIT
TUI_LINES=
fi
}