-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.sh
More file actions
executable file
·320 lines (276 loc) · 8.57 KB
/
menu.sh
File metadata and controls
executable file
·320 lines (276 loc) · 8.57 KB
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
#!/bin/bash
function setup_tui() {
tput clear
tput smcup
tput civis
# Save terminal settings
ORIGINAL_STTY=$(stty -g)
# Disable canonical mode and input echoing for single-key input
stty -icanon min 1 -echo
trap after_tui EXIT
STOP_MENU=false
MODES=(
"dev boy"
"give me bloattt!!"
"basic man"
)
FEATURES=(
"install ssh"
"setup minikube"
"upgrade"
"reboot"
)
FEATURE_STATE=("" "" "" "")
MODE_LEN="${#MODES[@]}"
FEATURES_NUM="${#FEATURES[@]}"
CURRENT_MODE=0
TOTAL_ITEMS=$(($MODE_LEN + $FEATURES_NUM))
CURRENT_CURSOR_INDEX=0
PREVIOUS_CURSOR_INDEX=-1
CURSOR_ON_FEATURE=false
REVERSE_ON=$(tput rev)
REVERSE_OFF=$(tput sgr0)
# (before_headers after_headers after_middle after_options)
EMPTY_LINES_SPACING=(2 1 2 1 2)
COL=5
}
function draw_static_screen() {
STATIC_INDEX=0 # incriments in draw_static_line
# draw static text
draw_static_line ${EMPTY_LINES_SPACING[0]}
draw_static_line "Wecome to the Archnet install menu" "header"
draw_static_line "Select 1 Install Option:" "header"
draw_static_line ${EMPTY_LINES_SPACING[1]}
MODE_START_INDEX=$STATIC_INDEX
draw_static_line $MODE_LEN
draw_static_line ${EMPTY_LINES_SPACING[2]}
FEAT_START_INDEX=$STATIC_INDEX
draw_static_line "Advanced Selection:" "middle"
draw_static_line ${EMPTY_LINES_SPACING[3]}
draw_static_line $FEATURES_NUM
draw_static_line ${EMPTY_LINES_SPACING[4]}
draw_static_line "Instructions: Use [Up]/[Down] to move, [Space] to select/toggle" "footer"
draw_static_line "[Enter] to apply, [Q] to quit." "footer"
}
function draw_screen() {
tput clear
draw_static_screen
# draw Modes
for ((i=0; i<$MODE_LEN; i++)); do
if [ $i -eq $CURRENT_MODE ]; then
draw_line $i true true
else
draw_line $i false false
fi
done
# draw feats
set_feats_from_mode $CURRENT_MODE
}
function draw_static_line() {
# $1: text
# $2: kind (header,middle,footer)
# $1: number of spaces
tput cup $STATIC_INDEX $COL
if [ $# -eq 1 ]; then
for ((i=0; i<$1; i++)); do
echo -e ""
STATIC_INDEX=$(( $STATIC_INDEX + 1 ))
done
else
echo -e "$1"
STATIC_INDEX=$(( $STATIC_INDEX + 1 ))
fi
}
function handle_key_press() {
# Read single character without timeout
IFS='' read -n 1 -s -r key
#key=$(dd bs=1 count=1 2>/dev/null) # old way not needed
# Reads two key presses to detect arrow keys
if [[ "$key" == $'\x1b' ]]; then
read -n 2 -t 0.001 sequence
key="$key$sequence"
fi
# Process Input
case "$key" in
# Arrow Keys
# UP Arrow
$'\x1b[A' | $'\x1b[D')
move_up
;;
# DOWN Arrow
$'\x1b[B' | $'\x1b[C')
move_down
;;
# Space Key ($'\x20' or actual space char ' '): Select Mode OR Toggle Feature
' ')
toggle_option
;;
# Enter Key: Confirm/Submit (Enter is $'\x0a' or sometimes $'\r' for Carriage Return)
#$'\n' | $'\r')
$'\x0a' | '')
STOP_MENU=true # finishes loop
QUIT=false
;;
# Q: Quit
'q' | 'Q')
STOP_MENU=true
QUIT=true # quiting the install
;;
esac
}
function move_up() {
if (($CURRENT_CURSOR_INDEX <= 0)); then
return
fi
if (($CURRENT_CURSOR_INDEX >= $MODE_LEN)); then
# feat
CURRENT_FEAT_INDEX=$(($CURRENT_CURSOR_INDEX - $MODE_LEN))
local condition="${FEATURE_STATE[$CURRENT_FEAT_INDEX]}"
draw_line $CURRENT_CURSOR_INDEX $condition false
CURRENT_CURSOR_INDEX=$((CURRENT_CURSOR_INDEX - 1))
CURRENT_FEAT_INDEX=$((CURRENT_FEAT_INDEX - 1))
if (($CURRENT_CURSOR_INDEX == (($MODE_LEN - 1)))); then
# moving between feat to mode
CURRENT_FEAT_INDEX=$(($CURRENT_CURSOR_INDEX - $MODE_LEN))
INDEX=$([ "$CURRENT_MODE" -eq "$CURRENT_CURSOR_INDEX" ] && echo true || echo false)
draw_line $CURRENT_CURSOR_INDEX $INDEX true
else
draw_line $CURRENT_CURSOR_INDEX ${FEATURE_STATE[$CURRENT_FEAT_INDEX]} true
fi
else
# mode
draw_line $CURRENT_CURSOR_INDEX $([ "$CURRENT_MODE" -eq "$CURRENT_CURSOR_INDEX" ] && echo true || echo false) false
CURRENT_CURSOR_INDEX=$((CURRENT_CURSOR_INDEX - 1))
draw_line $CURRENT_CURSOR_INDEX $([ "$CURRENT_MODE" -eq "$CURRENT_CURSOR_INDEX" ] && echo true || echo false) true
fi
}
function move_down() {
if (($CURRENT_CURSOR_INDEX >= $(($TOTAL_ITEMS - 1)))); then
return
fi
if (($CURRENT_CURSOR_INDEX >= $MODE_LEN)); then
# feat
CURRENT_FEAT_INDEX=$(($CURRENT_CURSOR_INDEX - $MODE_LEN))
local condition="${FEATURE_STATE[$CURRENT_FEAT_INDEX]}"
draw_line $CURRENT_CURSOR_INDEX $condition false
CURRENT_CURSOR_INDEX=$((CURRENT_CURSOR_INDEX + 1))
CURRENT_FEAT_INDEX=$((CURRENT_FEAT_INDEX + 1))
draw_line $CURRENT_CURSOR_INDEX ${FEATURE_STATE[$CURRENT_FEAT_INDEX]} true
else
# mode
draw_line $CURRENT_CURSOR_INDEX $([ "$CURRENT_MODE" -eq "$CURRENT_CURSOR_INDEX" ] && echo true || echo false) false
CURRENT_CURSOR_INDEX=$((CURRENT_CURSOR_INDEX + 1))
if (($CURRENT_CURSOR_INDEX == $MODE_LEN)); then
# moving between mode and feat
CURRENT_FEAT_INDEX=$(($CURRENT_CURSOR_INDEX - $MODE_LEN))
INDEX=${FEATURE_STATE[$CURRENT_FEAT_INDEX]}
draw_line $CURRENT_CURSOR_INDEX $INDEX true
else
draw_line $CURRENT_CURSOR_INDEX $([ "$CURRENT_MODE" -eq "$CURRENT_CURSOR_INDEX" ] && echo true || echo false) true
fi
fi
}
function toggle_option() {
# in space key press
local state
if (($CURRENT_CURSOR_INDEX >= $MODE_LEN)); then
# toggle feat
CURRENT_FEAT_INDEX=$(($CURRENT_CURSOR_INDEX - $MODE_LEN))
state="${FEATURE_STATE[$CURRENT_FEAT_INDEX]}"
if $state; then
state=false
else
state=true
fi
#echo $state
FEATURE_STATE[$CURRENT_FEAT_INDEX]=$state
draw_line $CURRENT_CURSOR_INDEX $state true
else
# set mode
if (($CURRENT_MODE != $CURRENT_CURSOR_INDEX)); then
draw_line $CURRENT_MODE false false
CURRENT_MODE=$CURRENT_CURSOR_INDEX
draw_line $CURRENT_CURSOR_INDEX true true
set_feats_from_mode $CURRENT_CURSOR_INDEX
fi
fi
}
function apply_feats() {
# $1 feat array
# change the different feat states
local -n new_feats=$1
for ((i = 0; i < $FEATURES_NUM; i++)); do
if [[ ${FEATURE_STATE[i]} != ${new_feats[i]} ]]; then
local new_index=$(( $MODE_LEN + $i ))
FEATURE_STATE[i]="${new_feats[i]}" # updates the array
draw_line "$new_index" "${new_feats[i]}" false # draw feat with opposit mark
fi
done
}
function set_feats_from_mode() {
local feats
case "$1" in
0) feats=(true true false false);;
1) feats=(true true true true);;
2) feats=(true false false false);;
esac
apply_feats feats
}
function draw_line() {
# $1: cursor index (0-end of features)
# $2: mark or unmark state?
# $3: is reverse
local start
local char
local end=""
if [ "$3" = "true" ]; then
start="$start${REVERSE_ON}"
end="${REVERSE_OFF}"
fi
if (($1 < $MODE_LEN)); then
# Mode
start="${Blue}$start"
end="${NC}${end}"
if "$2" = "true"; then
char="*"
else
char=" "
fi
tput cup $(( $1 + $MODE_START_INDEX )) $COL
echo -e "${start}($char) ${MODES[$1]}${end}"
else
# Feature
start="${Green}$start"
end="${NC}${end}"
if [[ "$2" = "true" ]]; then
char="V"
else
char=" "
fi
tput cup $(( $1 + $FEAT_START_INDEX )) $COL
echo -e "${start}[$char] ${FEATURES[$1-$MODE_LEN]}${end}"
fi
}
function after_tui() {
stty "$ORIGINAL_STTY"
tput cnorm
tput rmcup
}
function main() {
set -e
setup_tui
draw_screen
# main loop
while [[ "$STOP_MENU" != true ]]; do
handle_key_press
done
after_tui
if $QUIT; then
echo "quiting"
else
echo "starting install..."
echo "the results are:"
echo -e "$GREEN${FEATURE_STATE[@]}$NC"
fi
}
main