neodotfiles/source/dmenu/scripts/dmenu_power

138 lines
4.3 KiB
Text
Raw Normal View History

2024-03-28 21:40:01 +00:00
#!/usr/bin/env bash
# ***This script was made by Clay Gomera (Drake)***
# - Description: A simple power menu dmenu script
# - Dependencies: dmenu, power-profiles-daemon, slock
#######################
## Main manu 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)
## This variable will store the current wallpaper set by feh
currentwall=$(tail --lines=1 "$HOME/.fehbg" | awk '{print $4}' | sed "s/'//g")
2024-08-03 06:35:58 +00:00
# Check for missing dependencies
check_dependencies() {
if ! [ -x "$(command -v dmenu)" ]; then
echo "Missing dependency: dmenu 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" | dmenu -l 2 -i -p "$prompt" | grep -q " Yes"
}
####################
# Main Script Flow #
####################
# Check for dependencies
check_dependencies
2024-03-28 21:40:01 +00:00
action=$(echo -e "$options" | dmenu -i -p " Power Options"); # main menu prompt
2024-08-03 06:35:58 +00:00
if [ -z "$action" ]; then
exit 0
fi
2024-03-28 21:40:01 +00:00
case "$action" in
2024-08-03 06:35:58 +00:00
"$option1")
if confirm_action " Are you sure you want to logout?"; then
pkill X
fi
2024-03-28 21:40:01 +00:00
;;
"$option2")
2024-08-03 06:35:58 +00:00
if confirm_action " Are you sure you want to reboot?"; then
loginctl reboot
fi
2024-03-28 21:40:01 +00:00
;;
"$option3")
2024-08-03 06:35:58 +00:00
if confirm_action " Are you sure you want to power off?"; then
loginctl poweroff
fi
;;
2024-03-28 21:40:01 +00:00
"$option4")
2024-06-03 20:01:31 +00:00
slock -b "$currentwall" systemctl suspend
2024-03-28 21:40:01 +00:00
;;
"$option5")
2024-06-03 20:01:31 +00:00
slock -b "$currentwall"
2024-03-28 21:40:01 +00:00
;;
"$option6")
#####
## These conditions will be used for the prompt
#####
if [ "$currentpwr" = "performance" ]; then
2024-06-03 20:01:31 +00:00
currentpwr="$pwr1"
2024-03-28 21:40:01 +00:00
elif [ "$currentpwr" = "balanced" ]; then
2024-06-03 20:01:31 +00:00
currentpwr="$pwr2"
2024-03-28 21:40:01 +00:00
elif [ "$currentpwr" = "power-saver" ]; then
2024-06-03 20:01:31 +00:00
currentpwr="$pwr3"
2024-03-28 21:40:01 +00:00
fi
2024-06-03 20:01:31 +00:00
pwraction=$(echo -e "$pwrs" | dmenu -i -p " Power Profile Menu - Currently set to: $currentpwr") # power profiles submenu prompt
2024-03-28 21:40:01 +00:00
case "$pwraction" in
"$pwr1")
if [ "$currentpwr" = "$pwr1" ]; then # if the power profile is already set to performance
2024-06-03 20:01:31 +00:00
notify-send "The power profile is already set to performance"
exit 1
2024-03-28 21:40:01 +00:00
else
2024-06-03 20:01:31 +00:00
powerprofilesctl set performance && notify-send "Power profile switched to performance" # if not, set the powerprofile to performance
2024-03-28 21:40:01 +00:00
fi
;;
"$pwr2")
if [ "$currentpwr" = "$pwr2" ]; then # if the power profile is already set to balanced
2024-06-03 20:01:31 +00:00
notify-send "The power profile is already set to balanced"
exit 1
2024-03-28 21:40:01 +00:00
else
2024-06-03 20:01:31 +00:00
powerprofilesctl set balanced && notify-send "Power profile switched to balanced" # if not, set the powerprofile to balanced
2024-03-28 21:40:01 +00:00
fi
;;
"$pwr3")
if [ "$currentpwr" = "$pwr3" ]; then # if the power profile is already set to power saver
2024-06-03 20:01:31 +00:00
notify-send "The power profile is already set to power saver"
exit 1
2024-03-28 21:40:01 +00:00
else
2024-06-03 20:01:31 +00:00
powerprofilesctl set power-saver && notify-send "Power profile switched to power saver" # if not, set the powerprofile to power saver
2024-03-28 21:40:01 +00:00
fi
;;
"$pwr4")
2024-06-03 20:01:31 +00:00
exit 0
2024-03-28 21:40:01 +00:00
esac;;
"$option7")
2024-06-03 20:01:31 +00:00
exit 0
;;
2024-03-28 21:40:01 +00:00
esac