random-scripts/battery-notify.sh

36 lines
935 B
Bash
Raw Normal View History

2023-04-12 19:33:00 -07:00
#!/usr/bin/env zsh
if [[ "$(uname)" = 'Linux' ]]; then
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
echo "${0}: error: unknown os: \"$(uname)\"" >&2
exit 1
fi
function send_battery_warning {
notify-send -t 0 "Low Battery" "Battery is at ${1}%!"
}
local did_notify=false
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
elif ! ${did_notify}; then
did_notify=true
send_battery_warning ${battery_percent}
fi
sleep 10
done