Merge branch 'feature/shortened-uptime' into develop

This commit is contained in:
Kim Silkebækken 2013-04-05 16:22:38 +02:00
commit 5ff80ee206
2 changed files with 26 additions and 6 deletions

View File

@ -637,12 +637,19 @@ else:
@add_divider_highlight_group('background:divider') @add_divider_highlight_group('background:divider')
def uptime(pl, format='{days}d {hours:02d}h {minutes:02d}m'): def uptime(pl, days_format='{days:d}d', hours_format=' {hours:d}h', minutes_format=' {minutes:d}m', seconds_format=' {seconds:d}s', shorten_len=3):
'''Return system uptime. '''Return system uptime.
:param str format: :param str days_format:
format string, will be passed ``days``, ``hours``, ``minutes`` and day format string, will be passed ``days`` as the argument
seconds as arguments :param str hours_format:
hour format string, will be passed ``hours`` as the argument
:param str minutes_format:
minute format string, will be passed ``minutes`` as the argument
:param str seconds_format:
second format string, will be passed ``seconds`` as the argument
:param int shorten_len:
shorten the amount of units (days, hours, etc.) displayed
Divider highlight group used: ``background:divider``. Divider highlight group used: ``background:divider``.
''' '''
@ -654,7 +661,13 @@ def uptime(pl, format='{days}d {hours:02d}h {minutes:02d}m'):
minutes, seconds = divmod(seconds, 60) minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60) hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24) days, hours = divmod(hours, 24)
return format.format(days=int(days), hours=hours, minutes=minutes, seconds=seconds) time_formatted = list(filter(None, [
days_format.format(days=days) if days and days_format else None,
hours_format.format(hours=hours) if hours and hours_format else None,
minutes_format.format(minutes=minutes) if minutes and minutes_format else None,
seconds_format.format(seconds=seconds) if seconds and seconds_format else None,
]))[0:shorten_len]
return ''.join(time_formatted).strip()
class NetworkLoadSegment(KwThreadedSegment): class NetworkLoadSegment(KwThreadedSegment):

View File

@ -176,8 +176,15 @@ class TestCommon(TestCase):
def test_uptime(self): def test_uptime(self):
pl = Pl() pl = Pl()
with replace_attr(common, '_get_uptime', lambda: 259200):
self.assertEqual(common.uptime(pl=pl), [{'contents': '3d', 'divider_highlight_group': 'background:divider'}])
with replace_attr(common, '_get_uptime', lambda: 93784):
self.assertEqual(common.uptime(pl=pl), [{'contents': '1d 2h 3m', 'divider_highlight_group': 'background:divider'}])
self.assertEqual(common.uptime(pl=pl, shorten_len=4), [{'contents': '1d 2h 3m 4s', 'divider_highlight_group': 'background:divider'}])
with replace_attr(common, '_get_uptime', lambda: 65536): with replace_attr(common, '_get_uptime', lambda: 65536):
self.assertEqual(common.uptime(pl=pl), [{'contents': '0d 18h 12m', 'divider_highlight_group': 'background:divider'}]) self.assertEqual(common.uptime(pl=pl), [{'contents': '18h 12m 16s', 'divider_highlight_group': 'background:divider'}])
self.assertEqual(common.uptime(pl=pl, shorten_len=2), [{'contents': '18h 12m', 'divider_highlight_group': 'background:divider'}])
self.assertEqual(common.uptime(pl=pl, shorten_len=1), [{'contents': '18h', 'divider_highlight_group': 'background:divider'}])
def _get_uptime(): def _get_uptime():
raise NotImplementedError raise NotImplementedError