Skip to content

Instantly share code, notes, and snippets.

@yeiichi
Created March 12, 2025 00:29
Show Gist options
  • Save yeiichi/550b22065ef46e369195b5d668d90a72 to your computer and use it in GitHub Desktop.
Save yeiichi/550b22065ef46e369195b5d668d90a72 to your computer and use it in GitHub Desktop.
Interactive app menu for zsh
#!/usr/bin/env zsh
# Interactive app menu for zsh.
# Define colors
YELLOW="\033[93m"
RESET="\033[0m"
# Function to execute App. A, B, and C
do_something_A() {
echo "Doing something A..."
}
do_something_B() {
echo "Doing something B..."
}
do_something_C() {
echo "Doing something C..."
}
# Function to display the menu
display_menu() {
cat <<"EOF"
MENU
----
1) Do something A
2) Do something B
3) Do something C
4) Quit
EOF
}
# Function to display help message
display_help() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Options:
--help Show this help message and exit.
Menu Options:
1) Do something A
2) Do something B
3) Do something C
4) Quit
(Type 'exit' for an emergency break)
EOF
}
main() {
# Handle command-line arguments
if [[ "$1" == "--help" ]]; then
display_help
exit 0
fi
user_choice="" # Initialize choice variable
# Trap to handle CTRL+C (SIGINT)
trap "echo; echo 'Aborting...'; exit 0" SIGINT
# Loop until user selects 4 (Quit)
while [[ "$user_choice" != "4" ]]; do
display_menu
echo -n "${YELLOW}Enter your choice >> ${RESET}"
read -r user_choice
# Validate user input
if [[ ! "$user_choice" =~ ^[1-4]$ ]]; then
echo "Invalid choice. Please choose a number between 1 and 4."
continue
fi
case "$user_choice" in
1) do_something_A ;;
2) do_something_B ;;
3) do_something_C ;;
4) echo "Exiting..." ;;
*) echo "Unexpected input. Please try again." ;;
esac
done
}
# Execute main function with command-line arguments
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment