Use switch statements. Add message documentation comments.

This commit is contained in:
the-sane 2020-08-15 11:22:44 -04:00 committed by GitHub
parent d5b361b790
commit c6c5cc8c45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,28 +44,42 @@ conf_subdir="starcitizen-lug"
# followed by a string of arguments that will be passed to zenity or echoed to the user. # followed by a string of arguments that will be passed to zenity or echoed to the user.
# #
# To call this function, use the following format: message [number] [string] # To call this function, use the following format: message [number] [string]
# See the message types below for instructions on formatting the string. # See the message types below for specific instructions on formatting the string.
message() { message() {
if [ "$has_zen" -eq 1 ]; then if [ "$has_zen" -eq 1 ]; then
if [ "$1" -eq 1 ]; then case "$1" in
# info 1)
margs=("--info" "--no-wrap" "--text=") # info message
elif [ "$1" -eq 2 ]; then # call format: message 1 "text to display"
# warning margs=("--info" "--no-wrap" "--text=")
margs=("--warning" "--text=") ;;
elif [ "$1" -eq 3 ]; then 2)
# question # warning message
margs=("--question" "--text=") # call format: message 2 "text to display"
elif [ "$1" -eq 4 ]; then margs=("--warning" "--text=")
# radio list ;;
margs=("--list" "--radiolist" "--text=Choose from the following options:" "--hide-header" "--column=" "--column=Option") 3)
elif [ "$1" -eq 5 ]; then # question
# main menu radio list # call format: message 3 "question to ask?"
margs=("--list" "--radiolist" "--height=240" "--text=<b><big>Welcome, fellow Penguin, to the Star Citizen LUG Helper Script!</big>\n\nThis script is designed to help you optimize your system for Star Citizen.</b>\n\nYou may choose from the following options:" "--hide-header" "--column=" "--column=Option") margs=("--question" "--text=")
else ;;
echo -e "Invalid message format.\n\nThe message function expects a numerical argument followed by the string to display.\n" 4)
read -n 1 -s -p "Press any key..." # radio button list
fi # call format: message 4 "--height=165" "TRUE" "List item 1" "FALSE" "List item 2" "FALSE" "List item 3"
# IMPORTANT: When calling, specify an appropriate height for the dialog based on the number of items in your list
margs=("--list" "--radiolist" "--text=Choose from the following options:" "--hide-header" "--column=" "--column=Option")
;;
5)
# main menu radio list
# call format: message 5 "TRUE" "List item 1" "FALSE" "List item 2" "FALSE" "List item 3"
# IMPORTANT: Adjust the height value below based on the number of items listed in the main menu
margs=("--list" "--radiolist" "--height=240" "--text=<b><big>Welcome, fellow Penguin, to the Star Citizen LUG Helper Script!</big>\n\nThis script is designed to help you optimize your system for Star Citizen.</b>\n\nYou may choose from the following options:" "--hide-header" "--column=" "--column=Option")
;;
*)
echo -e "Invalid message format.\n\nThe message function expects a numerical argument followed by the string to display.\n"
read -n 1 -s -p "Press any key..."
;;
esac
# Display the message # Display the message
if [ "$1" -eq 4 ] || [ "$1" -eq 5 ]; then if [ "$1" -eq 4 ] || [ "$1" -eq 5 ]; then
@ -80,38 +94,46 @@ message() {
else else
# Text based menu. Does not work with message types 4 and 5 (zenity radio lists) # Text based menu. Does not work with message types 4 and 5 (zenity radio lists)
# those need to be handled specially in the code # those need to be handled specially in the code
if [ "$1" -eq 1 ]; then case "$1" in
# info 1)
clear # info message
echo -e "\n$2\n" # call format: message 1 "text to display"
read -n 1 -s -p "Press any key..." clear
elif [ "$1" -eq 2 ]; then echo -e "\n$2\n"
# warning read -n 1 -s -p "Press any key..."
clear ;;
echo -e "\n$2\n" 2)
read -n 1 -s -p "Press any key..." # warning message
return 0 # call format: message 2 "text to display"
elif [ "$1" -eq 3 ]; then clear
# question echo -e "\n$2\n"
clear read -n 1 -s -p "Press any key..."
echo -e "$2" return 0
while read -p "[y/n]: " yn; do ;;
case "$yn" in 3)
[Yy]*) # question
return 0 # call format: message 3 "question to ask?"
;; clear
[Nn]*) echo -e "$2"
return 1 while read -p "[y/n]: " yn; do
;; case "$yn" in
*) [Yy]*)
echo "Please type 'y' or 'n'" return 0
;; ;;
esac [Nn]*)
done return 1
else ;;
echo -e "\nInvalid message type.\n\nText menus are not compatible with message types 4 and 5 (zenity radio lists)\nand require special handling.\n" *)
read -n 1 -s -p "Press any key..." echo "Please type 'y' or 'n'"
fi ;;
esac
done
;;
*)
echo -e "\nInvalid message type.\n\nText menus are not compatible with message types 4 and 5 (zenity radio lists)\nand require special handling.\n"
read -n 1 -s -p "Press any key..."
;;
esac
fi fi
} }
@ -245,7 +267,7 @@ getdirs() {
# Save exported keybinds, wipe the USER directory, and restore keybinds # Save exported keybinds, wipe the USER directory, and restore keybinds
sanitize() { sanitize() {
# Prompt user to back up the current keybinds in the game # Prompt user to back up the current keybinds in the game
message 1 "Before proceeding, please be sure you have\nexported your Star Citizen keybinds from within the game!\n\nTo do this, launch the game and go to:\nOptions->Keybindings->Control Profiles->Save Control Settings" message 1 "Before proceeding, please be sure you have exported\nyour Star Citizen keybinds from within the game.\n\nTo do this, launch the game and go to:\nOptions->Keybindings->Control Profiles->Save Control Settings"
# Get/Set directory paths # Get/Set directory paths
getdirs getdirs
@ -262,7 +284,7 @@ sanitize() {
# Check for exported keybind files # Check for exported keybind files
if [ ! -d "$mappings_dir" ] || [ -z "$(ls -A "$mappings_dir")" ]; then if [ ! -d "$mappings_dir" ] || [ -z "$(ls -A "$mappings_dir")" ]; then
if message 3 "Warning: No exported keybindings found. Keybinds will not be backed up!\n\nDo you want to continue anyway?"; then if message 3 "Warning: No exported keybindings found.\nContinuing will erase your existing keybinds!\n\nDo you want to continue anyway?"; then
exported=0 exported=0
else else
# User said no # User said no