mirror of
https://github.com/Snigdha-OS/snigdhaos-system-config.git
synced 2025-09-21 20:14:58 +02:00
⚡️ perf: add explanation
This commit is contained in:
@@ -3,10 +3,11 @@
|
|||||||
# Author: Garuda Linux
|
# Author: Garuda Linux
|
||||||
# Adopted for Snigdha OS
|
# Adopted for Snigdha OS
|
||||||
|
|
||||||
set -euo pipefail # Enable strict error handling
|
set -euo pipefail # Enable strict error handling to ensure errors are caught
|
||||||
|
|
||||||
LAUNCH_TERMINAL_SHELL="bash"
|
LAUNCH_TERMINAL_SHELL="bash" # Default shell to launch in the terminal
|
||||||
|
|
||||||
|
# Usage function to display how the script is used
|
||||||
usage() {
|
usage() {
|
||||||
echo "Usage: ${0##*/} [options] [command]"
|
echo "Usage: ${0##*/} [options] [command]"
|
||||||
echo
|
echo
|
||||||
@@ -17,16 +18,16 @@ usage() {
|
|||||||
echo "Example:"
|
echo "Example:"
|
||||||
echo " ${0##*/} -s zsh 'echo Hello, World!'"
|
echo " ${0##*/} -s zsh 'echo Hello, World!'"
|
||||||
echo " ${0##*/} -h"
|
echo " ${0##*/} -h"
|
||||||
exit 1
|
exit 1 # Exit after displaying the usage information
|
||||||
}
|
}
|
||||||
|
|
||||||
# Parse options
|
# Parse options and arguments
|
||||||
opts="s:h"
|
opts="s:h" # Define the options we expect
|
||||||
while getopts "${opts}" arg; do
|
while getopts "${opts}" arg; do # Loop through each option provided
|
||||||
case "${arg}" in
|
case "${arg}" in
|
||||||
s) LAUNCH_TERMINAL_SHELL="$OPTARG" ;;
|
s) LAUNCH_TERMINAL_SHELL="$OPTARG" ;; # Set the shell if provided with -s
|
||||||
h) usage ;;
|
h) usage ;; # Show usage if -h is provided
|
||||||
?) usage ;;
|
?) usage ;; # Catch any invalid options and show usage
|
||||||
*)
|
*)
|
||||||
echo "Invalid option: ${arg}"
|
echo "Invalid option: ${arg}"
|
||||||
usage
|
usage
|
||||||
@@ -34,36 +35,36 @@ while getopts "${opts}" arg; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# Shift away the processed options
|
# Shift away the processed options, leaving only the command
|
||||||
shift $((OPTIND - 1))
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
# Check if a command was passed
|
# Ensure a command is provided
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
echo "Error: No command provided."
|
echo "Error: No command provided."
|
||||||
usage
|
usage # Show usage if no command is provided
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create temporary files for the shell scripts
|
# Create temporary files to store shell scripts
|
||||||
initfile=$(mktemp)
|
initfile=$(mktemp) # Temp file to store the initialization script
|
||||||
codefile=$initfile
|
codefile=$initfile # Initially, set codefile to the initfile
|
||||||
|
|
||||||
# Generate the initial shell script
|
# Generate the initial shell script
|
||||||
echo "#!/usr/bin/env bash" > "$initfile"
|
echo "#!/usr/bin/env bash" > "$initfile" # Start the script with bash
|
||||||
|
|
||||||
# If user has specified a shell, set it in the code
|
# If the user specifies a shell, use it instead of bash
|
||||||
if [ "$LAUNCH_TERMINAL_SHELL" != "bash" ]; then
|
if [ "$LAUNCH_TERMINAL_SHELL" != "bash" ]; then
|
||||||
codefile=$(mktemp)
|
codefile=$(mktemp) # Create another temp file for the specified shell
|
||||||
echo "$LAUNCH_TERMINAL_SHELL $codefile" >> "$initfile"
|
echo "$LAUNCH_TERMINAL_SHELL $codefile" >> "$initfile" # Add shell command to initfile
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Write the user's command to the code file
|
# Write the user's command into the code file
|
||||||
echo "$1" >> "$codefile"
|
echo "$1" >> "$codefile" # Add the user command to the code file
|
||||||
chmod +x "$initfile"
|
chmod +x "$initfile" # Make the initfile executable
|
||||||
|
|
||||||
# Command to run in the terminal emulator
|
# Define the command that will be run in the terminal
|
||||||
cmd="$initfile"
|
cmd="$initfile"
|
||||||
|
|
||||||
# Define a dictionary of supported terminal emulators
|
# Define a dictionary of supported terminal emulators and their commands
|
||||||
declare -A terminals=(
|
declare -A terminals=(
|
||||||
["alacritty"]="alacritty -e bash -c \"$cmd\" || LIBGL_ALWAYS_SOFTWARE=1 alacritty -e bash -c \"$cmd\""
|
["alacritty"]="alacritty -e bash -c \"$cmd\" || LIBGL_ALWAYS_SOFTWARE=1 alacritty -e bash -c \"$cmd\""
|
||||||
["konsole"]="konsole --noclose -e bash -c \"$cmd\""
|
["konsole"]="konsole --noclose -e bash -c \"$cmd\""
|
||||||
@@ -86,21 +87,21 @@ declare -a term_order=(
|
|||||||
# Select the terminal based on the desktop environment
|
# Select the terminal based on the desktop environment
|
||||||
terminal=""
|
terminal=""
|
||||||
case "$XDG_CURRENT_DESKTOP" in
|
case "$XDG_CURRENT_DESKTOP" in
|
||||||
KDE) terminal="konsole" ;;
|
KDE) terminal="konsole" ;; # If in KDE, try Konsole
|
||||||
GNOME)
|
GNOME)
|
||||||
if command -v "kgx" &> /dev/null; then
|
if command -v "kgx" &> /dev/null; then
|
||||||
terminal="kgx"
|
terminal="kgx" # If KGX is available, use it for GNOME
|
||||||
else
|
else
|
||||||
terminal="gnome-terminal"
|
terminal="gnome-terminal" # Fallback to gnome-terminal if KGX is not available
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
XFCE) terminal="xfce4-terminal" ;;
|
XFCE) terminal="xfce4-terminal" ;; # If in XFCE, use xfce4-terminal
|
||||||
LXQt) terminal="qterminal" ;;
|
LXQt) terminal="qterminal" ;; # For LXQt, use qterminal
|
||||||
MATE) terminal="mate-terminal" ;;
|
MATE) terminal="mate-terminal" ;; # For MATE, use mate-terminal
|
||||||
*)
|
*)
|
||||||
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" # Try to find any terminal from the list
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@@ -111,33 +112,33 @@ esac
|
|||||||
if [ -z "$terminal" ]; then
|
if [ -z "$terminal" ]; then
|
||||||
notify-send -t 15000 --app-name="Snigdha OS" "No terminal installed" \
|
notify-send -t 15000 --app-name="Snigdha OS" "No terminal installed" \
|
||||||
"No supported terminal emulator is installed. Please install a terminal emulator like Alacritty."
|
"No supported terminal emulator is installed. Please install a terminal emulator like Alacritty."
|
||||||
exit 1
|
exit 1 # Exit if no terminal is found
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Debugging information
|
# Debugging information: Display selected terminal and command
|
||||||
echo "Selected terminal: $terminal"
|
echo "Selected terminal: $terminal"
|
||||||
echo "Command to execute: ${terminals[$terminal]}"
|
echo "Command to execute: ${terminals[$terminal]}"
|
||||||
|
|
||||||
# Special handling for kgx to ensure proper behavior
|
# Special handling for kgx to ensure proper behavior
|
||||||
if [ "$terminal" = "kgx" ]; then
|
if [ "$terminal" = "kgx" ]; then
|
||||||
sed -i '2i sleep 0.1' "$initfile"
|
sed -i '2i sleep 0.1' "$initfile" # Add a small sleep to avoid issues
|
||||||
echo 'kill -SIGINT $PPID' >> "$initfile"
|
echo 'kill -SIGINT $PPID' >> "$initfile" # Send SIGINT to the parent process to close KGX
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Run the terminal emulator with the command
|
# Run the selected terminal with the command
|
||||||
exitcode=0
|
exitcode=0 # Default exit code
|
||||||
eval "${terminals[$terminal]}" || exitcode=$?
|
eval "${terminals[$terminal]}" || exitcode=$? # Run the terminal command and capture exit code
|
||||||
|
|
||||||
# Clean up temporary files
|
# Clean up temporary files
|
||||||
rm "$initfile"
|
rm "$initfile" # Remove the initial script
|
||||||
if [ "$codefile" != "$initfile" ]; then
|
if [ "$codefile" != "$initfile" ]; then
|
||||||
rm "$codefile"
|
rm "$codefile" # Remove the code file if it's different from the initfile
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Exit handling
|
# Exit handling
|
||||||
if [ "$exitcode" -eq 0 ]; then
|
if [ "$exitcode" -eq 0 ]; then
|
||||||
echo "Terminal executed the command successfully."
|
echo "Terminal executed the command successfully." # Inform user of success
|
||||||
elif [ "$exitcode" -ne 130 ]; then
|
elif [ "$exitcode" -ne 130 ]; then
|
||||||
echo "Error: Terminal launch failed with exit code $exitcode"
|
echo "Error: Terminal launch failed with exit code $exitcode" # Inform user of failure
|
||||||
exit "$exitcode"
|
exit "$exitcode" # Exit with the error code if terminal launch failed
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user