diff options
author | Justine Smithies <justine@smithies.me.uk> | 2024-02-07 18:58:43 +0000 |
---|---|---|
committer | Justine Smithies <justine@smithies.me.uk> | 2024-02-07 18:58:43 +0000 |
commit | d1ca9c43aa55859d08ff3b57e2ee676975c30648 (patch) | |
tree | 81cfcb41066ca253f2fd4701529c7740b6dd54ae /.config/eww/scripts/battery.sh | |
parent | 361f12c06b1504c400483a4fabc941e5b3a767a9 (diff) |
Removed python battery script from eww and added bash script
Diffstat (limited to '.config/eww/scripts/battery.sh')
-rwxr-xr-x | .config/eww/scripts/battery.sh | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/.config/eww/scripts/battery.sh b/.config/eww/scripts/battery.sh new file mode 100755 index 0000000..ca11210 --- /dev/null +++ b/.config/eww/scripts/battery.sh @@ -0,0 +1,54 @@ +#!/bin/sh + +# Send a notification if battery is low, change status in info file accordingly +notify_low () { + + # Battery isn't much but is charging (don't notify) + [ "$status" = "Charging" ] && return + + # Battery critically low + if [ "$capacity" -le 10 ] && ! grep -q critically-low "$lowinfo"; then + notify-send -i "$HOME/.config/icons/critical-battery.png" -u critical "Battery critically low!" + echo critically-low >"$lowinfo" + + # Battery is low + elif [ "$capacity" -le 20 ] && ! grep -q low "$lowinfo"; then + notify-send -i "$HOME/.config/icons/low-battery.png" -t 5500 "Battery low!" + echo low >"$lowinfo" + fi +} + +battery="/sys/class/power_supply/BAT0" +status="$(cat "$battery/status")" +capacity="$(cat "$battery/capacity")" + +# Get the low battery status from file ~/.cache/battery-low-status +lowinfo="$HOME/.cache/battery-low-status" +[ ! -e "$lowinfo" ] && touch "$lowinfo" + + +case $1 in + left-click) + ;; + *) + # Set charging icon and capacity icon + [ "$status" = "Charging" ] && charging_icon=" " || charging_icon="" + [ "$capacity" -gt 100 ] && capacity=100 + if [ "$capacity" -lt 15 ]; then capacity_icon=' ' + elif [ "$capacity" -lt 40 ]; then capacity_icon=' ' + elif [ "$capacity" -lt 60 ]; then capacity_icon=' ' + elif [ "$capacity" -lt 90 ]; then capacity_icon=' ' + else capacity_icon=' ' + fi + + # Report low battery + [ "$capacity" -le 20 ] && notify_low + + # Reset low battery information in these cases + { [ "$capacity" -gt 20 ] || [ "$status" = "Charging" ]; } && [ -n "$(cat "$lowinfo")" ] && echo "" >"$lowinfo" + + printf "%s%s%d%%\n" "$charging_icon" "$capacity_icon" "$capacity" + ;; +esac + + |