-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbash-menu.sh
276 lines (218 loc) · 6.51 KB
/
bash-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
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
#!/bin/bash
#
# Public Functions
#
# menuInit()
# menuLoop()
#
#
# Public Variables to Override
#
# Should these get passed into menuInit() rather than be set as global
# script variables?
#
# menuTop # Top row of menu (defaults to row 2)
# menuLeft # Left offset for menu item text (defaults to column 15)
# menuWidth # Width of menu (defaults to 42 columns)
# menuMargin # Left offset for menu border (defaults to column 4)
#
# menuColour # Colour of menu text (defaults to DRAW_COL_WHITE)
# menuHighlight # Highlight colour for menu (defaults to DRAW_COL_GREEN)
#
# menuTitle # Title of menu
# menuFooter # Footer text of menu
#
# menuItems # Array containing menu item text
# menuActions # Array containing functions to call upon menu item selection
#
# Ensure we are running under bash (will not work under sh or dash etc)
if [ "$BASH_SOURCE" = "" ]; then
echo "ERROR: bash-menu requires to be running under bash"
exit 1
fi
# Get script root (as we are sourced from another script, $0 will not be us)
declare -r menuScript=$(readlink -f ${BASH_SOURCE[0]})
menuRoot=$(dirname "$menuScript")
# Ensure we can access our dependencies
if [ ! -s "$menuRoot/bash-draw.sh" ]; then
echo "ERROR: Missing required draw.sh script"
exit 1
fi
# Load terminal drawing functions
. "$menuRoot/bash-draw.sh"
################################
# Private Variables
#
# These should not be overridden
################################
declare -a menuItems
declare -a menuActions
menuHeaderText=""
menuFooterText=""
menuBorderText=""
################################
# Setup Menu
#
# These are defaults which should
# be overridden as required.
################################
# Top of menu (row 2)
menuTop=2
# Left offset for menu items (not border)
menuLeft=15
# Width of menu
menuWidth=42
# Left offset for menu border (not menu items)
menuMargin=4
menuItems[0]="Exit"
menuActions[0]="return 0"
menuItemCount=1
menuLastItem=0
menuColour=$DRAW_COL_WHITE
menuHighlight=$DRAW_COL_GREEN
menuTitle=" Super Bash Menu System"
menuFooter=" Enter=Select, Up/Down=Prev/Next Option"
################################
# Initialise Menu
################################
menuInit() {
menuItemCount=${#menuItems[@]}
menuLastItem=$((menuItemCount-1))
# Ensure header and footer are padded appropriately
menuHeaderText=`printf "%-${menuWidth}s" "$menuTitle"`
menuFooterText=`printf "%-${menuWidth}s" "$menuFooter"`
# Menu (side) borders
local marginSpaces=$((menuMargin-1))
local menuSpaces=$((menuWidth-2))
local leftGap=`printf "%${marginSpaces}s" ""`
local midGap=`printf "%${menuSpaces}s" ""`
menuBorderText="${leftGap}x${midGap}x"
}
################################
# Show Menu
################################
menu_Display() {
local menuSize=$((menuItemCount+2))
local menuEnd=$((menuSize+menuTop+1))
drawClear
drawColour $menuColour $menuHighlight
# Menu header
drawHighlightAt $menuTop $menuMargin "$menuHeaderText" 1
# Menu (side) borders
for row in $(seq 1 $menuSize); do
drawSpecial "$menuBorderText" 1
done
# Menu footer
drawHighlightAt $menuEnd $menuMargin "$menuFooterText" 1
# Menu items
for item in $(seq 0 $menuLastItem); do
menu_ClearItem $item
done
}
################################
# Mark Menu Items
################################
# Ensure menu item is not highlighted
menu_ClearItem() {
local item=$1
local top=$((menuTop+item+2))
local menuText=${menuItems[$item]}
drawPlainAt $top $menuLeft "$menuText"
}
# Highlight menu item
menu_HighlightItem() {
local item=$1
local top=$((menuTop+item+2))
local menuText=${menuItems[$item]}
drawHighlightAt $top $menuLeft "$menuText"
}
################################
# Wait for and process user input
################################
menu_HandleInput() {
local choice=$1
local after=$((choice+1))
[[ $after -gt $menuLastItem ]] && after=0
local before=$((choice-1))
[[ $before -lt 0 ]] && before=$menuLastItem
# Clear highlight from prev/next menu items
menu_ClearItem $before
menu_ClearItem $after
# Highlight current menu item
menu_HighlightItem $choice
# Get keyboard input
local key=""
local extra=""
read -s -n1 key 2> /dev/null >&2
while read -s -n1 -t .05 extra 2> /dev/null >&2 ; do
key="$key$extra"
done
# Handle known keys
local escKey=`echo -en "\033"`
local upKey=`echo -en "\033[A"`
local downKey=`echo -en "\033[B"`
if [[ $key = $upKey ]]; then
return $before
elif [[ $key = $downKey ]]; then
return $after
elif [[ $key = $escKey ]]; then
if [[ $choice -eq $menuLastItem ]]; then
# Pressing Esc while on last menu item will trigger action
# This is a helper as we assume the last menu option is exit
key=""
else
# Jumping possibly more than 1 (next/prev) item
menu_ClearItem $choice
return $menuLastItem
fi
elif [[ ${#key} -eq 1 ]]; then
# See if we wanrt to jump to a menu item
# by entering the first character
for index in $(seq 0 $menuLastItem) ; do
local item=${menuItems[$index]}
local startChar=${item:0:1}
if [[ "$key" = "$startChar" ]]; then
# Jumping possibly more than 1 (next/prev) item
menu_ClearItem $choice
return $index
fi
done
fi
if [[ "$key" = "" ]]; then
# Notify that Enter key was pressed
return 255
fi
return $choice
}
################################
# Main Menu Loop
################################
menuLoop() {
local choice=0
local running=1
menu_Display
while [[ $running -eq 1 ]]; do
# Enable case insensitive matching
local caseMatch=`shopt -p nocasematch`
shopt -s nocasematch
menu_HandleInput $choice
local newChoice=$?
# Revert to previous case matching
$caseMatch
if [[ $newChoice -eq 255 ]]; then
# Enter pressed - run menu action
drawClear
action=${menuActions[$choice]}
$action
running=$?
# Back from action
# If we are still running, redraw menu
[[ $running -eq 1 ]] && menu_Display
elif [[ $newChoice -lt $menuItemCount ]]; then
# Update selected menu item
choice=$newChoice
fi
done
# Cleanup screen
drawClear
}