#!/bin/zsh
bat() {
PROC=/proc/acpi/battery/BAT0
SYS=/sys/class/power_supply/BAT0/uevent
STATE=
# dc: design capacity, rc: remaining capacity
if [ -f $PROC/info ]
then
STATE=$PROC/state # 2.6.24
dc=$(grep 'last full' < $PROC/info | awk '{ print $4 }')
rc=$(grep 'remaining' < $PROC/state | awk '{ print $3 }')
elif [ -f $SYS ]
then
STATE=$SYS # 2.6.25
dc=$(grep '\<power_SUPPLY_CHARGE_FULL\>' < $SYS | awk -F= '{ print $2 }')
rc=$(grep '\<power_SUPPLY_CHARGE_NOW\>' < $SYS | awk -F= '{ print $2 } ')
else
exit
fi
p=$(echo 3k $rc $dc / 100 \* p | dc )
if grep -iq discharging $STATE
then
printf " %02d" "$p"
else
if [ ${p%.*} -lt 100 ]; then
printf " %02d+" "$p"
fi
fi
}
bat