From a80bbdf17d2ba8fdd629db05eb8b188990d1b8da Mon Sep 17 00:00:00 2001 From: StopMotionCuber Date: Mon, 5 Oct 2020 21:32:36 +0200 Subject: [PATCH] Updated uptime segment to have show time in a better way (#2036) * Updated uptime segment to have show time in a better way * Made current solution backwards compatible * Include proposed change for proper uptime support * Updated tests to reflect new default values for uptime --- powerline/segments/common/sys.py | 15 +++++++++------ tests/test_python/test_segments.py | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/powerline/segments/common/sys.py b/powerline/segments/common/sys.py index f8c85ce0..29a2459a 100644 --- a/powerline/segments/common/sys.py +++ b/powerline/segments/common/sys.py @@ -148,7 +148,8 @@ else: @add_divider_highlight_group('background:divider') -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): +def uptime(pl, days_format='{days:d}d', hours_format=' {hours:d}h', minutes_format=' {minutes:02d}m', + seconds_format=' {seconds:02d}s', shorten_len=3): '''Return system uptime. :param str days_format: @@ -173,9 +174,11 @@ def uptime(pl, days_format='{days:d}d', hours_format=' {hours:d}h', minutes_form hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) 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] + days_format.format(days=days) if days_format else None, + hours_format.format(hours=hours) if hours_format else None, + minutes_format.format(minutes=minutes) if minutes_format else None, + seconds_format.format(seconds=seconds) if seconds_format else None, + ])) + first_non_zero = next((i for i, x in enumerate([days, hours, minutes, seconds]) if x != 0)) + time_formatted = time_formatted[first_non_zero:first_non_zero + shorten_len] return ''.join(time_formatted).strip() diff --git a/tests/test_python/test_segments.py b/tests/test_python/test_segments.py index 7ede1d94..7564bff0 100644 --- a/tests/test_python/test_segments.py +++ b/tests/test_python/test_segments.py @@ -885,10 +885,10 @@ class TestSys(TestCommon): def test_uptime(self): pl = Pl() with replace_attr(self.module, '_get_uptime', lambda: 259200): - self.assertEqual(self.module.uptime(pl=pl), [{'contents': '3d', 'divider_highlight_group': 'background:divider'}]) + self.assertEqual(self.module.uptime(pl=pl), [{'contents': '3d 0h 00m', 'divider_highlight_group': 'background:divider'}]) with replace_attr(self.module, '_get_uptime', lambda: 93784): - self.assertEqual(self.module.uptime(pl=pl), [{'contents': '1d 2h 3m', 'divider_highlight_group': 'background:divider'}]) - self.assertEqual(self.module.uptime(pl=pl, shorten_len=4), [{'contents': '1d 2h 3m 4s', 'divider_highlight_group': 'background:divider'}]) + self.assertEqual(self.module.uptime(pl=pl), [{'contents': '1d 2h 03m', 'divider_highlight_group': 'background:divider'}]) + self.assertEqual(self.module.uptime(pl=pl, shorten_len=4), [{'contents': '1d 2h 03m 04s', 'divider_highlight_group': 'background:divider'}]) with replace_attr(self.module, '_get_uptime', lambda: 65536): self.assertEqual(self.module.uptime(pl=pl), [{'contents': '18h 12m 16s', 'divider_highlight_group': 'background:divider'}]) self.assertEqual(self.module.uptime(pl=pl, shorten_len=2), [{'contents': '18h 12m', 'divider_highlight_group': 'background:divider'}])