🔨 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 #!/bin/bash
set -e 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(){ # Default shell
echo "Usage: ${0##*/} [cmd]" LAUNCH_TERMINAL_SHELL="bash"
echo ' -s [shell] Change shell to [shell]'
echo ' -h This help' usage() {
exit 1 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' opts='s:h'
while getopts "${opts}" arg; do while getopts "${opts}" arg; do
case "${arg}" in case "${arg}" in
s) LAUNCH_TERMINAL_SHELL="$OPTARGS" ;; s) LAUNCH_TERMINAL_SHELL="${OPTARG}" ;;
h | ?) usage 0 ;; h | ?) usage 0 ;;
*) *)
echo "Invalid arguments '${arg}'" echo -e "${RED}Invalid argument: '${arg}'${RESET}"
usage 1 ;; usage 1 ;;
esac esac
done done
shift $((OPTIND - 1)) 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)" initfile="$(mktemp)"
codefile="$initfile" 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 if [ "$LAUNCH_TERMINAL_SHELL" != "bash" ]; then
codefile="$(mktemp)" codefile="$(mktemp)"
echo "$LAUNCH_TERMINAL_SHELL $codefile" >>"$initfile" echo "$LAUNCH_TERMINAL_SHELL $codefile" >>"$initfile"
fi fi
echo "$1" >>"$codefile" echo "$COMMAND" >>"$codefile"
chmod +x "$initfile" chmod +x "$initfile"
cmd="\"$initfile\"" cmd="\"$initfile\""
terminal="" # 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") declare -A terminals=(
declare -a term_order=("alacritty" "knosole" "kgx" "gnome-terminal" "mate-terminal" "xfce4-terminal" "qterminal" "lxterminal" "xterm" "foot") ["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 case "$XDG_CURRENT_DESKTOP" in
KDE) KDE) terminal="konsole" ;;
terminal="konsole" GNOME)
;; if command -v "kgx" &>/dev/null; then
GNOME) terminal="kgx"
if command -v "kgx" &>/dev/null; then else
terminal=kgx terminal="gnome-terminal"
else fi
terminal=gnome-terminal ;;
fi XFCE) terminal="xfce4-terminal" ;;
;; LXQt) terminal="qterminal" ;;
XFCE) MATE) terminal="mate-terminal" ;;
terminal=xfce4-terminal
;;
LXQt)
terminal=qterminal
;;
MATE)
terminal=mate-terminal
;;
esac esac
# Fallback: Check for available terminals
if [ -z "$terminal" ] || ! command -v "$terminal" &>/dev/null; then 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 if command -v "$i" &>/dev/null; then
terminal="$i" terminal="$i"
break break
@@ -71,29 +105,28 @@ if [ -z "$terminal" ] || ! command -v "$terminal" &>/dev/null; then
done done
fi fi
# Error handling if no terminal is found
if [ -z "$terminal" ]; then 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 exit 1
fi fi
# Special case for "kgx" terminal
if [ "$terminal" == "kgx" ]; then if [ "$terminal" == "kgx" ]; then
# shellcheck disable=SC2086 sed -i '2i sleep 0.1' "$initfile"
sed -i '2i sleep 0.1' $initfile echo 'kill -SIGINT $PPID' >>"$initfile"
# shellcheck disable=SC2016
# shellcheck disable=SC2086
echo 'Kill -SIGNINT $PPID' >>$initfile
fi fi
eval "${terminals[${terminal}]}" || { # Launch the selected terminal
exitcode=$? echo -e "${CYAN}Launching command in terminal:${RESET} ${BOLD}${terminal}${RESET}"
} eval "${terminals[${terminal}]}" || exitcode=$?
rm "$initfile" # Clean up temporary files and handle errors
if [ -n "$exitcode" ] && [ "$exitcode" != 130 ]; then
if [ "$codefile" != "$initfile" ]; then echo -e "${RED}Command failed with exit code:${RESET} ${exitcode}"
rm "$codefile"
fi
if [ ! -z "$exitcode" ] && [ "$exitcode" != 130 ]; then
exit 2 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