Refactored OSX battery code
This commit is contained in:
parent
9accbdd9ef
commit
f62d749f80
|
@ -1076,22 +1076,21 @@ now_playing = NowPlayingSegment()
|
||||||
|
|
||||||
|
|
||||||
if os.path.exists('/sys/class/power_supply/BAT0/capacity'):
|
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:
|
with open('/sys/class/power_supply/BAT0/capacity', 'r') as f:
|
||||||
return int(float(f.readline().split()[0]))
|
return int(float(f.readline().split()[0]))
|
||||||
elif os.path.exists('/usr/bin/pmset'):
|
elif os.path.exists('/usr/bin/pmset'):
|
||||||
def _get_capacity():
|
def _get_capacity(pl):
|
||||||
import re
|
import re
|
||||||
import subprocess
|
battery_summary = run_cmd(pl, ['pmset', '-g', 'batt'])
|
||||||
battery_summary = subprocess.check_output(['pmset', '-g', 'batt']).decode('utf8')
|
battery_percent = re.search(r'(\d+)%', battery_summary).group(1)
|
||||||
batt_percent = re.search(r'\d+%', battery_summary).group(0)
|
return int(battery_percent)
|
||||||
return int(batt_percent.replace('%', ''))
|
|
||||||
else:
|
else:
|
||||||
def _get_capacity():
|
def _get_capacity(pl):
|
||||||
raise NotImplementedError
|
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.
|
'''Return battery charge status.
|
||||||
|
|
||||||
:param int steps:
|
: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``.
|
Highlight groups used: ``battery_gradient`` (gradient), ``battery``.
|
||||||
'''
|
'''
|
||||||
try:
|
try:
|
||||||
capacity = _get_capacity()
|
capacity = _get_capacity(pl)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
pl.warn('Unable to get battery capacity.')
|
pl.warn('Unable to get battery capacity.')
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue