tui-launcher.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. check_dependencies() {
  3. for dep in jq gum figlet lolcat; do
  4. if ! command -v "$dep" &> /dev/null; then
  5. gum style --foreground 212 "$dep is not installed."
  6. if gum confirm "Do you want to install $dep?"; then
  7. # Assuming Debian/Ubuntu based system
  8. sudo apt-get update && sudo apt-get install -y "$dep"
  9. if ! command -v "$dep" &> /dev/null; then
  10. gum style --foreground 212 "Failed to install $dep. Please install it manually."
  11. exit 1
  12. fi
  13. else
  14. gum style --foreground 212 "Installation cancelled. Please install $dep to continue."
  15. exit 1
  16. fi
  17. fi
  18. done
  19. }
  20. check_dependencies
  21. # Read the programs from the JSON file
  22. PROGRAMS_JSON=$(cat "$(dirname "$0")/programs.json")
  23. PROGRAM_NAMES=$(echo "$PROGRAMS_JSON" | jq -r '.[].name')
  24. while true; do
  25. clear
  26. figlet "TUI Launcher" | lolcat
  27. gum style --border normal --padding '1 2' --margin 1 "Press $(gum style --bold 'ESC') or $(gum style --bold 'Q') to exit."
  28. # Create header
  29. HEADER="Icon,Program,Description"
  30. # Create rows from JSON
  31. ROWS=$(echo "$PROGRAMS_JSON" | jq -r '.[] | [.emoji, .name, .description] | @csv')
  32. # Show the gum table
  33. CHOICE=$(echo -e "$HEADER\n$ROWS" | gum table --widths=5,20,75)
  34. # If the user made a choice
  35. if [ -n "$CHOICE" ]; then
  36. # Extract the program name from the selected row
  37. CHOSEN_NAME=$(echo "$CHOICE" | cut -d',' -f2)
  38. if [ -n "$CHOSEN_NAME" ]; then
  39. # Get the corresponding command
  40. COMMAND_TO_RUN=$(echo "$PROGRAMS_JSON" | jq -r --arg name "$CHOSEN_NAME" '.[] | select(.name == $name) | .command')
  41. # Run the command
  42. if [ -n "$COMMAND_TO_RUN" ]; then
  43. # Clear the screen before launching the program for a cleaner experience
  44. clear
  45. eval "$COMMAND_TO_RUN"
  46. else
  47. gum style --foreground 212 "Invalid selection."
  48. fi
  49. else
  50. gum style --foreground 212 "Could not determine selection."
  51. fi
  52. else
  53. gum style --foreground 212 "Exiting..."
  54. break
  55. fi
  56. gum style --border normal --padding '0 1' --margin 1 "[↑/k] Up | [↓/j] Down | [Enter] Select"
  57. done