Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Last active September 25, 2018 22:10
Show Gist options
  • Select an option

  • Save lavoiesl/11023856 to your computer and use it in GitHub Desktop.

Select an option

Save lavoiesl/11023856 to your computer and use it in GitHub Desktop.
Apple Battery Status
#!/bin/bash
#
# @link https://gist.github.com/lavoiesl/11023856
ioreg="$(ioreg -c AppleSmartBattery)"
current="$(echo "$ioreg" | grep CurrentCapacity | grep -oE "[0-9]+" | head -n1)"
max="$(echo "$ioreg" | grep MaxCapacity | grep -oE "[0-9]+" | head -n1)"
battery="$(( current * 100 / max ))"
echo -ne '|'
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 -n '+'
elif [[ $val -ge $(( i * 2 - 1)) ]]; then
echo -n '-'
else
echo -n ' '
fi
done
echo -n '|'
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