-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
interface_menu.sh
78 lines (67 loc) · 2.11 KB
/
interface_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
module_options+=(
["set_interface,author"]="Tearran"
["set_interface,feature"]="set_interface"
["set_interface,desc"]="Check for (Whiptail, DIALOG, READ) tools and set the user interface."
["set_interface,example"]=""
["set_interface,status"]="review"
)
#
# Check for (Whiptail, DIALOG, READ) tools and set the user interface
set_interface() {
# Set dialog tool hierarchy based on environment
if [[ -x "$(command -v whiptail)" ]]; then
DIALOG="whiptail"
elif [[ -x "$(command -v dialog)" ]]; then
DIALOG="dialog"
else
DIALOG="read" # Fallback to read if no dialog tool is available
fi
}
module_options+=(
["see_menu,author"]="Tearran"
["see_menu,feature"]="see_menu"
["see_menu,desc"]="Uses Avalible (Whiptail, DIALOG, READ) for the menu interface"
["see_menu,example"]="<function_name>"
["see_menu,status"]="review"
)
#
# Uses Avalible (Whiptail, DIALOG, READ) for the menu interface
function see_menu() {
# Check if the function name was provided
local function_name="$1"
# Get the help message from the specified function
help_message=$("$function_name" help)
# Prepare options for the dialog tool based on help message
options=()
while IFS= read -r line; do
if [[ $line =~ ^[[:space:]]*([a-zA-Z0-9_-]+)[[:space:]]*-\s*(.*)$ ]]; then
options+=("${BASH_REMATCH[1]}" " - ${BASH_REMATCH[2]}")
fi
done <<< "$help_message"
# Display menu based on DIALOG tool
case $DIALOG in
"dialog")
choice=$(dialog --title "${function_name^}" --menu "Choose an option:" 0 80 9 "${options[@]}" 2>&1 >/dev/tty)
;;
"whiptail")
choice=$(whiptail --title "${function_name^}" --menu "Choose an option:" 0 80 9 "${options[@]}" 3>&1 1>&2 2>&3)
;;
"read")
echo "Available options:"
for ((i=0; i<${#options[@]}; i+=2)); do
echo "$((i / 2 + 1)). ${options[i]} - ${options[i + 1]}"
done
read -p "Enter choice number: " choice_index
choice=${options[((choice_index - 1) * 2)]}
;;
esac
# Check if choice was made or canceled
if [[ -z $choice ]]; then
echo "Menu canceled."
return 1
fi
# Call the specified function with the chosen option
"$function_name" "$choice"
}
#set_interface
#see_menu "$@"