neodotfiles/user/.config/suckless/dwmblocks/scripts/block_battery

92 lines
2.7 KiB
Text
Raw Normal View History

2023-09-04 17:36:18 +00:00
#!/usr/bin/env bash
# ***This script was made by Clay Gomera (Drake)***
# - Description: A dwmblocks script to print the current power profile and battery status
# - Dependencies: dwm, dwmblocks, power-profiles-daemon
# this variable will store the current power profile
2022-12-20 00:15:21 +00:00
currntpwr=$(powerprofilesctl get)
if [ "${currntpwr}" = "performance" ]; then
2023-05-26 06:32:03 +00:00
pwr=" | 󰓅 Performance "
2022-12-20 00:15:21 +00:00
elif [ "${currntpwr}" = "balanced" ]; then
2023-05-26 06:32:03 +00:00
pwr=" | 󰾅 Balanced "
2022-12-20 00:15:21 +00:00
elif [ "${currntpwr}" = "power-saver" ]; then
2023-05-26 06:32:03 +00:00
pwr=" | 󰾆 PowerSaver "
2022-12-20 00:15:21 +00:00
fi
2023-09-04 17:36:18 +00:00
# loop through all the available batteries and get it's current capacity
2023-05-26 06:32:03 +00:00
for battery in /sys/class/power_supply/BAT?*; do
2023-09-04 17:36:18 +00:00
# if non-first battery, print a space separator.
2023-05-26 06:32:03 +00:00
[ -n "${capacity+x}" ] && printf " "
2023-09-04 17:36:18 +00:00
# sets up the status and capacity
2023-05-26 06:32:03 +00:00
case "$(cat "$battery/status" 2>&1)" in
"Full")
status=" 󰁹"
;;
"Discharging")
2023-09-04 17:36:18 +00:00
# this variable will store the percentage of remaining charge
percentage="$(cat "$battery/capacity" 2>&1)"
2023-09-04 17:36:18 +00:00
# updates the status icon based on the battery percentage
if [ "$percentage" -le 20 ]; then
status=" 󰁻"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 30 ]; then
status=" 󰁼"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 40 ]; then
status=" 󰁽"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 50 ]; then
status=" 󰁾"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 60 ]; then
status=" 󰁿"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 70 ]; then
status=" 󰂀"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 80 ]; then
status=" 󰂁"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 90 ]; then
status=" 󰂂"
2023-09-04 17:36:18 +00:00
else
status=" 󰁹"
fi
;;
2023-09-04 17:36:18 +00:00
"Charging")
# this variable will store the percentage of remaining charge
percentage="$(cat "$battery/capacity" 2>&1)"
2023-09-04 17:36:18 +00:00
# updates the status icon based on the battery percentage while charging
if [ "$percentage" -le 10 ]; then
status=" 󰢜"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 20 ]; then
status=" 󰂆"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 30 ]; then
status=" 󰂇"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 40 ]; then
status=" 󰂈"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 50 ]; then
status=" 󰢝"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 60 ]; then
status=" 󰂉"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 70 ]; then
status=" 󰢞"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 80 ]; then
status=" 󰂊"
2023-09-04 17:36:18 +00:00
elif [ "$percentage" -le 90 ]; then
status=" 󰂋"
else
status=" 󰂅"
fi
;;
"Not charging")
2023-09-04 17:36:18 +00:00
echo " 󰂃 " && exit 0 # just in case
;;
"Unknown")
2023-09-04 17:36:18 +00:00
echo " 󰂃 " && exit 0 # just in case
;;
2023-09-04 17:36:18 +00:00
*) exit 1 ;; # just exit if there isn't a battery on the system
2023-05-26 06:32:03 +00:00
esac
2023-09-04 17:36:18 +00:00
# will make a warning variable if discharging and low
[ "$status" = " 󰁺" ] && [ "$percentage" -le 10 ] && warn="󱈸"
2023-09-04 17:36:18 +00:00
# print everything
printf "%s%s%d%%%s" "$status" "$warn " "$percentage" "$pwr"; unset warn
2023-05-26 06:32:03 +00:00
done && printf "\\n"