random-scripts/old/x11/battery-notify.sh

43 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env zsh
local system_uname="$(uname)"
if [[ "${system_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)"
((${connected} == 1))
}
# Linux ends here
else
echo "${0}: error: unknown os: \"${system_uname}\"" >&2
exit 1
fi
function send_battery_warning {
dunstify -p -t 0 "Low Battery" "Battery is at ${1}%!"
}
local did_notify=false
let notify_id=-1
while true; do
let battery_percent="$(get_battery_percent)"
if ((${battery_percent} > 10)) || is_adapted_connected; then
did_notify=false
if ((${notify_id} > 0)); then
dunstify -C ${notify_id}
notify_id=-1
fi
elif ! ${did_notify}; then
did_notify=true
notify_id="$(send_battery_warning ${battery_percent})"
fi
sleep 10
done