Refactored OSX battery code

This commit is contained in:
Joshua Perry 2014-02-08 12:51:13 -07:00 committed by ZyX
parent 9accbdd9ef
commit f62d749f80
1 changed files with 8 additions and 9 deletions

View File

@ -1076,22 +1076,21 @@ now_playing = NowPlayingSegment()
if os.path.exists('/sys/class/power_supply/BAT0/capacity'):
def _get_capacity():
def _get_capacity(pl):
with open('/sys/class/power_supply/BAT0/capacity', 'r') as f:
return int(float(f.readline().split()[0]))
elif os.path.exists('/usr/bin/pmset'):
def _get_capacity():
def _get_capacity(pl):
import re
import subprocess
battery_summary = subprocess.check_output(['pmset', '-g', 'batt']).decode('utf8')
batt_percent = re.search(r'\d+%', battery_summary).group(0)
return int(batt_percent.replace('%', ''))
battery_summary = run_cmd(pl, ['pmset', '-g', 'batt'])
battery_percent = re.search(r'(\d+)%', battery_summary).group(1)
return int(battery_percent)
else:
def _get_capacity():
def _get_capacity(pl):
raise NotImplementedError
def battery(pl, format='{batt:3.0%}', steps=5, gamify=False):
def battery(pl, format='{batt:3.0%}', steps=100, gamify=False):
'''Return battery charge status.
:param int steps:
@ -1102,7 +1101,7 @@ def battery(pl, format='{batt:3.0%}', steps=5, gamify=False):
Highlight groups used: ``battery_gradient`` (gradient), ``battery``.
'''
try:
capacity = _get_capacity()
capacity = _get_capacity(pl)
except NotImplementedError:
pl.warn('Unable to get battery capacity.')
return None