Skip to content

Commit

Permalink
zshrc: rewrite battery function
Browse files Browse the repository at this point in the history
Now supports multiple batteries
Now indicates status (^ Charging, v Discharging, = Full or Unknown)
Now supports different functions for different OSes

TODO: add configurability (zstyle, etc.)
TODO: add support for other OSes (ATM, only Linux is supported)
  • Loading branch information
moviuro authored and ft committed Sep 15, 2014
1 parent 0731b9e commit 287c348
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions etc/zsh/zshrc
Expand Up @@ -1649,28 +1649,49 @@ PS4='+%N:%i:%_> '
# - debian_chroot
# - vcs_info setup and version specific fixes

# display battery status on right side of prompt via running 'BATTERY=1 zsh'
# display battery status on right side of prompt using 'BATTERY=1' in .zshrc.pre
if [[ $BATTERY -gt 0 ]] ; then
if ! check_com -c acpi ; then
if ! islinux ; then
# not yet supported
BATTERY=0
fi
fi

battery() {
if [[ $BATTERY -gt 0 ]] ; then
PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
if [[ -z "$PERCENT" ]] ; then
PERCENT='acpi not present'
else
if [[ "$PERCENT" -lt 20 ]] ; then
PERCENT="warning: ${PERCENT}%%"
else
PERCENT="${PERCENT}%%"
fi
if islinux ; then
batterylinux
fi
fi
}

batterylinux(){
PERCENT=''
local batteries bat capacity
batteries=( /sys/class/power_supply/BAT*(N) )
if (( $#batteries > 0 )) ; then
for bat in $batteries ; do
capacity=$(< $bat/capacity)
case $(< $bat/status) in
Charging)
PERCENT+=" ^"
;;
Discharging)
if (( capacity < 20 )) ; then
PERCENT+=" !v"
else
PERCENT+=" v"
fi
;;
*) # Full, Unknown
PERCENT+=" ="
;;
esac
PERCENT+="${capacity}%%"
done
fi
}

# set variable debian_chroot if running in a chroot with /etc/debian_chroot
if [[ -z "$debian_chroot" ]] && [[ -r /etc/debian_chroot ]] ; then
debian_chroot=$(</etc/debian_chroot)
Expand Down

0 comments on commit 287c348

Please sign in to comment.