diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 558d807f..7be327a6 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -637,12 +637,19 @@ else: @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. - :param str format: - format string, will be passed ``days``, ``hours``, ``minutes`` and - seconds as arguments + :param str days_format: + day format string, will be passed ``days`` as the argument + :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``. ''' @@ -654,7 +661,13 @@ def uptime(pl, format='{days}d {hours:02d}h {minutes:02d}m'): minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) 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): diff --git a/tests/test_segments.py b/tests/test_segments.py index d8c7bb3d..b95bad62 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -176,8 +176,15 @@ class TestCommon(TestCase): def test_uptime(self): 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): - 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(): raise NotImplementedError