Improved battery segment:

- Ignore `steps` if gamify is False.
- Add `full_heart` and `empty_heart` keywords.
- Document `format` keyword.
- Replace `draw_soft_divider` with `draw_inner_divider`.
This commit is contained in:
ZyX 2014-02-09 13:44:06 +04:00
parent cce6d26670
commit 66d98e7def
2 changed files with 43 additions and 23 deletions

View File

@ -1,6 +1,6 @@
# vim:fileencoding=utf-8:noet
from __future__ import unicode_literals, absolute_import
from __future__ import unicode_literals, absolute_import, division
import os
import sys
@ -1090,13 +1090,21 @@ else:
raise NotImplementedError
def battery(pl, format='{batt:3.0%}', steps=5, gamify=False):
def battery(pl, format='{capacity:3.0%}', steps=5, gamify=False, full_heart='', empty_heart=''):
'''Return battery charge status.
:param str format:
Percent format in case gamify is False.
:param int steps:
number of discrete steps to show between 0% and 100% capacity
Number of discrete steps to show between 0% and 100% capacity if gamify
is True.
:param bool gamify:
measure in hearts () instead of percentages
Measure in hearts () instead of percentages.
:param str full_heart:
Heart displayed for full part of battery.
:param str empty_heart:
Heart displayed for used part of battery. It is also displayed using
another gradient level, so it is OK for it to be the same as full_heart.
Highlight groups used: ``battery_gradient`` (gradient), ``battery``.
'''
@ -1106,27 +1114,25 @@ def battery(pl, format='{batt:3.0%}', steps=5, gamify=False):
pl.warn('Unable to get battery capacity.')
return None
ret = []
denom = int(steps)
numer = int(denom * capacity / 100)
full_heart = ''
if gamify:
denom = int(steps)
numer = int(denom * capacity / 100)
ret.append({
'contents': full_heart * numer,
'draw_soft_divider': False,
'draw_inner_divider': False,
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 99
'gradient_level': 99,
})
ret.append({
'contents': full_heart * (denom - numer),
'draw_soft_divider': False,
'contents': empty_heart * (denom - numer),
'draw_inner_divider': False,
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 1
'gradient_level': 1,
})
else:
batt = numer / float(denom)
ret.append({
'contents': format.format(batt=batt),
'contents': format.format(capacity=(capacity / 100.0)),
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': batt * 100
'gradient_level': capacity,
})
return ret

View File

@ -391,30 +391,44 @@ class TestCommon(TestCase):
with replace_attr(common, '_get_capacity', _get_capacity):
self.assertEqual(common.battery(pl=pl), [{
'contents': '80%',
'contents': '86%',
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 80.0
'gradient_level': 86
}])
self.assertEqual(common.battery(pl=pl, format='{batt:.2f}'), [{
'contents': '0.80',
self.assertEqual(common.battery(pl=pl, format='{capacity:.2f}'), [{
'contents': '0.86',
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 80.0
'gradient_level': 86
}])
self.assertEqual(common.battery(pl=pl, steps=7), [{
'contents': '86%',
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 85.71428571428571
'gradient_level': 86
}])
self.assertEqual(common.battery(pl=pl, gamify=True), [
{
'contents': '♥♥♥♥',
'draw_soft_divider': False,
'draw_inner_divider': False,
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 99
},
{
'contents': '',
'draw_soft_divider': False,
'draw_inner_divider': False,
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 1
}
])
self.assertEqual(common.battery(pl=pl, gamify=True, full_heart='+', empty_heart='-', steps='10'), [
{
'contents': '++++++++',
'draw_inner_divider': False,
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 99
},
{
'contents': '--',
'draw_inner_divider': False,
'highlight_group': ['battery_gradient', 'battery'],
'gradient_level': 1
}