blob: 2b55475c28993872d4528603834749d566def142 (
plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
#!/bin/sh
#################################################################
#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████#
#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █#
#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █#
#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █#
#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███#
#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ #
#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████#
# #
# #
# #
# #
# #
# #
# #
#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████#
#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ #
#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███#
#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █#
#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █#
#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █#
#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████#
#################################################################
#include colors
COLOR_FG=${WHITE}
COLOR_FG2=${LIGHT_BLUE}
COLOR_PTRN=${LIGHT_BLUE}
COLOR_BG=${BG_WHITE}
t_init () {
export LINES=$(tput lines)
export COLUMNS=$(tput cols)
t_styl_cur l
tput clear
t_set_tty
t_gen_ptrn
}
t_set_tty () {
export SAVED_TTY_SETTINGS=$(stty -g)
stty raw -echo
}
t_clean () {
stty $SAVED_TTY_SETTINGS
tput clear
t_yes_cur
}
readc () {
stty -echo -icanon min 1 time 0
s="$(dd bs=1 count=1 of=/dev/stdout 2>/dev/null)"
stty -icanon min 0 time 0
[ "$s" = "" ] && {
s="$s$(dd bs=1 count=2 of=/dev/stdout 2>/dev/null)"
}
printf "$s"
}
t_no_cur () {
tput civis
}
t_yes_cur () {
tput cnorm
}
t_set_cur () {
printf "[$1;$2H"
}
max_line_length () {
local max=-1
while IFS= read -r line; do
[ "${#line}" -gt "$max" ] && max=${#line}
done
echo "$max"
}
t_styl_cur () {
case "$1" in
H) printf "\033[2 q";;
H0) printf "\033[1 q";;
l) printf "\033[6 q";;
l0) printf "\033[5 q";;
_) printf "\033[4 q";;
_0) printf "\033[3 q";;
*) printf"\033[0 q]";;
esac > $(tty)
}
t_gen_ptrn () {
export P=""
for l in $(seq 7); do
for c in $(seq $COLUMNS); do
case "$l" in
1) [ "$((c%8))" != "0" ] ;;
2) [ "$((c%8))" = "1" ] || [ "$((c%8))" = "7" ] ;;
3) [ "$((c%2))" = "1" ] || [ "$((c%8))" = "4" ] ;;
4) [ "$((c%2))" = "1" ] ;;
5) [ "$((c%2))" != "0" ] || [ "$((c%8))" = "2" ] ;;
6) [ "$((c%8))" = "5" ] || [ "$((c%8))" = "7" ] ;;
7) [ "$((c%8))" != "6" ] ;;
*) false ;;
esac && P="$P█" || P="$P "
done
P="$P\n"
done
}
t_prnt_ptrn () {
printf "$@$P"
}
t_cls () {
printf "[7;0H"
printf " %.0s" $(seq $(( (LINES-14) * COLUMNS)))
}
t_cls_ptrn () {
printf "[7;0H"
printf " %.0s" $(seq $(( (LINES-14) * COLUMNS)))
t_drw_ptrn
}
t_drw_ptrn () {
t_drw_txt_clr "${COLOR_PTRN}" 1 1 "$P"
t_drw_txt_clr "${COLOR_PTRN}" 1 $((LINES-6)) "$(printf "$P" | rev)"
}
# draws inside the given area
# t_drw_box x y w h
#
t_drw_box () {
local x=$1 y=$2 w=$3 h=$4
l="$(printf "${COLOR_FG2}║"
printf " %.0s" $(seq $((w-2)))
printf "${COLOR_FG2}║"
)"
b="$(printf "═%.0s" $(seq $((w-2))))"
printf "[${y};${x}H${COLOR_FG2}╔$b╗"
for i in $(seq $((y+1)) $((y+h-1))); do
printf "[${i};${x}H$l"
done
printf "[$((y+h-1));${x}H${COLOR_FG2}╚$b╝"
}
# draw text at location
# t_drw_txt x y [txt...]
#
t_drw_txt () {
local x=$1 y=$2
shift 2
echo "$@" | while IFS= read -r line; do
printf "[${y};${x}H$line"
y=$((y+1))
done
}
# draw colored text
#
t_drw_txt_clr () {
local clr=$1 x=$2 y=$3
shift 3
echo "$@" | while IFS= read -r line; do
printf "[${y};${x}H${clr}$line"
y=$((y+1))
done
}
t_msg () {
h_txt_w_h "$@\n"
h_cntr $w $h
t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2))
t_drw_txt_clr "${COLOR_FG}" $x $y "$@"
}
h_drw_btns () {
local sel=$1 i=0
while read -r btn; do
[ "$sel" = "$i" ] && styl="${COLOR_BG}" || styl="${COLOR_FG2}"
h_drw_btn "${styl}" ${btn}
i=$((i+1))
done
}
h_drw_btn () {
styl="$1" x=$2 y=$3 ; shift 3; txt="$@"
t_drw_txt $x $y "${COLOR_FG2}${styl}$txt${RESET}"
}
h_cntr() {
local w=$1 h=$2
x=$(( ( (COLUMNS+w)/2 ) - $1 ))
y=$(( ( (LINES+h)/2 ) - $2 ))
}
h_txt_w_h() {
w=$(printf "$*" | max_line_length)
h=$(printf "$*" | wc -l)
}
h_t_result() {
local s=$1 i=0; shift
for opt in "$@"; do
case " $s " in
*" $i "*) echo "$opt";;
esac
i=$((i+1))
done
}
t_dialog() {
# TODO these are messy af
local msg="$1"; shift
local btns_len="$#" w_btns=$(echo "$*" | wc -c)
h_txt_w_h "$msg\n $*\n"
h_cntr $w $h
local btn_y=$((y+h-1)) \
btn_x=$(( ((COLUMNS+w_btns) / 2) - w_btns))
btns=""
for btn in "$@"; do
btns="$btns$btn_x $btn_y $btn\n"
btn_x=$((btn_x+${#btn}+1))
done
t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2))
t_drw_txt_clr ${COLOR_FG} $x $y "$msg"
local sel="0"
while true; do
printf "$btns" | h_drw_btns $sel
case "$(readc)" in
'
') break;;
'[B'|'[C'|' ')
sel=$(((sel+1)%btns_len));;
'[A'|'[D')
sel=$(((sel+btns_len-1)%btns_len));;
esac
done
export T_RESULT=$(h_t_result "$sel" "$@")
}
t_radio() {
local msg="$1"; shift
local btns_len="$#" w_btns=$(echo "$*" | wc -c)
text=`{
printf "$msg\n"
for b in "$@"; do
printf "╚═$b${RESET}\n"
done
}`
t_msg "$text"
x=$((x+2)) y=$((y+1))
local py=$((y))
btns=""
for btn in "$@"; do
btns="$btns$x $y $btn\n"
y=$((y+1))
done
local sel="0"
while true; do
printf "$btns" | h_drw_btns $sel
case "$(readc)" in
'
') break;;
'[B'|'[C'|' ')
sel=$(((sel+1)%btns_len));;
'[A'|'[D')
sel=$(((sel+btns_len-1)%btns_len));;
esac
done
export T_RESULT=$(h_t_result "$sel" "$@")
}
t_check () {
local msg="$1"; shift
local btns_len="$#"
btns_len=$((btns_len+1))
text=`{
printf "$msg\n"
for b in "$@"; do
printf "[ ] $b${RESET}\n"
done
printf " <OK>${RESET}\n"
}`
t_msg "$text"
x=$((x+4)) y=$((y+1))
local py=$((y))
btns=""
for btn in "$@"; do
btns="$btns$x $y $btn\n"
y=$((y+1))
done
btns="$btns$x $y <OK>\n"
local sel="0" checked=""
while true; do
printf "$btns" | h_drw_btns $sel
case "$(readc)" in
'[B'|'[C'|' ')
sel=$(((sel+1)%btns_len))
;;
'[A'|'[D')
sel=$(((sel+btns_len-1)%btns_len));;
' '|'
')
[ "$((sel+1))" = "$btns_len" ] && break || {
case " $checked " in
*$sel*)
checked=$(echo "$checked" | sed "s/$sel//")
t_drw_txt $((x-3)) $((py+sel)) " "
;;
*)
checked="$checked$sel "
t_drw_txt $((x-3)) $((py+sel)) "x"
;;
esac
sel=$(((sel+1)%btns_len))
};;
esac
done
export T_RESULT=$(h_t_result "$checked" "$@")
}
t_prompt () {
t_dialog "$@" "<ok>"
}
t_yesno () {
t_dialog "$@" "<yes>" "<no>"
[ "$T_RESULT" = "<yes>" ]
}
t_input () {
stty $SAVED_TTY_SETTINGS
t_msg "$@
>" > $(tty)
t_yes_cur
read var
t_set_tty
t_no_cur
export T_RESULT="$var"
}
t_input_hidden () {
stty $SAVED_TTY_SETTINGS
stty -echo
t_msg "$@
>" > $(tty)
t_yes_cur
read var
t_set_tty
t_no_cur
export T_RESULT="$var"
}
t_tail() {
[ -f "$1" ] && file="$1" || file=/dev/stdin
t_drw_txt 0 7 "$( tail -$((LINES-14)) $1)"
}
t_paged_radio () {
perpage=9
heading=$1
shift
T_RESULT="more..."
page=0
max_pages=$((($#/perpage)+1))
while true; do
start=$((page*perpage))
current=$(echo $@ | tr ' ' '\n' | tail -$(($#-start)) | head -$perpage)
current="$current more..."
t_cls_ptrn
t_radio "$heading" $current
[ "$T_RESULT" = "more..." ] && page=$(((page+1)%max_pages)) || return 0
done
}
t_demo () {
t_init
t_no_cur
t_cls_ptrn
t_prompt "Hello world?"
t_cls_ptrn
t_paged_radio "Pick a number" $(seq 34)
t_prompt "You selected the following: $T_RESULT"
t_cls_ptrn
exit 1
t_radio "Pick one:" "toast" "bread" "bread but fishy" "empty sandwich"
t_prompt "You selected the following: $T_RESULT"
t_cls_ptrn
t_check "Please select your order:" "yeast" "fish and chips" "twigs" "tuna" "pizza" "aspic"
t_prompt "You selected the following: $T_RESULT"
t_cls_ptrn
t_dialog "Can a match box?" "<Yes>" "<No>" "<No but a tin can>"
t_prompt "You selected the following: $T_RESULT"
t_cls_ptrn
#t_cls_ptrn
t_input "Enter your name"
t_prompt "Hello $T_RESULT"
t_cls_ptrn
file=$2.sh
[ -f $file ] && {
t_tail $file
} || {
t_prompt "The file $file does not exist"
}
t_prompt "Clear"
t_cls_ptrn
t_prompt "Exit?"
t_clean
}
"$@"
|