Add battery watch

This commit is contained in:
Przemek Grondek 2023-05-29 01:15:27 +02:00
parent 9b068ef6c4
commit 05f1170b61
2 changed files with 44 additions and 0 deletions

View File

@ -13,6 +13,7 @@ if [[ "$DESKTOP_SESSION" = "dwm" || "$DESKTOP_SESSION" == "dwm-gnome" ]]; then
dunst &
numlockx on &
nm-applet &
battery-watch &
if command -v barrier &> /dev/null ; then
barrier &

43
bin/battery-watch Executable file
View File

@ -0,0 +1,43 @@
#!/bin/sh
# https://www.reddit.com/r/suckless/comments/m6r71v/comment/gr7t2z1/?utm_source=reddit&utm_medium=web2x&context=3
# Control variable
# Possible values: NONE, FULL, LOW, CRITICAL
last="NONE"
# Default values for LOW/CRITICAL status
low=15
critical=10
while true; do
# If battery is plugged, do stuff
battery="/sys/class/power_supply/BAT0"
if [ -d $battery ]; then
capacity=$(cat $battery/capacity)
status=$(cat $battery/status)
# If battery full and not already warned about that
if [ "$last" != "FULL" ] && [ "$status" = "Full" ]; then
notify-send "Battery full"
last="FULL"
fi
# If low and discharging
if [ "$last" != "LOW" ] && [ "$status" = "Discharging" ] && \
[ $capacity -le $low ]; then
notify-send "Battery low: $capacity%"
last=LOW
fi
# If critical and discharging
if [ "$status" = "Discharging" ] && [ $capacity -le $critical ]; then
notify-send -u critical "Battery very low: $capacity%"
last=CRITICAL
fi
fi
sleep 60
done