Files
dotfiles/.config/polybar/scripts/battery.sh
Julian Prein d327ddf79b polybar: Ignore/abort on missing commands/files
Catch if the files needed in the battery module are missing and exit
early.

Do not mess up the bar by printing error messages. Instead abort.
2022-10-16 01:09:59 +02:00

30 lines
756 B
Bash
Executable File

#!/usr/bin/env bash
## config
full=80
low=20
############################
red='%{F#f00}'
green='%{F#0f0}'
end='%{F-}'
bat="$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null)" || exit
ac="$(cat /sys/class/power_supply/AC/online 2>/dev/null)"
declare -a ramp
ramp=(          )
# display in red when under $low and no charger is connected
[[ "$bat" -gt "$low" || "$ac" -eq 1 ]] || color="$red"
# display in green when over $full and a charger is connected
[[ "$bat" -lt "$full" || "$ac" -eq 0 ]] || color="$green"
let "icon_index = $bat / (${#ramp[@]} - 1)"
[[ $icon_index -lt ${#ramp[@]} ]] || icond_index=10
icon="${ramp[$icon_index]}"
[[ "$ac" -eq 0 ]] || charge=""
echo "${color}${icon}${charge} ${bat}%"