-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.sh
525 lines (419 loc) · 13.7 KB
/
github.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env bash
#===============================================================================
#NAME
# checkbox.sh
#
#DESCRIPTION
# Creates interactive checkboxes (menu) for the terminal
# For more info look the README.md on <https://github.com/pedro-hs/checkbox.sh>
# Features:
# - Select only a option or multiple options
# - Select or unselect multiple options easily
# - Select all or unselect all
# - Pagination
# - Optional Vim keybinds
# - Start with options selected
# - Show selected options counter for multiple options
# - Show custom message
# - Show current option index and options amount
# - Copy current option value to clipboard
# - Help tab when press h or wrongly call the script
#
#SOURCE
# <https://github.com/pedro-hs/checkbox.sh>
#
#INSPIRED BY
# <https://gist.github.com/blurayne/f63c5a8521c0eeab8e9afd8baa45c65e>
# <https://www.bughunter2k.de/blog/cursor-controlled-selectmenu-in-bash>
#
#===============================================================================
# CONTANTS
#===============================================================================
readonly SELECTED="[x]"
readonly UNSELECTED="[ ]"
readonly WHITE="\033[2K\033[37m"
readonly BLUE="\033[2K\033[34m"
readonly RED="\033[2K\033[31m"
readonly GREEN="\033[2K\033[32m"
readonly INTERFACE_SIZE=6
readonly DEFAULT_OPTIONS=( ">>> ChessPZL" ">>> SOFTWARE <<<" ">>> MAINMENU <<<")
#===============================================================================
# VARIABLES
#===============================================================================
cursor=0
options_length=0
terminal_width=0
start_page=0
end_page=0
has_multiple_options=false
will_return_index=false
unselect_mode_on=false
select_mode_on=false
copy_in_message=false
invalid_parameter=false
options=("${DEFAULT_OPTIONS[@]}")
selected_options=()
content=""
message=""
separator=""
options_input=""
color=$WHITE
checkbox_output=()
#===============================================================================
# UTILS
#===============================================================================
array_without_value() {
local value="$1" && shift
local new_array=()
for array in ${@}; do
if [[ $value != $array ]]; then
new_array+=("$array")
fi
done
echo "${new_array[@]}"
}
value_in_array() {
local element="$1" && shift
local elements="$@"
for elements; do
[[ $elements == $element ]] && return 0
done
return 1
}
help_page_opt() {
local output="(press q to quit)\n"
output+="# Avaiable options:\n\n\t--multiple:\n\t\tSelected multiple options\n\t\tExample:\n\t\t\t$ ./checkbox.sh --multiple\n\t--index:\n\t\tReturn index instead of value\n\t\tExample:\n\t\t\t$ ./checkbox.sh --index\n\t--message:\n\t\tCustom message\n\t\tExample:\n\t\t\t$ ./checkbox.sh --message=\"this message will be shown in the header\"\n\t--options:\n\t\tMenu options\n\t\tExample:\n\t\t\t$ ./checkbox.sh --options=\"checkbox 1\n\t\t\tcheckbox 2\n\t\t\tcheckbox 3\n\t\t\tcheckbox 4\n\t\t\tcheckbox 5\""
output+="\n(press q to quit)"
reset_screen
printf "\033[2J\033[?25l%b\n" "$output"
while true; do
local key=$( get_pressed_key )
case $key in
_esc|q) return;;
esac
done
}
help_page_keys() {
local output="(press q to quit)\n"
output+="# Keybinds\n\n\t[ENTER] or o: Close and return selected options\n\t[SPACE] or x: Select current option\n\t[ESC] or q: Exit\n\t[UP ARROW] or k: Move cursor to option above\n\t[DOWN ARROW] or j: Move cursor to option below\n\t[HOME] or g: Move cursor to first option\n\t[END] or G: Move cursor to last option\n\t[PAGE UP] or u: Move cursor 5 options above\n\t[PAGE DOWN] or d: Move cursor 5 options below\n\tc or y: Copy current option\n\tr : Refresh renderization\n\th : Help page"
if $has_multiple_options; then
output+="\n\tA : Unselect all options\n\ta : Select all options\n\t[INSERT] or v: On/Off select options during navigation (select mode)\n\t[BACKSPACE] or V: On/Off unselect options during navigation (unselect mode)"
fi
output+="\n(press q to quit)"
reset_screen
printf "\033[2J\033[?25l%b\n" "$output"
while true; do
local key=$( get_pressed_key )
case $key in
_esc|q) return;;
esac
done
}
#===============================================================================
# AUXILIARY FUNCTIONS
#===============================================================================
handle_options() {
content=""
for index in ${!options[@]}; do
if [[ $index -ge $start_page && $index -le $end_page ]]; then
local option=${options[$index]}
[[ ${options[$cursor]} == $option ]] && set_line_color
handle_option "$index" "$option"
color=$WHITE
fi
done
}
handle_option() {
local index="$1" option="$2"
if value_in_array "$index" "${selected_options[@]}"; then
content+="$color $SELECTED $option\n"
else
content+="$color $UNSELECTED $option\n"
fi
}
set_line_color() {
if $has_multiple_options && $select_mode_on; then
color=$GREEN
elif $has_multiple_options && $unselect_mode_on; then
color=$RED
else
color=$BLUE
fi
}
select_many_options() {
if ! value_in_array "$cursor" "${selected_options[@]}" \
&& $has_multiple_options && $select_mode_on; then
selected_options+=("$cursor")
elif value_in_array "$cursor" "${selected_options[@]}" \
&& $has_multiple_options && $unselect_mode_on; then
selected_options=($( array_without_value "$cursor" "${selected_options[@]}" ))
fi
}
set_options() {
if ! [[ $options_input == "" ]]; then
options=()
local temp_options=$( echo "${options_input#*=}" | sed "s/\\a//g;s/\\b//g;s/\\f//g;s/\\n//g;s/\\r//g;s/\\t//g" )
temp_options=$( echo "$temp_options" | sed "s/|\+/|/g" )
temp_options=$( echo "$temp_options" | tr "\n" "|" )
IFS="|" read -a temp_options <<< "$temp_options"
for index in ${!temp_options[@]}; do
local option=${temp_options[index]}
if [[ ${option::1} == "+" ]]; then
if $has_multiple_options || [[ -z $selected_options ]]; then
selected_options+=("$index")
fi
option=${option:1}
fi
options+=("$option")
done
fi
}
validate_terminal_size() {
if [[ $terminal_width -lt 8 ]]; then
reset_screen
printf "Resize the terminal to least 8 lines and press r to refresh. The current terminal has $terminal_width lines"
fi
}
get_footer() {
local footer="$(( $cursor + 1 ))/$options_length"
if $has_multiple_options; then
footer+=" | ${#selected_options[@]} selected"
fi
if $copy_in_message; then
footer+=" | current line copied"
copy_in_message=false
fi
echo "$footer"
}
get_output() {
terminal_width=$( tput lines )
handle_options
local footer="$( get_footer )"
local output=" $message\n"
output+="$WHITE$separator\n"
output+="$content"
output+="$WHITE$separator\n"
output+=" $footer\n"
echo "$output"
}
#===============================================================================
# KEY PRESS FUNCTIONS
#===============================================================================
toggle_select_mode() {
if $has_multiple_options; then
unselect_mode_on=false
if $select_mode_on; then
select_mode_on=false
else
select_mode_on=true
if ! value_in_array "$cursor" "${selected_options[@]}"; then
selected_options+=("$cursor")
fi
fi
fi
}
toggle_unselect_mode() {
if $has_multiple_options; then
select_mode_on=false
if $unselect_mode_on; then
unselect_mode_on=false
else
unselect_mode_on=true
selected_options=($( array_without_value "$cursor" "${selected_options[@]}" ))
fi
fi
}
select_all() {
if $has_multiple_options; then
selected_options=()
for index in ${!options[@]}; do
selected_options+=(${index})
done
fi
}
unselect_all() {
[[ $has_multiple_options ]] && selected_options=()
}
page_up() {
cursor=$(( $cursor - 5 ))
[[ $cursor -le $start_page ]] \
&& start_page=$(( $cursor - 1 ))
[[ $start_page -le 0 ]] \
&& start_page=0
[[ $cursor -le 0 ]] \
&& cursor=0
end_page=$(( $start_page + $terminal_width - $INTERFACE_SIZE ))
}
page_down() {
cursor=$(( $cursor + 5 ))
[[ $cursor -ge $end_page ]] \
&& end_page=$(( $cursor + 1 ))
[[ $end_page -ge $options_length ]] \
&& end_page=$(( $options_length - 1 ))
[[ $cursor -ge $options_length ]] \
&& cursor=$(( $options_length - 1 ))
start_page=$(( $end_page + $INTERFACE_SIZE - $terminal_width ))
}
up() {
[[ $cursor -gt 0 ]] \
&& cursor=$(( $cursor - 1 ))
[[ $cursor -eq $start_page ]] \
&& start_page=$(( $cursor - 1 ))
[[ $cursor -gt 0 ]] \
&& end_page=$(( $start_page + $terminal_width - $INTERFACE_SIZE ))
select_many_options
}
down() {
[[ $cursor -lt $(( $options_length - 1 )) ]] \
&& cursor=$(( $cursor + 1 ))
[[ $cursor -eq $end_page ]] \
&& end_page=$(( $cursor + 1 ))
[[ $cursor -lt $(( $options_length - 1 )) ]] \
&& start_page=$(( $end_page + $INTERFACE_SIZE - $terminal_width ))
select_many_options
}
home() {
cursor=0
start_page=0
end_page=$(( $start_page + $terminal_width - $INTERFACE_SIZE ))
}
end() {
cursor=$(( $options_length - 1 ))
end_page=$(( $options_length - 1 ))
start_page=$(( $end_page + $INTERFACE_SIZE - $terminal_width ))
}
select_option() {
if ! value_in_array "$cursor" "${selected_options[@]}"; then
$has_multiple_options \
&& selected_options+=("$cursor") \
|| selected_options=("$cursor")
else
selected_options=($( array_without_value "$cursor" "${selected_options[@]}" ))
fi
}
confirm() {
if $will_return_index; then
checkbox_output="${selected_options[@]}"
else
for index in ${!options[@]}; do
if value_in_array "$index" "${selected_options[@]}"; then
case $index in
0) sudo -u batan bash offl_puzzles.sh ;;
1) sudo -u batan bash software.sh ;;
2) sudo -u batan bash first.sh ;;
esac
fi
done
fi
}
copy() {
echo "${options[$cursor]}" | xclip -sel clip
echo "${options[$cursor]}" | xclip
copy_in_message=true
}
refresh() {
terminal_width=$( tput lines )
start_page=$(( $cursor - 1 ))
end_page=$(( $start_page + $terminal_width - $INTERFACE_SIZE ))
}
#===============================================================================
# CORE FUNCTIONS
#===============================================================================
render() {
printf "\033[1;%dH"
printf "\033[2J\033[?25l%b\n" "$(get_output)"
}
reset_screen() {
printf "\033[2J\033[?25h\033[1;%dH"
}
get_pressed_key() {
IFS= read -sn1 key 2>/dev/null >&2
read -sn1 -t 0.0001 k1
read -sn1 -t 0.0001 k2
read -sn1 -t 0.0001 k3
key+="$k1$k2$k3"
case $key in
'') key=_enter;;
' ') key=_space;;
$'\x1b') key=_esc;;
$'\e[F') key=_end;;
$'\e[H') key=_home;;
$'\x7f') key=_backspace;;
$'\x1b\x5b\x32\x7e') key=_insert;;
$'\x1b\x5b\x41') key=_up;;
$'\x1b\x5b\x42') key=_down;;
$'\x1b\x5b\x35\x7e') key=_pgup;;
$'\x1b\x5b\x36\x7e') key=_pgdown;;
esac
echo "$key"
}
get_opt() {
while [[ $# -gt 0 ]]; do
opt=$1
shift
case $opt in
--index) will_return_index=true;;
--multiple) has_multiple_options=true;;
--message=*) message="${opt#*=}";;
--options=*) options_input="$opt";;
*) help_page_opt && invalid_parameter=true;;
esac
done
}
constructor() {
set_options
options_length=${#options[@]}
terminal_width=$( tput lines )
start_page=0
end_page=$(( $start_page + $terminal_width - $INTERFACE_SIZE ))
[[ ${#message} -gt 40 ]] \
&& message_length=$(( ${#message} + 10 )) \
|| message_length=50
separator=$( perl -E "say '-' x $message_length" )
}
#===============================================================================
# MAIN
#===============================================================================
main() {
get_opt "$@"
if $invalid_parameter; then
reset_screen
return
fi
constructor
render
while true; do
validate_terminal_size
local key=$( get_pressed_key )
case $key in
_up|k) up;;
_down|j) down;;
_home|g) home;;
_end|G) end;;
_pgup|u) page_up;;
_pgdown|d) page_down;;
_esc|q) break;;
_enter|o) confirm && break;;
_space|x) select_option;;
_insert|v) toggle_select_mode;;
_backspace|V) toggle_unselect_mode;;
c|y) copy;;
r) refresh;;
a) select_all;;
A) unselect_all;;
h) help_page_keys;;
esac
render
done
reset_screen
if [[ ${#checkbox_output[@]} -gt 0 ]]; then
printf "Selected:\n"
for option in "${checkbox_output[@]}"; do
printf "$option\n"
done
else
printf "None selected\n"
fi
return
}
main "$@"