🧹 chore(bump): rename && refactor

This commit is contained in:
Eshan Roy
2024-11-27 20:14:39 +05:30
parent a0e177a61e
commit 8fe68b9275

View File

@@ -14,20 +14,19 @@ CYAN="\033[1;36m"
LAUNCH_TERMINAL_SHELL="bash"
usage() {
cat <<EOF
${BOLD}Usage:${RESET} ${0##*/} [cmd] [options]
${BOLD}Options:${RESET}
-s [shell] Change the shell to [shell].
-h Display this help message.
${BOLD}Description:${RESET}
This script launches a specified command in an appropriate terminal emulator,
automatically detecting the best option based on the desktop environment.
${BOLD}Examples:${RESET}
${CYAN}${0##*/} "echo Hello World" -s zsh${RESET}
EOF
# Use echo to output the help message with colors
echo -e "${BOLD}Usage:${RESET} ${0##*/} [cmd] [options]"
echo -e ""
echo -e "${BOLD}Options:${RESET}"
echo -e " -s [shell] Change the shell to [shell]."
echo -e " -h Display this help message."
echo -e ""
echo -e "${BOLD}Description:${RESET}"
echo -e " This script launches a specified command in an appropriate terminal emulator,"
echo -e " automatically detecting the best option based on the desktop environment."
echo -e ""
echo -e "${BOLD}Examples:${RESET}"
echo -e " ${CYAN}${0##*/} \"echo Hello World\" -s zsh${RESET}"
exit "${1:-0}"
}
@@ -51,32 +50,18 @@ if [ $# -lt 1 ]; then
fi
COMMAND="$1"
# Temporary file for shell script execution
initfile="$(mktemp)"
codefile="$initfile"
trap 'rm -f "$initfile" "$codefile"' EXIT
echo "#!/usr/bin/env $LAUNCH_TERMINAL_SHELL" >"$initfile"
if [ "$LAUNCH_TERMINAL_SHELL" != "bash" ]; then
codefile="$(mktemp)"
echo "$LAUNCH_TERMINAL_SHELL $codefile" >>"$initfile"
fi
echo "$COMMAND" >>"$codefile"
chmod +x "$initfile"
cmd="\"$initfile\""
# Detect terminal emulator
declare -A terminals=(
["alacritty"]="alacritty -e $cmd || LIBGL_ALWAYS_SOFTWARE=1 alacritty -e $cmd"
["konsole"]="konsole -e $cmd"
["kgx"]="kgx -e $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 exec-terminal -e $cmd"
["alacritty"]="alacritty -e bash -c"
["konsole"]="konsole -e bash -c"
["kgx"]="kgx -- bash -c"
["gnome-terminal"]="gnome-terminal -- bash -c"
["xfce4-terminal"]="xfce4-terminal --disable-server --command"
["qterminal"]="qterminal -e bash -c"
["lxterminal"]="lxterminal -e bash -c"
["mate-terminal"]="mate-terminal --disable-factory -e bash -c"
["xterm"]="xterm -e bash -c"
["foot"]="foot -T exec-terminal -e bash -c"
)
term_order=("alacritty" "konsole" "kgx" "gnome-terminal" "mate-terminal" "xfce4-terminal" "qterminal" "lxterminal" "xterm" "foot")
@@ -112,21 +97,15 @@ if [ -z "$terminal" ]; then
exit 1
fi
# Special case for "kgx" terminal
if [ "$terminal" == "kgx" ]; then
sed -i '2i sleep 0.1' "$initfile"
echo 'kill -SIGINT $PPID' >>"$initfile"
fi
# Debug detected terminal
echo -e "${YELLOW}Detected terminal:${RESET} ${BOLD}${terminal}${RESET}"
# Launch the selected terminal
echo -e "${CYAN}Launching command in terminal:${RESET} ${BOLD}${terminal}${RESET}"
eval "${terminals[${terminal}]}" || exitcode=$?
# Clean up temporary files and handle errors
if [ -n "$exitcode" ] && [ "$exitcode" != 130 ]; then
echo -e "${RED}Command failed with exit code:${RESET} ${exitcode}"
# Launch the command in the terminal
echo -e "${CYAN}Launching command:${RESET} ${BOLD}${COMMAND}${RESET}"
eval "${terminals[${terminal}]} \"${COMMAND}\"" || {
echo -e "${RED}Failed to launch command!${RESET}"
exit 2
fi
}
# Success message
echo -e "${GREEN}Command executed successfully!${RESET}"
echo -e "${GREEN}Command executed successfully in terminal!${RESET}"