battery-notify.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env zsh
  2. local system_uname="$(uname)"
  3. if [[ "${system_uname}" = 'Linux' ]]; then
  4. let charge_full="$(cat '/sys/class/power_supply/BAT0/charge_full')."
  5. function get_battery_percent {
  6. let charge_now="$(cat '/sys/class/power_supply/BAT0/charge_now')."
  7. printf '%.0f' "$((charge_now / charge_full * 100))"
  8. }
  9. function is_adapted_connected {
  10. let connected="$(cat /sys/class/power_supply/ADP1/online)"
  11. ((${connected} == 1))
  12. }
  13. # Linux ends here
  14. else
  15. echo "${0}: error: unknown os: \"${system_uname}\"" >&2
  16. exit 1
  17. fi
  18. function send_battery_warning {
  19. dunstify -p -t 0 "Low Battery" "Battery is at ${1}%!"
  20. }
  21. local did_notify=false
  22. let notify_id=-1
  23. while true; do
  24. let battery_percent="$(get_battery_percent)"
  25. if ((${battery_percent} > 10)) || is_adapted_connected; then
  26. did_notify=false
  27. if ((${notify_id} > 0)); then
  28. dunstify -C ${notify_id}
  29. notify_id=-1
  30. fi
  31. elif ! ${did_notify}; then
  32. did_notify=true
  33. notify_id="$(send_battery_warning ${battery_percent})"
  34. fi
  35. sleep 10
  36. done