neodotfiles/config/rofi/scripts/rs_power
2024-09-27 23:08:07 -04:00

146 lines
4.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# rs_power - A simple power options menu script for rofi/dmenu/wofi
# Author: Clay Gomera (Drake)
# Dependencies: rofi, power-profiles-daemon (powerprofilesctl), loginctl, libnotify (notify-send)
#######################
## Main menu options ##
#######################
option1=" Logout"
option2=" Reboot"
option3=" Power off"
option4="󰒲 Suspend"
option5=" Lock"
option6=" Change power profile"
option7=" Exit"
options="$option1\n$option2\n$option3\n$option4\n$option5\n$option6\n$option7"
####################################
## Power profiles submenu options ##
####################################
pwr1="󰓅 Performance"
pwr2="󰾅 Balanced"
pwr3="󰾆 Power Saver"
pwr4=" Cancel"
pwrs="$pwr1\n$pwr2\n$pwr3\n$pwr4"
## This variable will store the current power profile
currentpwr=$(powerprofilesctl get)
if [ "$currentpwr" = "performance" ]; then
currentpwr="$pwr1"
elif [ "$currentpwr" = "balanced" ]; then
currentpwr="$pwr2"
elif [ "$currentpwr" = "power-saver" ]; then
currentpwr="$pwr3"
fi
# prompts
prompt1=" Power Options  "
prompt2=" Power Profiles - Currently set to: $currentpwr"
########################
# Function Definitions #
########################
# Check for missing dependencies
check_dependencies() {
local run_launcher_found=false
for launcher in rofi; do
if command -v "$launcher" &> /dev/null; then
run_launcher_found=true
break
fi
done
if [ "$run_launcher_found" = false ]; then
echo "Missing dependency: one of rofi, dmenu, or wofi is required."
exit 1
fi
local missing_deps=()
for dep in loginctl powerprofilesctl notify-send; do
if ! command -v "$dep" &> /dev/null; then
missing_deps+=("$dep")
fi
done
if [ ${#missing_deps[@]} -ne 0 ]; then
echo "Missing dependencies: ${missing_deps[*]}"
exit 1
fi
}
## Ask yes/no for action confirmation
confirm_action() {
local prompt="$1"
echo -e " Yes\n No" | $RUNNER -i -p "$prompt" | grep -q " Yes"
}
####################
# Main Script Flow #
####################
# Check for dependencies
check_dependencies
action=$(echo -e "$options" | $RUNNER -i -p "$prompt1") # main menu prompt
if [ -z "$action" ]; then
exit 0
fi
case "$action" in
"$option1")
if confirm_action " Are you sure you want to logout?"; then
loginctl kill-session self
fi
;;
"$option2")
if confirm_action " Are you sure you want to reboot?"; then
systemctl reboot
fi
;;
"$option3")
if confirm_action " Are you sure you want to power off?"; then
systemctl poweroff
fi
;;
"$option4")
systemctl suspend
;;
"$option5")
loginctl lock-session
;;
"$option6")
pwraction=$(echo -e "$pwrs" | $RUNNER -l 6 -i -p "$prompt2")
case "$pwraction" in
"$pwr1")
if [ "$currentpwr" = "$pwr1" ]; then
notify-send "The power profile is already set to performance"
exit 1
else
powerprofilesctl set performance && notify-send "Power profile switched to performance"
fi
;;
"$pwr2")
if [ "$currentpwr" = "$pwr2" ]; then
notify-send "The power profile is already set to balanced"
exit 1
else
powerprofilesctl set balanced && notify-send "Power profile switched to balanced"
fi
;;
"$pwr3")
if [ "$currentpwr" = "$pwr3" ]; then
notify-send "The power profile is already set to power saver"
exit 1
else
powerprofilesctl set power-saver && notify-send "Power profile switched to power saver"
fi
;;
"$pwr4")
exit 0
esac;;
"$option7")
exit 0;
esac