random-scripts/battery-notify.sh

40 lines
1018 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)"
if ((${connected} == 1)); then
echo true
else
echo false
fi
}
# 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)"
if ((${battery_percent} > 10)) || "$(is_adapted_connected)"; then
did_notify=false
elif ! ${did_notify}; then
did_notify=true
send_battery_warning ${battery_percent}
fi
sleep 10
done