Created
March 19, 2011 06:45
-
-
Save rpetrich/877288 to your computer and use it in GitHub Desktop.
Battery/charging indicator for OS X
This file contains 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
# Change prompt to include battery indicator | |
# Use some funky escapes so bash determines the proper prompt length | |
PS1='\w \[$(~/bin/battery.sh)\] \[\033[1D\] ' |
This file contains 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
Colors: | |
White = fully charged | |
Green = 60% charged or better | |
Yellow = 40% charged or better | |
Red = less than 40% charged | |
Symbol: | |
$ = connected to power | |
! = not connected |
This file contains 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
#!/usr/bin/env python | |
# coding=UTF-8 | |
import math, subprocess | |
p = subprocess.Popen(["ioreg", "-rc", "AppleSmartBattery"], stdout=subprocess.PIPE) | |
output = p.communicate()[0] | |
o_max = [l for l in output.splitlines() if 'MaxCapacity' in l][0] | |
o_cur = [l for l in output.splitlines() if 'CurrentCapacity' in l][0] | |
o_crg = [l for l in output.splitlines() if 'ExternalConnected' in l][0] | |
o_ful = [l for l in output.splitlines() if 'FullyCharged' in l][0] | |
b_max = float(o_max.rpartition('=')[-1].strip()) | |
b_cur = float(o_cur.rpartition('=')[-1].strip()) | |
b_crg = o_crg.rpartition('=')[-1].strip() | |
b_ful = o_ful.rpartition('=')[-1].strip() | |
charge = b_cur / b_max | |
# Output | |
import sys | |
color_green = '[32m' | |
color_yellow = '[1;33m' | |
color_red = '[31m' | |
color_reset = '[00m' | |
color_out = ( | |
"" if b_ful == "Yes" | |
else color_green if charge > 0.6 | |
else color_yellow if charge > 0.4 | |
else color_red | |
) | |
out = color_out | |
if b_crg == "Yes": | |
out += '$' | |
else: | |
out += '!' | |
out += color_reset | |
sys.stdout.write(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment