1
0
Fork 1
mirror of https://github.com/Zerodya/hyprfreeze.git synced 2024-11-10 01:18:49 +01:00

Sanitize code

This commit is contained in:
Nicola 2024-02-11 18:01:24 +01:00 committed by GitHub
parent 176df68f13
commit a6a7493eba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,24 +33,24 @@ function toggleFreeze() {
if [[ $DRYRUN == "1" ]]; then return 0; fi if [[ $DRYRUN == "1" ]]; then return 0; fi
# Get pids of process tree # Get pids of process tree
PIDS=$(pstree -p $PID | grep -oP '\(\K[^\)]+') PIDS=$(pstree -p "$PID" | grep -oP '\(\K[^\)]+')
debugPrint "PIDs: $PIDS" debugPrint "PIDs: $PIDS"
# Prevent suspending itself # Prevent suspending itself
local pid_of_script=$$ local pid_of_script=$$
if echo $PIDS | grep -q "$pid_of_script"; then if echo "$PIDS" | grep -q "$pid_of_script"; then
echo "You are trying to suspend the hyprfreeze process." echo "You are trying to suspend the hyprfreeze process."
exit 1 exit 1
fi fi
# Suspend or resume processes # Suspend or resume processes
if [[ "$(ps -o state= $PID)" == T ]]; then if [[ "$(ps -o state= "$PID")" == T ]]; then
debugPrint "Resuming processes..." debugPrint "Resuming processes..."
kill -CONT $PIDS 2>/dev/null && echo "Resumed $(ps -p $PID -o comm= 2>/dev/null) (PID $PID)" || exit 1 kill -CONT "$PIDS" 2>/dev/null && echo "Resumed $(ps -p "$PID" -o comm= 2>/dev/null) (PID $PID)" || exit 1
else else
debugPrint "Suspending processes..." debugPrint "Suspending processes..."
kill -STOP $PIDS 2>/dev/null && echo "Suspended $(ps -p $PID -o comm= 2>/dev/null) (PID $PID)" || exit 1 kill -STOP "$PIDS" 2>/dev/null && echo "Suspended $(ps -p "$PID" -o comm= 2>/dev/null) (PID $PID)" || exit 1
fi fi
} }
@ -63,7 +63,7 @@ function getPidByActive() {
function getPidByPid() { function getPidByPid() {
debugPrint "Getting PID by PID: $1" debugPrint "Getting PID by PID: $1"
# Check if process pid exists # Check if process pid exists
if ! ps -p $1 &>/dev/null; then if ! ps -p "$1" &>/dev/null; then
echo "Process ID $1 not found" echo "Process ID $1 not found"
exit 1 exit 1
fi fi
@ -80,7 +80,7 @@ function getPidByName() {
fi fi
# Get last process if there are multiple # Get last process if there are multiple
PID=$(pidof $1 | awk '{print $NF}') PID=$(pidof "$1" | awk '{print $NF}')
debugPrint "PID by name: $PID" debugPrint "PID by name: $PID"
} }
@ -98,23 +98,24 @@ function getPidByProp() {
function printInfo() { function printInfo() {
debugPrint "Printing process info...\n" debugPrint "Printing process info...\n"
echo -e "$(tput bold)Process tree:$(tput sgr0)" echo -e "$(tput bold)Process tree:$(tput sgr0)"
ps -p $PID 2>/dev/null && pstree -p $PID ps -p "$PID" 2>/dev/null && pstree -p "$PID"
echo -e "\n$(tput bold)Process threads:$(tput sgr0)" echo -e "\n$(tput bold)Process threads:$(tput sgr0)"
ps -eLo pid,tid,comm | grep $PID 2>/dev/null ps -eLo pid,tid,comm | grep "$PID" 2>/dev/null
echo -e "\n$(tput bold)Process ID$(tput sgr0) = $PID \ echo -e "\n$(tput bold)Process ID$(tput sgr0) = $PID \
\n$(tput bold)Process name$(tput sgr0) = $(ps -p $PID -o comm= 2>/dev/null) \ \n$(tput bold)Process name$(tput sgr0) = $(ps -p "$PID" -o comm= 2>/dev/null) \
\n$(tput bold)Process state$(tput sgr0) = $(ps -o state= -p $PID 2>/dev/null)\n" \n$(tput bold)Process state$(tput sgr0) = $(ps -o state= -p "$PID" 2>/dev/null)\n"
} }
function sendNotification() { function sendNotification() {
debugPrint "Sending notification..." debugPrint "Sending notification..."
local title=$([[ "$(ps -o state= $PID)" == T ]] && local title
echo "Suspended $(ps -p $PID -o comm= 2>/dev/null)" || title=$( [[ "$(ps -p "$PID" -o state=)" == T ]] &&
echo "Resumed $(ps -p $PID -o comm= 2>/dev/null)") echo "Suspended $(ps -p "$PID" -o comm= 2>/dev/null)" ||
echo "Resumed $(ps -p "$PID" -o comm= 2>/dev/null)")
local message=$(echo "PID $PID") local message="PID $PID"
notify-send "${title}" "${message}" -t "$NOTIF_TIMEOUT" -a Hyprfreeze notify-send "${title}" "${message}" -t "$NOTIF_TIMEOUT" -a Hyprfreeze
} }
@ -126,7 +127,9 @@ function args() {
# Parse options # Parse options
local options="hap:n:rst:" local options="hap:n:rst:"
local long_options="help,active,pid:,name:,prop,silent,notif-timeout:,info,dry-run,debug" local long_options="help,active,pid:,name:,prop,silent,notif-timeout:,info,dry-run,debug"
local parsed_args=$(getopt -o $options --long $long_options -n "$(basename "$0")" -- "$@") local parsed_args
parsed_args=$(getopt -o "$options" --long "$long_options" -n "$(basename "$0")" -- "$@")
eval set -- "$parsed_args" eval set -- "$parsed_args"
while true; do while true; do
case $1 in case $1 in
@ -170,7 +173,6 @@ function args() {
;; ;;
--) --)
shift # Skip -- argument shift # Skip -- argument
COMMAND=${@:2}
break break
;; ;;
*) *)