-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.sh
216 lines (185 loc) · 5.46 KB
/
menu.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
#!/bin/bash
CHAR__GREEN='\033[0;32m'
CHAR__RED='\033[0;31m'
CHAR__RESET='\033[0m'
menuStr=""
returnOrExit=""
function hideCursor {
printf "\033[?25l"
# capture CTRL+C so cursor can be reset
trap "showCursor && echo '' && ${returnOrExit} 0" SIGINT
}
function showCursor {
printf "\033[?25h"
trap - SIGINT
}
function renderMenu {
local start=0
local selector=""
local instruction="$1"
local selectedIndex=$2
local listLength=$itemsLength
local longest=0
local spaces=""
menuStr="\n $instruction\n"
# Get the longest item from the list so that we know how many spaces to add
# to ensure there's no overlap from longer items when a list is scrolling up or down.
for (( i=0; i<$itemsLength; i++ )); do
if (( ${#menuItems[i]} > longest )); then
longest=${#menuItems[i]}
fi
done
spaces=$(printf ' %.0s' $(eval "echo {1.."$(($longest))"}"))
if [ $3 -ne 0 ]; then
listLength=$3
if [ $selectedIndex -ge $listLength ]; then
start=$(($selectedIndex+1-$listLength))
listLength=$(($selectedIndex+1))
fi
fi
for (( i=$start; i<$listLength; i++ )); do
local currItem="${menuItems[i]}"
currItemLength=${#currItem}
if [[ $i = $selectedIndex ]]; then
currentSelection="${currItem}"
selector="${CHAR__GREEN}ᐅ${CHAR__RESET}"
currItem="${CHAR__GREEN}${currItem}${CHAR__RESET}"
else
selector=" "
fi
currItem="${spaces:0:0}${currItem}${spaces:currItemLength}"
menuStr="${menuStr}\n ${selector} ${currItem}"
done
menuStr="${menuStr}\n"
printf "${menuStr}"
}
function renderHelp {
echo;
echo "Usage: getChoice [OPTION]..."
echo "Renders a keyboard navigable menu with a visual indicator of what's selected."
echo;
echo " -h, --help Displays this message"
echo " -i, --index The initially selected index for the options"
echo " -m, --max Limit how many options are displayed"
echo " -o, --options An Array of options for a user to choose from"
echo " -q, --query Question or statement presented to the user"
echo " -v, --selectionVariable Variable the selected choice will be saved to. Defaults to the 'selectedChoice' variable."
echo;
echo "Example:"
echo " foodOptions=(\"pizza\" \"burgers\" \"chinese\" \"sushi\" \"thai\" \"italian\" \"shit\")"
echo;
echo " getChoice -q \"What do you feel like eating?\" -o foodOptions -i 6 -m 4 -v \"firstChoice\""
echo " printf \"\\n First choice is '\${firstChoice}'\\n\""
echo;
echo " getChoice -q \"Select another option in case the first isn't available\" -o foodOptions"
echo " printf \"\\n Second choice is '\${selectedChoice}'\\n\""
echo;
}
function getChoice {
local KEY__ARROW_UP=$(echo -e "\033[A")
local KEY__ARROW_DOWN=$(echo -e "\033[B")
local KEY__ENTER=$(echo -e "\n")
local captureInput=true
local displayHelp=false
local maxViewable=0
local instruction="Select an item from the list:"
local selectedIndex=0
unset selectedChoice
unset selectionVariable
if [[ "${PS1}" == "" ]]; then
# running via script
returnOrExit="exit"
else
# running via CLI
returnOrExit="return"
fi
if [[ "${BASH}" == "" ]]; then
printf "\n ${CHAR__RED}[ERROR] This function utilizes Bash expansion, but your current shell is \"${SHELL}\"${CHAR__RESET}\n"
$returnOrExit 1
elif [[ $# == 0 ]]; then
printf "\n ${CHAR__RED}[ERROR] No arguments provided${CHAR__RESET}\n"
renderHelp
$returnOrExit 1
fi
local remainingArgs=()
while [[ $# -gt 0 ]]; do
local key="$1"
case $key in
-h|--help)
displayHelp=true
shift
;;
-i|--index)
selectedIndex=$2
shift 2
;;
-m|--max)
maxViewable=$2
shift 2
;;
-o|--options)
menuItems=$2[@]
menuItems=("${!menuItems}")
shift 2
;;
-q|--query)
instruction="$2"
shift 2
;;
-v|--selectionVariable)
selectionVariable="$2"
shift 2
;;
*)
remainingArgs+=("$1")
shift
;;
esac
done
# just display help
if $displayHelp; then
renderHelp
$returnOrExit 0
fi
set -- "${remainingArgs[@]}"
local itemsLength=${#menuItems[@]}
# no menu items, at least 1 required
if [[ $itemsLength -lt 1 ]]; then
printf "\n ${CHAR__RED}[ERROR] No menu items provided${CHAR__RESET}\n"
renderHelp
$returnOrExit 1
fi
# store cursor position
tput sc
renderMenu "$instruction" $selectedIndex $maxViewable
hideCursor
while $captureInput; do
read -rsn3 key # `3` captures the escape (\033'), bracket ([), & type (A) characters.
# restore cursor position and clear menu
tput ed
tput rc
case "$key" in
"$KEY__ARROW_UP")
selectedIndex=$((selectedIndex-1))
(( $selectedIndex < 0 )) && selectedIndex=$((itemsLength-1))
renderMenu "$instruction" $selectedIndex $maxViewable true
;;
"$KEY__ARROW_DOWN")
selectedIndex=$((selectedIndex+1))
(( $selectedIndex == $itemsLength )) && selectedIndex=0
renderMenu "$instruction" $selectedIndex $maxViewable true
;;
"$KEY__ENTER")
captureInput=false
showCursor
if [[ "${selectionVariable}" != "" ]]; then
printf -v "${selectionVariable}" "${currentSelection}"
else
selectedChoice="${currentSelection}"
fi
;;
esac
done
tput ed
tput rc
}