2021-02-13 20:17:23 +01:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
######################################################################
|
2021-02-15 00:12:27 +01:00
|
|
|
# @author : swytch (adapted from Luke Smith - lukesmith.xyz)
|
2021-02-13 20:17:23 +01:00
|
|
|
# @file : sb-battery
|
2021-02-15 00:37:34 +01:00
|
|
|
# @license : GPLv3
|
2021-02-13 20:17:23 +01:00
|
|
|
# @created : Saturday Feb 13, 2021 17:19:04 CET
|
|
|
|
#
|
|
|
|
# @description : battery block dwmblocks
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
|
|
# Will show all batteries with approximate icon for remaining power.
|
|
|
|
# Or show that the computer is plugged to a power source
|
|
|
|
# In any case, show the remaining battery percentage
|
2021-02-13 20:32:03 +01:00
|
|
|
# Also sends a notification if battery running low
|
|
|
|
|
2021-10-23 02:54:42 +02:00
|
|
|
time="$XDG_STATE_HOME/batteryupdate"
|
2021-02-13 20:32:03 +01:00
|
|
|
notify() { \
|
2021-02-22 12:06:11 +01:00
|
|
|
now=$(date +%s)
|
2021-10-23 02:54:42 +02:00
|
|
|
if [ -e $time ]; then
|
|
|
|
old=$(cat $time)
|
2021-02-22 12:06:11 +01:00
|
|
|
delta=$(expr $now - $old)
|
|
|
|
else
|
|
|
|
delta=$now
|
|
|
|
fi
|
|
|
|
[ 300 -gt $delta ] && return;
|
2021-10-23 02:54:42 +02:00
|
|
|
echo $now > $time
|
2021-09-23 23:32:27 +02:00
|
|
|
case "$capacity" in
|
|
|
|
2[0-9]) notify-send -u "normal" "Battery is running low ($capacity%)" "Please plug your computer to a power source" ;;
|
2021-09-07 23:44:05 +02:00
|
|
|
*) notify-send -u "critical" "Battery is dangerously low ($capacity%)" "Please plug your computer to a power source - <b>NOW!</b>" ;;
|
2021-02-13 20:32:03 +01:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:44:05 +02:00
|
|
|
total=0
|
2021-02-13 20:17:23 +01:00
|
|
|
for bat in /sys/class/power_supply/BAT?/
|
|
|
|
do
|
|
|
|
status="$(cat "$bat/status")"
|
|
|
|
capacity="$(cat "$bat/capacity")"
|
2021-09-07 23:44:05 +02:00
|
|
|
total="$(expr $total + $capacity)"
|
2021-09-09 22:18:20 +02:00
|
|
|
slot="$(basename $bat)"
|
2021-09-19 12:32:33 +02:00
|
|
|
if [ "Full" = "$status" ]; then
|
2021-02-16 20:53:50 +01:00
|
|
|
status=" " && capacity="FULL"
|
2021-09-09 22:18:20 +02:00
|
|
|
printf " %s:%s(%s) " "$slot" "$status" "$capacity"
|
|
|
|
else
|
2022-06-11 12:24:01 +02:00
|
|
|
if [ 100 -lt $(expr $capacity) ]; then
|
|
|
|
status=""
|
2021-09-09 22:18:20 +02:00
|
|
|
else
|
2022-06-11 12:24:01 +02:00
|
|
|
if [ "$status" = "Charging" ]; then
|
|
|
|
status=""
|
|
|
|
else
|
|
|
|
case "$capacity" in
|
|
|
|
100|[8-9][0-9]) status="" ;;
|
|
|
|
[6-7][0-9]) status="" ;;
|
|
|
|
[4-5][0-9]) status="" ;;
|
|
|
|
[2-3][0-9]) status="" ;;
|
|
|
|
*) status="" ;;
|
|
|
|
esac
|
|
|
|
[ "BAT0" = $slot ] && [ 30 -gt $capacity ] && notify;
|
|
|
|
fi
|
|
|
|
printf " %s:%s (%0.2d%%) " "$slot" "$status" "$capacity";
|
2021-09-09 22:18:20 +02:00
|
|
|
fi
|
|
|
|
fi
|
2021-09-07 23:52:48 +02:00
|
|
|
done
|