#!/usr/bin/bash # Author: Garuda Linux # Adopted for Snigdha OS set -euo pipefail # Enable strict error handling LAUNCH_TERMINAL_SHELL="bash" usage() { echo "Usage: ${0##*/} [options] [command]" echo echo "Options:" echo " -s [shell] Change shell to [shell] (default: bash)" echo " -h Show this help message" echo echo "Example:" echo " ${0##*/} -s zsh 'echo Hello, World!'" echo " ${0##*/} -h" exit 1 } # Parse options opts="s:h" while getopts "${opts}" arg; do case "${arg}" in s) LAUNCH_TERMINAL_SHELL="$OPTARG" ;; h) usage ;; ?) usage ;; *) echo "Invalid option: ${arg}" usage ;; esac done # Shift away the processed options shift $((OPTIND - 1)) # Check if a command was passed if [ $# -eq 0 ]; then echo "Error: No command provided." usage fi # Create temporary files for the shell scripts initfile=$(mktemp) codefile=$initfile # Generate the initial shell script echo "#!/usr/bin/env bash" > "$initfile" # If user has specified a shell, set it in the code if [ "$LAUNCH_TERMINAL_SHELL" != "bash" ]; then codefile=$(mktemp) echo "$LAUNCH_TERMINAL_SHELL $codefile" >> "$initfile" fi # Write the user's command to the code file echo "$1" >> "$codefile" chmod +x "$initfile" # Command to run in the terminal emulator cmd="\"$initfile\"" # Define a dictionary of supported terminal emulators declare -A terminals=( ["alacritty"]="alacritty -e $cmd || LIBGL_ALWAYS_SOFTWARE=1 alacritty -e $cmd" ["konsole"]="konsole -e $cmd" ["kgx"]="kgx -- $cmd" ["gnome-terminal"]="gnome-terminal --wait -- $cmd" ["xfce4-terminal"]="xfce4-terminal --disable-server --command '$cmd'" ["qterminal"]="qterminal -e $cmd" ["lxterminal"]="lxterminal -e $cmd" ["mate-terminal"]="mate-terminal --disable-factory -e $cmd" ["xterm"]="xterm -e $cmd" ["foot"]="foot -T launch-terminal -e $cmd" ) # Define an ordered list of terminals to try declare -a term_order=( "alacritty" "konsole" "kgx" "gnome-terminal" "mate-terminal" "xfce4-terminal" "qterminal" "lxterminal" "xterm" "foot" ) # Select the terminal based on the desktop environment case "$XDG_CURRENT_DESKTOP" in KDE) terminal="konsole" ;; GNOME) if command -v "kgx" &> /dev/null; then terminal="kgx" else terminal="gnome-terminal" fi ;; XFCE) terminal="xfce4-terminal" ;; LXQt) terminal="qterminal" ;; MATE) terminal="mate-terminal" ;; *) for i in "${term_order[@]}"; do if command -v "$i" &> /dev/null; then terminal="$i" break fi done ;; esac # If no terminal emulator is found, notify and exit if [ -z "$terminal" ]; then notify-send -t 15000 --app-name=Snigdha\ OS "No terminal installed" \ "No supported terminal emulator is installed. Please install a terminal emulator like Alacritty." exit 1 fi # Special handling for kgx to ensure proper behavior if [ "$terminal" = "kgx" ]; then sed -i '2i sleep 0.1' "$initfile" echo 'kill -SIGINT $PPID' >> "$initfile" fi # Run the terminal emulator with the command eval "${terminals[$terminal]}" || exitcode=$? # Clean up temporary files rm "$initfile" if [ "$codefile" != "$initfile" ]; then rm "$codefile" fi # Exit gracefully if no terminal error occurred, otherwise propagate the error if [ -n "${exitcode-}" ] && [ "$exitcode" != 130 ]; then echo "Error: Terminal launch failed with exit code $exitcode" exit 2 fi