random-scripts/battery-notify.sh

43 lines
1.1 KiB
Bash
Raw Normal View History

2023-04-12 19:33:00 -07:00
#!/usr/bin/env zsh
2023-05-17 22:14:47 -07:00
local system_uname="$(uname)"
if [[ "${system_uname}" = 'Linux' ]]; then
2023-04-12 19:33:00 -07:00
let charge_full="$(cat '/sys/class/power_supply/BAT0/charge_full')."
function get_battery_percent {
let charge_now="$(cat '/sys/class/power_supply/BAT0/charge_now')."
printf '%.0f' "$((charge_now / charge_full * 100))"
}
function is_adapted_connected {
let connected="$(cat /sys/class/power_supply/ADP1/online)"
2023-05-04 15:05:08 -07:00
((${connected} == 1))
2023-04-12 19:33:00 -07:00
}
# Linux ends here
else
2023-05-17 22:14:47 -07:00
echo "${0}: error: unknown os: \"${system_uname}\"" >&2
2023-04-12 19:33:00 -07:00
exit 1
fi
function send_battery_warning {
2023-05-17 22:14:47 -07:00
dunstify -p -t 0 "Low Battery" "Battery is at ${1}%!"
2023-04-12 19:33:00 -07:00
}
local did_notify=false
2023-05-17 22:14:47 -07:00
let notify_id=-1
2023-04-12 19:33:00 -07:00
while true; do
let battery_percent="$(get_battery_percent)"
2023-05-04 15:05:08 -07:00
if ((${battery_percent} > 10)) || is_adapted_connected; then
2023-04-12 19:33:00 -07:00
did_notify=false
2023-05-17 22:14:47 -07:00
if ((${notify_id} > 0)); then
dunstify -C ${notify_id}
notify_id=-1
fi
2023-04-12 19:33:00 -07:00
elif ! ${did_notify}; then
did_notify=true
2023-05-17 22:14:47 -07:00
notify_id="$(send_battery_warning ${battery_percent})"
2023-04-12 19:33:00 -07:00
fi
sleep 10
done