-
Notifications
You must be signed in to change notification settings - Fork 3
/
multiselect.sh
executable file
·161 lines (145 loc) · 4.48 KB
/
multiselect.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
#!/usr/bin/env bash
####################### DESCRIPTION: #######################
#
# multiselect is a pure bash implementation of a multi
# selection menu.
#
# If "true" is passed as first argument a help (similar to
# the overview in section "USAGE") will be printed before
# showing the options. Any other value will hide it.
#
# The result will be stored as an array in a variable
# that is passed to multiselect as second argument.
#
# The third argument takes an array that contains all
# available options.
#
# The last argument is optional and can be used to
# preselect certain options. If used it must be an array
# that has a value of "true" for every index of the options
# array that should be preselected.
#
########################## USAGE: ##########################
#
# j or ↓ => down
# k or ↑ => up
# ⎵ (Space) => toggle selection
# ⏎ (Enter) => confirm selection
#
######################### EXAMPLE: #########################
#
# source <(curl -sL multiselect.miu.io)
#
# my_options=( "Option 1" "Option 2" "Option 3" )
# preselection=( "true" "true" "false" )
#
# multiselect "true" result my_options preselection
#
# idx=0
# for option in "${my_options[@]}"; do
# echo -e "$option\t=> ${result[idx]}"
# ((idx++))
# done
#
############################################################
function multiselect {
if [[ $1 = "true" ]]; then
echo -e "j or ↓\t\t=> down"
echo -e "k or ↑\t\t=> up"
echo -e "⎵ (Space)\t=> toggle selection"
echo -e "⏎ (Enter)\t=> confirm selection"
echo
fi
# little helpers for terminal print control and key input
ESC=$(printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_inactive() { printf "$2 $1 "; }
print_active() { printf "$2 $ESC[7m $1 $ESC[27m"; }
get_cursor_row() {
IFS=';' read -sdR -p $'\E[6n' ROW COL
echo ${ROW#*[}
}
local return_value=$2
eval "local -a options=(\"\${"$3"[@]}\")"
eval "local -a defaults=(\"\${"$4"[@]}\")"
local selected=()
for ((i = 0; i < ${#options[@]}; i++)); do
if [[ ${defaults[i]} = "true" ]]; then
selected+=("true")
else
selected+=("false")
fi
printf "\n"
done
# determine current screen position for overwriting the options
local lastrow=$(get_cursor_row)
local startrow=$(($lastrow - ${#options[@]}))
# ensure cursor and input echoing back on upon a ctrl+c during read -s
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
cursor_blink_off
key_input() {
local key
IFS= read -rsn1 key 2>/dev/null >&2
if [[ $key = "" ]]; then echo enter; fi
if [[ $key = $'\x20' ]]; then echo space; fi
if [[ $key = "k" ]]; then echo up; fi
if [[ $key = "j" ]]; then echo down; fi
if [[ $key = $'\x1b' ]]; then
read -rsn2 key
if [[ $key = [A || $key = k ]]; then echo up; fi
if [[ $key = [B || $key = j ]]; then echo down; fi
fi
}
toggle_option() {
local option=$1
if [[ ${selected[option]} == true ]]; then
selected[option]=false
else
selected[option]=true
fi
}
print_options() {
# print options by overwriting the last lines
local idx=0
for option in "${options[@]}"; do
local prefix="[ ]"
if [[ ${selected[idx]} == true ]]; then
prefix="[\e[38;5;46m✔\e[0m]"
fi
cursor_to $(($startrow + $idx))
if [ $idx -eq $1 ]; then
print_active "$option" "$prefix"
else
print_inactive "$option" "$prefix"
fi
((idx++))
done
}
local active=0
while true; do
print_options $active
# user key control
case $(key_input) in
space) toggle_option $active ;;
enter)
print_options -1
break
;;
up)
((active--))
if [ $active -lt 0 ]; then active=$((${#options[@]} - 1)); fi
;;
down)
((active++))
if [ $active -ge ${#options[@]} ]; then active=0; fi
;;
esac
done
# cursor position back to normal
cursor_to $lastrow
printf "\n"
cursor_blink_on
eval $return_value='("${selected[@]}")'
}