Created
July 10, 2012 13:52
-
-
Save brymck/3083365 to your computer and use it in GitHub Desktop.
zsh battery display for OS X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A purely .zsh method inspired by Steve Losh's Python script for displaying battery power with zsh: | |
| # http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/#my-right-prompt-battery-capacity | |
| # | |
| # For best results assign to RPROMPT | |
| function battery_charge { | |
| # Adjust to your preferred number of segments | |
| typeset -i SEGMENTS | |
| SEGMENTS=10 | |
| # Get maximum and current capacity as floats via ioreg | |
| results="$(ioreg -rc AppleSmartBattery)" | |
| typeset -F max_capacity | |
| typeset -F current_capacity | |
| max_capacity="$(echo $results | grep 'MaxCapacity' | awk '{print $3}')" | |
| current_capacity="$(echo $results | grep 'CurrentCapacity' | awk '{print $3}')" | |
| # Calculate the number of green, yellow and red segments | |
| segments_left=$(( $current_capacity / $max_capacity * $SEGMENTS )) | |
| typeset -i green_segments | |
| typeset -i yellow_segments | |
| typeset -i red_segments | |
| green_segments=$segments_left | |
| yellow_segments=$(( $segments_left - $green_segments > 0.5 )) | |
| red_segments=$(( $SEGMENTS - $green_segments - $yellow_segments )) | |
| # Display everything | |
| echo -n "%{$fg[green]%}" | |
| repeat $green_segments echo -n "▂" | |
| echo -n "%{$fg[yellow]%}" | |
| repeat $yellow_segments echo -n "▂" | |
| echo -n "%{$fg[red]%}" | |
| repeat $red_segments echo -n "▂" | |
| echo -n "%{$reset_color%}" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment