Files
dotfiles/i3/i3/scripts/battery

98 lines
2.1 KiB
Plaintext
Raw Normal View History

2015-09-25 17:14:36 +02:00
#!/bin/bash
rundir="$RUNDIR/batwarn"
logfile="$LOGDIR/batwarn.log"
2015-11-30 23:38:00 +01:00
_PATH_WARN_1="$rundir/batwarn1"
_PATH_WARN_2="$rundir/batwarn2"
2015-09-25 17:14:36 +02:00
2015-11-30 23:38:00 +01:00
_THRESHOLD1=25
_THRESHOLD2=5
2015-09-25 17:14:36 +02:00
acpi_output=$(acpi -b)
log() {
2015-11-30 23:38:00 +01:00
printf '%s\n' "[$(date +%FT%T)] $*" >> $logfile
2015-09-25 17:14:36 +02:00
}
charging() {
2015-11-30 23:38:00 +01:00
[[ "$status" == "C" ]]
2015-09-25 17:14:36 +02:00
}
discharging() {
2015-11-30 23:38:00 +01:00
[[ "$status" == "D" ]]
2015-09-25 17:14:36 +02:00
}
threshold1() {
2015-11-30 23:38:00 +01:00
(( $percent <= $_THRESHOLD1 ))
2015-09-25 17:14:36 +02:00
}
threshold2() {
2015-11-30 23:38:00 +01:00
(( $percent <= $_THRESHOLD2 ))
2015-09-25 17:14:36 +02:00
}
2015-11-30 23:38:00 +01:00
notify_threshold1() {
notify-send --icon dialog-warning "Battery below ${_THRESHOLD2}%" --expire-time 0
}
2015-09-25 17:14:36 +02:00
2015-11-30 23:38:00 +01:00
notify_threshold2() {
notify-send --icon dialog-warning "Battery below ${_THRESHOLD1}%" --expire-time 30000
}
2015-09-25 17:14:36 +02:00
2015-11-30 23:38:00 +01:00
if [[ -n "$acpi_output" ]] ; then
has_battery=1
percent="$(printf '%s' "$acpi_output" | grep -oP '\d+(?=%)')"
if [[ $percent == 100 ]] ; then
status="F"
else
status="$(printf '%s' "$acpi_output" | grep -oP '(?<=: )\w+(?=,)' | cut -c 1 )"
fi
else
has_battery=0
fi
if (( has_battery )) ; then
if discharging ; then
2015-09-25 17:14:36 +02:00
if threshold2 ; then
2015-11-30 23:38:00 +01:00
if [[ ! -f "$_PATH_WARN_2" ]] ; then
log "battery fell below $_THRESHOLD2 percent. issuing warning."
touch "$_PATH_WARN_2"
notify_threshold1
fi
2015-09-25 17:14:36 +02:00
elif threshold1 ; then
2015-11-30 23:38:00 +01:00
if [[ ! -f "$_PATH_WARN_1" ]] ; then
log "battery fell below $_THRESHOLD1 percent. issuing warning."
touch "$_PATH_WARN_1"
notify_threshold2
fi
fi
elif charging ; then
if [[ -f "$_PATH_WARN_1" ]] ; then
log "charging now. resetting warnings."
rm "$_PATH_WARN_1"
2015-09-25 17:14:36 +02:00
fi
2015-11-30 23:38:00 +01:00
[[ -f "$_PATH_WARN_2" ]] && rm "$_PATH_WARN_2"
fi
2015-09-25 17:14:36 +02:00
2015-11-30 23:38:00 +01:00
if threshold2 ; then
color="#FF0000" # red
urgent=1
elif threshold1 ; then
color="#FFFF00" # yellow
urgent=0
2015-09-25 17:14:36 +02:00
else
2015-11-30 23:38:00 +01:00
color="#FFFFFF" # white
urgent=0
2015-09-25 17:14:36 +02:00
fi
2015-11-30 23:38:00 +01:00
printf '%s\n' "${status} ${percent}%"
printf '%s\n'
printf '%s\n' $color
(( $urgent )) && exit 33
exit 0
2015-11-30 23:38:00 +01:00
else
printf '%s\n' "no battery"
printf '%s\n'
printf '%s\n' "#FFFFFF"
fi