More menu refactoring

This commit is contained in:
the-sane 2020-09-07 19:42:18 -04:00 committed by GitHub
parent 6b17322d9e
commit 37223e0d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,6 +51,7 @@ conf_subdir="starcitizen-lug"
# See the message types below for instructions on formatting the string. # See the message types below for instructions on formatting the string.
message() { message() {
if [ "$has_zen" -eq 1 ]; then if [ "$has_zen" -eq 1 ]; then
# Use zenity messages if available
case "$1" in case "$1" in
1) 1)
# info message # info message
@ -67,14 +68,6 @@ message() {
# call format: message 3 "question to ask?" # call format: message 3 "question to ask?"
margs=("--question" "--text=") margs=("--question" "--text=")
;; ;;
4)
# radio button menu
# call format: message 4 "TRUE" "List item 1" "FALSE" "List item 2" "FALSE" "List item 3"
# IMPORTANT: Set the variables "menu_height" and "menu_zenity_text" with appropriate values
# for the height of the dialog based on the number of items in your list and
# the text to be displayed on the menu
margs=("--list" "--radiolist" "--height=$menu_height" "--text=$menu_zenity_text" "--hide-header" "--column=" "--column=Option")
;;
*) *)
echo -e "Invalid message format.\n\nThe message function expects a numerical argument followed by string arguments.\n" echo -e "Invalid message format.\n\nThe message function expects a numerical argument followed by string arguments.\n"
read -n 1 -s -p "Press any key..." read -n 1 -s -p "Press any key..."
@ -82,18 +75,10 @@ message() {
esac esac
# Display the message # Display the message
if [ "$1" -eq 4 ]; then shift 1 # drop the first numerical argument and shift the remaining up one
# requires a space between the assembled arguments zenity "${margs[@]}""$@" --width="400" --title="Star Citizen LUG Helper"
shift 1 # drop the first numerical argument and shift the remaining up one
zenity "${margs[@]}" "$@" --width="400" --title="Star Citizen LUG Helper"
else
# no space between the assmebled arguments
shift 1 # drop the first numerical argument and shift the remaining up one
zenity "${margs[@]}""$@" --width="400" --title="Star Citizen LUG Helper"
fi
else else
# Text based menu. Does not work with type 4 (zenity radio lists) # Fall back to text-based messages when zenity is not available
# that type is handled specially in the menu() function
case "$1" in case "$1" in
1) 1)
# info message # info message
@ -130,7 +115,7 @@ message() {
done done
;; ;;
*) *)
echo -e "\nInvalid message type.\n\nThe message() function is incompatible with message type 4 (zenity radio lists).\nIt requires special handling (see the menu() function).\n" echo -e "\nInvalid message type.\n"
read -n 1 -s -p "Press any key..." read -n 1 -s -p "Press any key..."
;; ;;
esac esac
@ -181,6 +166,7 @@ menu() {
if [ "$has_zen" -eq 1 ]; then if [ "$has_zen" -eq 1 ]; then
# Format the options array for Zenity by adding # Format the options array for Zenity by adding
# TRUE or FALSE to indicate default selections # TRUE or FALSE to indicate default selections
# ie: "TRUE" "List item 1" "FALSE" "List item 2" "FALSE" "List item 3"
for (( i=0; i<"${#menu_options[@]}"-1; i++ )); do for (( i=0; i<"${#menu_options[@]}"-1; i++ )); do
if [ "$i" -eq 0 ]; then if [ "$i" -eq 0 ]; then
# Select the first radio button by default # Select the first radio button by default
@ -192,8 +178,8 @@ menu() {
fi fi
done done
# Display the zenity menu # Display the zenity radio button menu
choice="$(message 4 "${zen_options[@]}")" choice="$(zenity --list --radiolist --width="400" --height="$menu_height" --text="$menu_zenity_text" --title="Star Citizen LUG Helper" --hide-header --column="" --column="Option" "${zen_options[@]}")"
# Loop through the options array to match the chosen option # Loop through the options array to match the chosen option
matched="false" matched="false"