1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prezto/themes/twain/charge.py

41 lines
1.2 KiB

13 years ago
#!/usr/bin/env python
# coding=UTF-8
""" Prints a battery meter for use in a zsh theme """
import sys
13 years ago
""" Config """
output_length = 10
hide_when_charge_is_above = 90
13 years ago
filled_char, empty_char = u'', u''
""" Read battery charge """
def read_battery_percent (sp, regex):
""" Reads the battery charge as an integer between 0 and 100, using a given process and regex. """
13 years ago
import subprocess, re
process = subprocess.Popen(sp, stdout=subprocess.PIPE)
13 years ago
output = process.communicate()[0].strip()
return int(re.search(regex, output, re.MULTILINE).group(1))
13 years ago
if sys.platform.startswith('darwin'):
""" OSX - 'pmset -g batt' """
charge = read_battery_percent(["pmset", "-g", "batt"], "^.+\t([\d]+)\%")
elif sys.platform.startswith('freebsd'):
""" Freebsd - 'acpiconf -i 0' """
charge = read_battery_percent(["acpiconf", "-i", "0"], "")
else:
""" Linux - 'acpi -b' """
charge = read_battery_percent(["acpi", "-b"], "^Battery 0: .+, ([\d]+)\%")
13 years ago
""" Output """
13 years ago
if charge < hide_when_charge_is_above:
filled = int(charge / output_length)
13 years ago
empty = output_length - filled
meter = (filled * filled_char + empty * empty_char).encode('utf-8')
color = 'green'if filled > 6 else 'yellow' if filled > 4 else 'red'
sys.stdout.write("%F{{{0}}}{1}%f".format(color, meter))