-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.sh
193 lines (182 loc) · 5.4 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
#!/bin/bash
function MENU() {
# COLORS
GRAY="$(tput setaf 0; tput bold)"
RED="$(tput setaf 1; tput bold)"
GREEN="$(tput setaf 2; tput bold)"
YELLOW="$(tput setaf 3; tput bold; tput smul)"
BLUE="$(tput setaf 4; tput bold)"
MAGENTA="$(tput setaf 5; tput bold)"
CYAN="$(tput setaf 6; tput bold)"
WHITE="$(tput setaf 7; tput bold)"
EOS="$(tput sgr0)"
DIV="===================="
ARROW="$(tput setaf 3; tput bold)"
INDICATOR="-->"
# ERROR CODES
declare -A MENU_ERRORS
MENU_ERRORS[INVALID_PROMPT]=100
MENU_ERRORS[INVALID_OPTIONS]=101
# ERROR MESSAGES
declare -A ERRORS
ERROR[INVALID_PROMPT]="\n${RED}MENU ERROR: No prompt provided${EOS}"
ERROR[INVALID_OPTIONS]="\n${RED}MENU ERROR: No options provided${EOS}"
# MENU
if [ -n "$ZSH_VERSION" ]; then
if [ -z "$1" ]; then
echo -e ${ERROR[INVALID_PROMPT]}
return ${MENU_ERRORS[INVALID_PROMPT]}
fi
if [ -z "$2" ]; then
echo -e ${ERROR[INVALID_OPTIONS]}
return ${MENU_ERRORS[INVALID_OPTIONS]}
fi
MENUPROMPT=$1
SELECTED=1
OPTIONS=("${@:2}")
LENGTH=${#OPTIONS[@]}
else
if [ -z "$1" ]; then
echo -e ${ERROR[INVALID_PROMPT]}
return ${MENU_ERRORS[INVALID_PROMPT]}
fi
if [ -z "$2" ]; then
echo -e ${ERROR[INVALID_OPTIONS]}
return ${MENU_ERRORS[INVALID_OPTIONS]}
fi
PROMPT=("$1")
SELECTED=0
OPTIONS="${@:2}"
LENGTH="$((${#OPTIONS[@]} - 4))"
fi
# FUNCTIONS
PRINT_MENU() {
# Runs clear to prevent infinite scroll when choosing
clear
# Displays menu header
if [ -n "$ZSH_VERSION" ]; then
echo -e "$GREEN$MENUPROMPT$EOS"
else
echo -e "$GREEN${PROMPT[0]}$EOS"
fi
echo -e "$GREEN$DIV$EOS"
# Displays menu options
if [ -n "$ZSH_VERSION" ]; then
for (( i=1;i<=${#OPTIONS[@]};i++ ))
do
if [[ $SELECTED -eq $i ]]
# Renders current option in bold
then
OPT=${OPTIONS[$i-1]}
echo -e "\n $ARROW$INDICATOR$EOS $YELLOW$OPT$EOS"
# Renders other options
else
OPT=${OPTIONS[$i-1]}
echo -e "\n$GRAY$OPT$EOS"
fi
done
else
for (( i=0;i<${#OPTIONS[@]};i++ ))
do
if [[ $SELECTED -eq $i ]]
# Renders current option in bold
then
OPT=${OPTIONS[$i]}
echo -e "\n $ARROW$INDICATOR$EOS $YELLOW$OPT$EOS"
# Renders other options
else
OPT=${OPTIONS[$i]}
echo -e "\n$GRAY$OPT$EOS"
fi
done
fi
# Displays menu footer
echo -e "\n$GREEN$DIV$EOS"
}
# Displays menu
PRINT_MENU
# Reads user input // Navigation
if [ -n "$ZSH_VERSION" ]; then
while read -rsk1 input
do
case $input in
"A")
if [[ $SELECTED -lt 2 ]]
then
SELECTED=$(($LENGTH))
else
SELECTED=$(($SELECTED - 1))
fi
PRINT_MENU
;;
"B")
if [[ $SELECTED -gt $(($LENGTH - 1)) ]]
then
SELECTED=1
else
SELECTED=$(($SELECTED + 1))
fi
PRINT_MENU
;;
# Returns selected option
""|"\n") return $(($SELECTED)) ;;
esac
done
else
while read -rsn1 input
do
case $input in
"A")
if [[ $SELECTED -lt 1 ]]
then
SELECTED=$(($LENGTH + 3))
else
SELECTED=$(($SELECTED - 1))
fi
PRINT_MENU
;;
"B")
if [[ $SELECTED -gt $(($LENGTH + 2)) ]]
then
SELECTED=0
else
SELECTED=$(($SELECTED + 1))
fi
PRINT_MENU
;;
# Returns selected option
"") return $(($SELECTED)) ;;
esac
done
fi
}
#################################################################################################################
#
# USAGE
#
# Once the menu.sh script is located on the desired directory, source in your code:
#
# source menu.sh
#
# Now just declare your options in an array.
# Finally, call the menu with the following:
#
# PROMPT="Choose an option from the menu below: "
# OPTIONS=("Option 1" "Option 2" "Option 3")
# MENU "${PROMPT}" "${OPTIONS}"
#
# Once you have selected an option, the menu will return the value of the index of the selected option.
# For example, you can return the value of the selected option with the following:
#
# RESULT=${OPTIONS[$?]} ======> selected option value (output: option 2)
#
# Or you can return the index of the selected option with the following:
#
# RESULT=$? ======> selected index value (output: 1)
#
# echo -e "\nYou selected: $RESULT" >> echoes the selected option value
# to the terminal
#
# Enjoy!
#
#################################################################################################################