Skip to content

Instantly share code, notes, and snippets.

@ElijahLynn
Created September 11, 2013 16:46
Show Gist options
  • Save ElijahLynn/6526419 to your computer and use it in GitHub Desktop.
Save ElijahLynn/6526419 to your computer and use it in GitHub Desktop.
Battery info Manually forked from https://github.com/richoH/dotfiles/blob/master/bin/battery Place in your ~/bin folder and now the command 'battery' will show you your battery percentage in Ubuntu. I have this set to always display in my Tmux pane with set -g status-right "#(~/bin/battery) | #[fg=cyan]%d %b %R" Modified so when charging it will…
#!/bin/sh
HEART_FULL=♥
HEART_EMPTY=♡
[ -z "$NUM_HEARTS" ] &&
NUM_HEARTS=5
cutinate()
{
perc=$1
inc=$(( 100 / $NUM_HEARTS))
for i in `seq $NUM_HEARTS`; do
if [ $perc -lt 100 ]; then
echo $HEART_EMPTY
else
echo $HEART_FULL
fi
perc=$(( $perc + $inc ))
done
}
linux_get_bat ()
{
bf=$(cat $BAT_FULL)
bn=$(cat $BAT_NOW)
echo $(( 100 * $bn / $bf ))
}
freebsd_get_bat ()
{
echo "$(sysctl -n hw.acpi.battery.life)"
}
# Do with grep and awk unless too hard
# TODO Identify which machine we're on from the script.
battery_status()
{
case $(uname -s) in
"Linux")
BATPATH=/sys/class/power_supply/BAT0
STATUS=$BATPATH/status
if [ -f $BATPATH/energy_full ]; then
BAT_FULL=$BATPATH/energy_full
else
BAT_FULL=$BATPATH/charge_full
fi
if [ -f $BATPATH/energy_now ]; then
BAT_NOW=$BATPATH/energy_now
else
BAT_NOW=$BATPATH/charge_now
fi
if [ "$1" = `cat $STATUS` -o "$1" = "" ]; then
linux_get_bat
fi
;;
"FreeBSD")
STATUS=`sysctl -n hw.acpi.battery.state`
case $1 in
"Discharging")
if [ $STATUS -eq 1 ]; then
freebsd_get_bat
fi
;;
"Charging")
if [ $STATUS -eq 2 ]; then
freebsd_get_bat
fi
;;
"")
freebsd_get_bat
;;
esac
;;
"Darwin")
case $1 in
"Discharging")
ext="No";;
"Charging")
ext="Yes";;
esac
ioreg -c AppleSmartBattery -w0 | \
grep -o '"[^"]*" = [^ ]*' | \
sed -e 's/= //g' -e 's/"//g' | \
sort | \
while read key value; do
case $key in
"MaxCapacity")
export maxcap=$value;;
"CurrentCapacity")
export curcap=$value;;
"ExternalConnected")
if [ "$ext" != "$value" ]; then
exit
fi
;;
"FullyCharged")
if [ "$value" = "Yes" ]; then
exit
fi
;;
esac
if [[ -n "$maxcap" && -n $curcap ]]; then
echo $(( 100 * $curcap / $maxcap ))
break
fi
done
esac
}
BATTERY_STATUS=`battery_status $1`
[ -z "$BATTERY_STATUS" ] && exit
if [ -n "$CUTE_BATTERY_INDICATOR" ]; then
echo `cutinate $BATTERY_STATUS`
else
echo ${BATTERY_STATUS}%
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment