Skip to content

Instantly share code, notes, and snippets.

@redacted
Last active October 29, 2015 10:26
Show Gist options
  • Save redacted/99a45ede4158a24282fe to your computer and use it in GitHub Desktop.
Save redacted/99a45ede4158a24282fe to your computer and use it in GitHub Desktop.
parts of probably oh-my-zsh for battery monitoring on macs
function battery_pct() {
typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
integer i=$(((currentcapacity/maxcapacity) * 100))
echo $i
}
function battery_pct_remaining() {
if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
battery_pct
else
echo "External Power"
fi
}
function battery_pct_prompt () {
if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
b=$(battery_pct_remaining)
if [ $b -gt 50 ] ; then
color='green'
elif [ $b -gt 20 ] ; then
color='yellow'
else
color='red'
fi
echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
else
echo "∞"
fi
}
# RPROMPT='$(battery_pct_prompt)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment