Skip to content

Instantly share code, notes, and snippets.

@yfrancis
Forked from rpetrich/.bash_profile
Created March 19, 2011 06:49
Show Gist options
  • Save yfrancis/877289 to your computer and use it in GitHub Desktop.
Save yfrancis/877289 to your computer and use it in GitHub Desktop.
PS1="\w \$(~/bin/battery.sh) "
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
#!/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 = ''
color_yellow = ''
color_red = ''
color_reset = ''
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