Created
          June 3, 2016 19:54 
        
      - 
      
- 
        Save mindware/8c4de0e3b6b4cc38ad2b64a1be880091 to your computer and use it in GitHub Desktop. 
    Battery monitoring for Mac - Python
  
        
  
    
      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
    
  
  
    
  | #!/usr/bin/env python | |
| # coding=UTF-8 | |
| # -*- coding: UTF-8 -*- | |
| # source: | |
| # http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/#my-right-prompt-battery-capacity | |
| 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] | |
| b_max = float(o_max.rpartition('=')[-1].strip()) | |
| b_cur = float(o_cur.rpartition('=')[-1].strip()) | |
| charge = b_cur / b_max | |
| charge_threshold = int(math.ceil(10 * charge)) | |
| # Output | |
| total_slots, slots = 5, [] | |
| filled = int(math.ceil(charge_threshold * (total_slots / 10.0))) * u'□' | |
| empty = (total_slots - len(filled)) * u'■' | |
| out = (filled + empty).encode('utf-8') | |
| import sys | |
| color_green = '\e[0;32m' | |
| color_yellow = '\e[033m' | |
| color_red = '\e[0;31m' | |
| color_reset = '\e[00m' | |
| color_out = ( | |
| color_green if len(filled) > 6 | |
| else color_yellow if len(filled) > 4 | |
| else color_red | |
| ) | |
| out = color_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
  
            
Here you go @bmuscolino