#!/usr/bin/env fish # listen to battery state and warn on low battery # read low battery state level from environment and fallback to a value of 8 set LOW 8 test -n "$BAT_CRITICAL_LEVEL" -a "$BAT_CRITICAL_LEVEL" -gt 0; and set LOW "$BAT_CRITICAL_LEVEL" # read config from environment set ALARM_TEMP 1000 test -z "$BAT_ALARM_TEMP" -a "$BAT_ALARM_TEMP" -gt 1000; and set ALARM_TEMP "$BAT_ALARM_TEMP" # if we are currently below or above our LOW threshold set isLow "false" # print config echo "Set up with LOW=$LOW and ALARM_TEMP=$ALARM_TEMP" #echo "redshift="(which redshift) function check_btry set state (upower -i /org/freedesktop/UPower/devices/battery_BAT0 | rg "(percent|state)" | awk -F " " '{print $2}') set charge (echo "$state[2]" | string replace "%" "") set state "$state[1]" set c_low (test "$charge" -lt "$LOW"; and echo true; or echo false) if test $c_low = true; and test $state = discharging; and test $isLow != true echo "transitioning to low battery state" low_btry_action set isLow true else if test ! $state = discharging; and test $isLow != false echo "transtioning to high bat state (charger)" set isLow false high_btry_action else if test $c_low = false; and test $isLow != false echo "transitioning to high bat state (magic)" set isLow false high_btry_action end end function low_btry_action # kill redshift job and set fixed alarm temp if jobs -q echo killing redshift with pid (jobs -p) kill (jobs -p) end redshift -P -O "$ALARM_TEMP" end function high_btry_action # start redshift again redshift -P -l 50.11552:8.68417 -t 5500:2500 & end if test "$argv[1]" = once check_btry set state (upower -i /org/freedesktop/UPower/devices/battery_BAT0 | rg "(percent|state)" | awk -F " " '{print $2}') set charge (echo "$state[2]" | string replace "%" "") set state "$state[1]" echo "$charge : $state" else #set percent (upower -i /org/freedesktop/UPower/devices/battery_BAT0 | rg "(percent)" | awk -F " " '{print $2}' | string replace "%" "") # set initial isLow variable to the opposite of what it's supposed to be to trigger a #test "$percent" -lt "$LOW"; and set isLow "false" set isLow unknown sleep 5 while true check_btry sleep (test $isLow = true; and echo 1; or echo 60) end end