Re-wrote battery script to be faster and more efficient.

This commit is contained in:
Archie Hilton (thytom) 2019-10-25 01:24:02 +01:00
parent 2785224db7
commit 7eba91d038

View file

@ -9,36 +9,35 @@ BATTERY_2_ICON=''
BATTERY_3_ICON='' BATTERY_3_ICON=''
BATTERY_4_ICON='' BATTERY_4_ICON=''
FULL_AT=98
BAT_ICON=""
ICON=""
get_battery() get_battery()
{ {
if [ -d /sys/class/power_supply/BAT? ]; then # The vast majority of people only use one battery.
ac_adapter=$(cat /sys/class/power_supply/BAT?/status)
if [ "$ac_adapter" == "Charging" ]; then if [ -d /sys/class/power_supply/BAT0 ]; then
echo "$CHARGING_ICON" capacity=$(cat /sys/class/power_supply/BAT0/capacity)
charging=$(cat /sys/class/power_supply/BAT0/status)
if [[ "$charging" == "Charging" ]]; then
ICON="$CHARGING_ICON"
elif [[ $capacity -le 25 ]]; then
ICON="$WARNING_ICON"
fi fi
# Will show all batteries with approximate icon for remaining power. if [[ $capacity -ge $FULL_AT ]]; then
for x in /sys/class/power_supply/BAT?/capacity; BAT_ICON=$BATTERY_FULL_ICON
do elif [[ $capacity -le 25 ]]; then
case "$(cat $x)" in BAT_ICON=$BATTERY_4_ICON
100) echo "$BATTERY_FULL_ICON" ;; elif [[ $capacity -le 50 ]]; then
9[0-9]) echo "$BATTERY_FULL_ICON $(cat $x)%" ;; BAT_ICON=$BATTERY_3_ICON
8[0-9]|7[0-9]) echo "$BATTERY_2_ICON $(cat $x)%" ;; elif [[ $capacity -le $FULL_AT ]]; then
6[0-9]|5[0-9]) echo "$BATTERY_3_ICON $(cat $x)%" ;; BAT_ICON=$BATTERY_2_ICON
4[0-9]|3[0-9]) echo "$BATTERY_4_ICON $(cat $x)%" ;;
2[0-9]|1[0-9]) if [ "$ac_adapter" == "Charging" ]; then
echo "$BATTERY_4_ICON $(cat $x)%"
else
echo "$WARNING_ICON $BATTERY_4_ICON $(cat $x)%"
fi ;;
[0-9]) if [ "$ac_adapter" == "Charging" ]; then
echo "$BATTERY_4_ICON $(cat $x)%"
else
echo "$WARNING_ICON $BATTERY_4_ICON $(cat $x)%"
fi ;;
esac
done
fi fi
fi
echo "$ICON $BAT_ICON $capacity%"
} }
get_battery get_battery