Skip to content

Instantly share code, notes, and snippets.

@devnore
Last active December 21, 2015 01:39
Show Gist options
  • Save devnore/6229009 to your computer and use it in GitHub Desktop.
Save devnore/6229009 to your computer and use it in GitHub Desktop.
Battery indicator for use with RPROMPT in oh-my-zsh. Has a fallback to displaying time if on a Linux-system with no acpi (haven't tested acpi as i don't have a Linux-laptop here atm)
function battery_charge {
if [[ $(uname -s) == 'Darwin' ]]; then
percent_battery=$(pmset -g batt | grep -o "[0-9]*%" )
percent_battery=${percent_battery:0:-1}
charge="%{$fg_bold[green]%}⚡️ "
if [[ -z $(pmset -g batt |grep "AC Power") ]]; then charge="%{$fg_bold[red]%}🔋 " fi
elif [[ $(uname -s) == 'Linux' ]]; then
percent_battery=0
#if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
# percent_battery="$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')"
#fi
if [[ $percent_battery -eq 0 ]]; then
# Fallback to displaying IP and Time
echo -n '['
echo -n `/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
echo "][%*]"
return 0
fi
fi
color=green
if [[ 20 -ge $percent_battery ]]; then
color=red
elif [[ 50 -ge $percent_battery ]]; then
color=blue
fi
charged=$(( ($percent_battery) / 10 ))
rest=$(( $percent_battery%10 ))
if [[ $rest > 4 ]]; then charged=$(( $charged + 1 )) fi
# print
echo -n "$charge%{$fg_bold[$color]%}"
for ((i = 0; i < $charged; i++)); do echo -n '▸' ; done
for ((i = 0; i < $(( 10 - $charged )); i++)); do echo -n '▹' ;done
echo "%{$reset_color%}" 2>/dev/null
}
@devnore
Copy link
Author

devnore commented Aug 14, 2013

Changed char for displaying charging och discharging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment