dotfiles/.local/bin/battmon.sh

49 lines
1.2 KiB
Bash

#!/bin/bash
# battmon, the battery monitor 🦇
BAT_LEVEL_WARN=${BAT_LEVEL_WARN:=20}
BAT_LEVEL_CRITICAL=${BAT_LEVEL_CRITICAL:=10}
function _acpib() {
acpi -b | grep -Fv ' 0%'
}
function isDischarging() {
[[ "$(_acpib)" == *"Discharging"* ]] && echo true || echo false
}
function getLevel() {
_acpib | sed -ne 's/.* [0-9].* \([0-9]*\)%.*/\1/p'
}
function getLevelMessage() {
echo "The battery level is $(getLevel)%"
}
function levelIsWarn() {
[[ $(getLevel) -le $BAT_LEVEL_WARN ]] && echo true || echo false
}
function levelIsCritical() {
[[ $(getLevel) -le $BAT_LEVEL_CRITICAL ]] && echo true || echo false
}
if [[ $(_acpib | wc -l) -gt 0 ]]; then
echo "No batteries installed."
exit 0
fi
while true; do
if eval "$(isDischarging)"; then
if eval "$(levelIsCritical)"; then
i3-nagbar -t warning -m "$(getLevelMessage) which is below the critical level of $BAT_LEVEL_CRITICAL%. The computer will be suspended in 30 seconds unless charged."
sleep 30
eval "$(levelIsCritical)" && systemctl suspend
elif eval "$(levelIsWarn)"; then
notify-send "$(getLevelMessage)"
fi
fi
sleep 3
done