🔨 refactor(critical): contains bug and may be unstable

This commit is contained in:
Eshan Roy
2024-11-27 19:58:03 +05:30
parent 3b87ff06dc
commit d5e6986d78
4 changed files with 89 additions and 56 deletions

0
snigdhaos-libs/check-snapshot-boot Normal file → Executable file
View File

145
snigdhaos-libs/exec-terminal Normal file → Executable file
View File

@@ -1,69 +1,103 @@
#!/bin/bash
set -e
LAUNCH_TERMINAL_SHELL=bash
# Color definitions
RESET="\033[0m"
BOLD="\033[1m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
usage(){
echo "Usage: ${0##*/} [cmd]"
echo ' -s [shell] Change shell to [shell]'
echo ' -h This help'
exit 1
# Default shell
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
exit "${1:-0}"
}
# Parse command-line options
opts='s:h'
while getopts "${opts}" arg; do
case "${arg}" in
s) LAUNCH_TERMINAL_SHELL="$OPTARGS" ;;
h | ?) usage 0 ;;
*)
echo "Invalid arguments '${arg}'"
usage 1 ;;
s) LAUNCH_TERMINAL_SHELL="${OPTARG}" ;;
h | ?) usage 0 ;;
*)
echo -e "${RED}Invalid argument: '${arg}'${RESET}"
usage 1 ;;
esac
done
shift $((OPTIND - 1))
# Validate input command
if [ $# -lt 1 ]; then
echo -e "${RED}Error:${RESET} A command is required to execute."
usage 1
fi
COMMAND="$1"
# Temporary file for shell script execution
initfile="$(mktemp)"
codefile="$initfile"
echo "#!/usr/bin/env bash" >"$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 "$1" >>"$codefile"
echo "$COMMAND" >>"$codefile"
chmod +x "$initfile"
cmd="\"$initfile\""
terminal=""
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")
declare -a term_order=("alacritty" "knosole" "kgx" "gnome-terminal" "mate-terminal" "xfce4-terminal" "qterminal" "lxterminal" "xterm" "foot")
# 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"
)
term_order=("alacritty" "konsole" "kgx" "gnome-terminal" "mate-terminal" "xfce4-terminal" "qterminal" "lxterminal" "xterm" "foot")
# Desktop environment-specific terminal preference
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
;;
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" ;;
esac
# Fallback: Check for available terminals
if [ -z "$terminal" ] || ! command -v "$terminal" &>/dev/null; then
# shellcheck disable=SC2068
for i in ${term_order[@]}; do
for i in "${term_order[@]}"; do
if command -v "$i" &>/dev/null; then
terminal="$i"
break
@@ -71,29 +105,28 @@ if [ -z "$terminal" ] || ! command -v "$terminal" &>/dev/null; then
done
fi
# Error handling if no terminal is found
if [ -z "$terminal" ]; then
notify-send -t 1500 --app-name=Snigdha\ OS "No Terminal Found!"
echo -e "${RED}Error:${RESET} No terminal emulator found!"
notify-send -t 1500 --app-name="Terminal Launcher" "No terminal emulator found!"
exit 1
fi
# Special case for "kgx" terminal
if [ "$terminal" == "kgx" ]; then
# shellcheck disable=SC2086
sed -i '2i sleep 0.1' $initfile
# shellcheck disable=SC2016
# shellcheck disable=SC2086
echo 'Kill -SIGNINT $PPID' >>$initfile
sed -i '2i sleep 0.1' "$initfile"
echo 'kill -SIGINT $PPID' >>"$initfile"
fi
eval "${terminals[${terminal}]}" || {
exitcode=$?
}
# Launch the selected terminal
echo -e "${CYAN}Launching command in terminal:${RESET} ${BOLD}${terminal}${RESET}"
eval "${terminals[${terminal}]}" || exitcode=$?
rm "$initfile"
if [ "$codefile" != "$initfile" ]; then
rm "$codefile"
fi
if [ ! -z "$exitcode" ] && [ "$exitcode" != 130 ]; then
# Clean up temporary files and handle errors
if [ -n "$exitcode" ] && [ "$exitcode" != 130 ]; then
echo -e "${RED}Command failed with exit code:${RESET} ${exitcode}"
exit 2
fi
fi
# Success message
echo -e "${GREEN}Command executed successfully!${RESET}"

0
snigdhaos-libs/install-package Normal file → Executable file
View File

0
snigdhaos-libs/pkexec-gui Normal file → Executable file
View File