diff --git a/battery-notify.sh b/battery-notify.sh new file mode 100755 index 0000000..4d9086b --- /dev/null +++ b/battery-notify.sh @@ -0,0 +1,39 @@ +#!/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