Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Created August 28, 2012 23:06
Show Gist options
  • Save lavoiesl/3505147 to your computer and use it in GitHub Desktop.
Save lavoiesl/3505147 to your computer and use it in GitHub Desktop.
Display battery status on OSX, including progress bar
#!/bin/bash
# Usage: osx-battery-status.sh [width of progress bar]
# If specified, will use $width characters to display a progress bar, padding with spaces
# Uses half block when needed
ioreg="`ioreg -c AppleSmartBattery`"
current=`echo "$ioreg" | grep CurrentCapacity | grep -oE "[0-9]+"`
max=`echo "$ioreg" | grep MaxCapacity | grep -oE "[0-9]+"`
battery=$(( $current * 100 / $max ))
if [[ -n "$1" ]]; then
width="$1"
if [[ "$width" -gt 0 ]]; then
val=$(( $battery * 2 * $width / 100 ))
for (( i = 1; i <= $width; i++ )); do
if [[ $val -ge $(( $i * 2 )) ]]; then
echo -ne '\xE2\x96\x88'
elif [[ $val -ge $(( $i * 2 - 1)) ]]; then
echo -ne '\xE2\x96\x8C'
else
echo -n ' '
fi
done
echo -ne '\xe2\x8e\xb8'
# echo -ne '\xe2\x94\x82'
fi
fi
# Pad with a space
printf '%3d%%' $battery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment