dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

mac-battery-warning (807B)


      1 #!/usr/bin/env bash
      2 # On macOS, send a notification warning if battery level is under/over a certain limit.
      3 checkdeps() {
      4   for com in "$@"; do
      5     command -v "$com" >/dev/null 2>&1 \
      6       || { printf '%s required but not found.\n' "$com" >&2 && exit 1; }
      7   done
      8 }
      9 checkdeps terminal-notifier
     10 
     11 batt_status=$(pmset -g batt)
     12 battery_pct=$(awk '/InternalBattery/ { print $3 }' <<< "$batt_status" | cut -d% -f1)
     13 if { grep -q "from 'Battery Power'" <<< "$batt_status"; }  && [ "$battery_pct" -le 15 ]; then
     14   /usr/local/bin/terminal-notifier -title "Battery low" -message "Charge your computer" -group battery;
     15 elif { grep -q "from 'AC Power'" <<< "$batt_status"; } && [ "$battery_pct" -ge 95 ]; then
     16   /usr/local/bin/terminal-notifier -title "Charged" -message "You can unplug your charger" -group battery;
     17 fi