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

Better function names and code formatting

This commit is contained in:
Nicola 2023-10-20 12:42:23 +02:00 committed by GitHub
parent ac5e792e40
commit f2b216e0da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function printHelp() { function printHelp() {
cat <<EOF cat <<EOF
Usage: hyprfreeze (-a | -p [pid] | -n [name] | -r) [--info --dry-run] Usage: hyprfreeze (-a | -p [pid] | -n [name] | -r) [--info --dry-run]
Hyprfreeze is an utility to suspend a game process (and other programs) in Hyprland. Hyprfreeze is an utility to suspend a game process (and other programs) in Hyprland.
@ -17,7 +17,7 @@ Options:
EOF EOF
} }
function hyprfreeze() { function toggleFreeze() {
# Skip this function if --dry-run flag was provided # Skip this function if --dry-run flag was provided
if [[ $DRYRUN == "1" ]]; then return 0; fi if [[ $DRYRUN == "1" ]]; then return 0; fi
@ -27,10 +27,10 @@ function hyprfreeze() {
# Pause or resume processes # Pause or resume processes
if [[ "$(ps -o state= $PID)" == T ]]; then if [[ "$(ps -o state= $PID)" == T ]]; then
kill -CONT $PIDS 2>/dev/null && kill -CONT $PIDS 2>/dev/null &&
echo "Resumed $(ps -p $PID -o comm= 2>/dev/null) ($PID)" || exit 1 echo "Resumed $(ps -p $PID -o comm= 2>/dev/null) ($PID)" || exit 1
else else
kill -STOP $PIDS 2>/dev/null && kill -STOP $PIDS 2>/dev/null &&
echo "Stopped $(ps -p $PID -o comm= 2>/dev/null) ($PID)" || exit 1 echo "Stopped $(ps -p $PID -o comm= 2>/dev/null) ($PID)" || exit 1
fi fi
} }
@ -45,18 +45,18 @@ function freezeActive() {
exit 1 exit 1
fi fi
hyprfreeze toggleFreeze
} }
function freezePid() { function freezePid() {
# 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
PID=$1 PID=$1
hyprfreeze toggleFreeze
} }
function freezeName() { function freezeName() {
@ -68,15 +68,15 @@ function freezeName() {
# 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}')
hyprfreeze toggleFreeze
} }
function freezeProp() { function freezeProp() {
PID=$(hyprprop | jq '.pid') PID=$(hyprprop | jq '.pid')
hyprfreeze toggleFreeze
} }
function info() { function printInfo() {
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
@ -91,57 +91,58 @@ function info() {
function args() { function args() {
# Check if no options were passed # Check if no options were passed
if [ -z "$1" ]; then if [ -z "$1" ]; then
printHelp printHelp
exit 1 exit 1
fi fi
# Track valid flags # Track valid flags
local valid_flag_count=0 local valid_flag_count=0
# Parse options # Parse options
local options="hap:n:r" local options="hap:n:r"
local long_options="help,active,pid:,name:,prop,info,dry-run" local long_options="help,active,pid:,name:,prop,info,dry-run"
local parsed_args=$(getopt -o $options --long $long_options -n "$(basename "$0")" -- "$@") local 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
-h|--help) -h | --help)
printHelp printHelp
exit 0 exit 0
;; ;;
-a|--active) -a | --active)
((valid_flag_count++)) ((valid_flag_count++))
FLAG_ACTIVE=true FLAG_ACTIVE=true
;; ;;
-p|--pid) -p | --pid)
((valid_flag_count++)) ((valid_flag_count++))
shift; shift
FLAG_PID="$1" FLAG_PID="$1"
;; ;;
-n|--name) -n | --name)
((valid_flag_count++)) ((valid_flag_count++))
shift; shift
NAME_FLAG="$1" NAME_FLAG="$1"
;; ;;
-r|--prop) -r | --prop)
((valid_flag_count++)) ((valid_flag_count++))
FLAG_PROP=true FLAG_PROP=true
;; ;;
--info) --info)
INFO=1 INFO=1
;; ;;
--dry-run) --dry-run)
DRYRUN=1 DRYRUN=1
;; ;;
--) --)
shift; # Skip -- argument shift # Skip -- argument
COMMAND=${@:2} COMMAND=${@:2}
break;; break
*) ;;
exit 1 *)
;; exit 1
esac ;;
shift esac
shift
done done
# Check if more than one valid flag is provided, or if none was provided # Check if more than one valid flag is provided, or if none was provided
@ -153,14 +154,18 @@ function args() {
function main() { function main() {
# Handle the chosen valid flag # Handle the chosen valid flag
if [ "$FLAG_ACTIVE" = true ]; then freezeActive; if [ "$FLAG_ACTIVE" = true ]; then
elif [ -n "$FLAG_PID" ]; then freezePid "$FLAG_PID"; freezeActive
elif [ -n "$NAME_FLAG" ]; then freezeName "$NAME_FLAG"; elif [ -n "$FLAG_PID" ]; then
elif [ "$FLAG_PROP" = true ]; then freezeProp; freezePid "$FLAG_PID"
elif [ -n "$NAME_FLAG" ]; then
freezeName "$NAME_FLAG"
elif [ "$FLAG_PROP" = true ]; then
freezeProp
fi fi
# Run info function after PID is obtained # Run info function after PID is obtained
if [ $INFO -eq 1 ]; then info; fi if [ $INFO -eq 1 ]; then printInfo; fi
} }
INFO=0 INFO=0