This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/.local/bin/statusbar/sb-battery

57 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env sh
######################################################################
# @author : swytch (adapted from Luke Smith - lukesmith.xyz)
# @file : sb-battery
# @license : GPLv3
# @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
# Also sends a notification if battery running low
notify() { \
now=$(date +%s)
if [ -e $XDG_CONFIG_HOME/batteryupdate ]; then
old=$(cat $XDG_CONFIG_HOME/batteryupdate)
delta=$(expr $now - $old)
else
delta=$now
fi
[ 300 -gt $delta ] && return;
echo $now > $XDG_CONFIG_HOME/batteryupdate
case "$capacity" in
2021-09-07 23:44:05 +02:00
[2-3][0-9]) notify-send -u "normal" "Battery is running low ($capacity%)" "Please plug your computer to a power source" ;;
*) notify-send -u "critical" "Battery is dangerously low ($capacity%)" "Please plug your computer to a power source - <b>NOW!</b>" ;;
esac
}
2021-09-07 23:44:05 +02:00
total=0
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)"
if [ "$status" = "Full" ]; then
2021-02-16 20:53:50 +01:00
status=" " && capacity="FULL"
printf " %s %s " "$status" "$capacity" && exit
elif [ "$status" = "Charging" ]; then
status=""
else
case "$capacity" in
100|[8-9][0-9]) status="" ;;
2021-02-22 12:06:31 +01:00
[6-7][0-9]) status="" ;;
[4-5][0-9]) status="" ;;
[2-3][0-9]) status="" ;;
2021-09-07 23:44:05 +02:00
*) status="" ;;
esac
fi
2021-09-07 23:44:05 +02:00
[ 40 -gt $total ] && notify;
2021-02-16 20:53:50 +01:00
printf " %s %3d%% " "$status" "$capacity";
2021-09-07 23:52:48 +02:00
done