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: utf8 -*- | |
import subprocess | |
from operator import itemgetter, methodcaller | |
def get_current_state(battery): | |
state = '' | |
pct = 100.0 | |
output = subprocess.check_output( | |
'upower -i {} | grep -E "state|percentage"'.format(battery), shell=True | |
).strip().split('\n') | |
for line in map(methodcaller('strip'), output): | |
name, _, data = line.rpartition(' ') | |
if 'state' in name: | |
state = data | |
elif 'percentage' in name: | |
pct = float(data.rstrip('%')) | |
return state, pct | |
def as_hearts(percent, factor=10): | |
# FIXME: Show 1 full heart for every 10% it has | |
heart = u'♥'.encode('utf8') | |
num_full = int(round(percent / factor)) | |
num_empty = (100 / factor) - num_full | |
full_hearts = heart * num_full | |
empty_hearts = heart * num_empty | |
return '#[fg=red,bg=black]{}#[fg=white,bg=black]{}'.format(full_hearts, empty_hearts) | |
def get_battery(factor=10): | |
status = [] | |
output = subprocess.check_output('upower -e | grep BAT', shell=True).strip().split('\n') | |
batteries = map(methodcaller('strip'), output) | |
for battery in batteries: | |
_, _, name = battery.rpartition('_') | |
name = name.replace('BAT', 'B') | |
state, pct = get_current_state(battery) | |
if pct > 25: | |
color = '#[fg=white,bg=black]' | |
elif pct > 15: | |
color = '#[fg=yellow,bg=black]' | |
else: | |
color = '#[fg=red,bg=black]' | |
if state == 'charging': | |
color = '#[fg=green,bg=black]+' | |
status.append((name, as_hearts(pct, factor=factor), color)) | |
return ' '.join([ | |
'{}{}:{}#[fg=white,bg=black]'.format(c, b, h) for b, h, c in sorted(status, key=itemgetter(0)) | |
]) | |
def main(): | |
factor = 20 | |
print get_battery(factor=factor) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment