Skip to content

Instantly share code, notes, and snippets.

@trobrock
Created April 11, 2012 18:16
Show Gist options
  • Save trobrock/2361093 to your computer and use it in GitHub Desktop.
Save trobrock/2361093 to your computer and use it in GitHub Desktop.
Used for showing, and coloring the battery percentage in my tmux status line
#!/bin/zsh
power=$(/usr/sbin/ioreg -l | awk 'BEGIN{a=0;b=0}
$0 ~ "MaxCapacity" {a=$5;next}
$0 ~ "CurrentCapacity" {b=$5;nextfile}
END{printf("%.2f%%", b/a * 100)}')
charging=$(/usr/sbin/ioreg -l | awk '/IsCharging/{ print $5 }')
power_int=$(echo $power | sed 's/\..*//')
output=""
if [ $power_int -gt 75 ]
then
output="$output#[default]"
elif [ $power_int -lt 20 ]
then
output="$output#[fg=red]"
else
output="$output#[fg=yellow]"
fi
if [ $charging = "Yes" ]
then
output="#[default]⟲ $output"
fi
echo "$output$power"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment