dotfiles/bin/battery-watch

46 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2023-05-28 23:15:27 +00:00
#!/bin/sh
# source of first version: https://www.reddit.com/r/suckless/comments/m6r71v/comment/gr7t2z1/?utm_source=reddit&utm_medium=web2x&context=3
# My changes:
# * fixed double notifications for critical level
# * added dunst stack tag
# * added dunst value for progress bar
2023-05-28 23:15:27 +00:00
# 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 -h string:x-dunst-stack-tag:battery_notif -h "int:value:$last" "Battery full"
2023-05-28 23:15:27 +00:00
last="FULL"
fi
# If low and discharging
if [ "$last" != "LOW" ] && [ "$status" = "Discharging" ] && [ $capacity -le $low ] && [ $capacity -gt $critical ]; then
notify-send -h string:x-dunst-stack-tag:battery_notif -h "int:value:$last" "Battery low: $capacity%"
2023-05-28 23:15:27 +00:00
last=LOW
fi
# If critical and discharging
if [ "$status" = "Discharging" ] && [ $capacity -le $critical ]; then
notify-send -h string:x-dunst-stack-tag:battery_notif -h "int:value:$last" -u critical "Battery very low: $capacity%"
2023-05-28 23:15:27 +00:00
last=CRITICAL
fi
fi
sleep 60
done