diff --git a/doc/MODULE_MIGRATION.md b/doc/MODULE_MIGRATION.md index b04ea1a..8e8e9bf 100644 --- a/doc/MODULE_MIGRATION.md +++ b/doc/MODULE_MIGRATION.md @@ -7,3 +7,4 @@ - engine.input.register_callback is now core.input.register - update() only has a single parameter (self) (no widgets anymore) - bumblebee.util.format stuff moved into util.format (byte, aslist, asbool, etc.) +- util.format.duration -> shorten -> compact, suffix -> unit diff --git a/modules/core/battery.py b/modules/core/battery.py index ffe39fe..68fcf5c 100644 --- a/modules/core/battery.py +++ b/modules/core/battery.py @@ -61,7 +61,7 @@ class Module(core.module.Module): return '' except Exception: return '' - return util.format.duration(estimate*60, shorten=True, suffix=True) # estimate is in minutes + return util.format.duration(estimate*60, compact=True, unit=True) # estimate is in minutes def capacity(self, widget): widget.set('capacity', -1) diff --git a/tests/util/test_format.py b/tests/util/test_format.py index 95773b2..885bcb3 100644 --- a/tests/util/test_format.py +++ b/tests/util/test_format.py @@ -63,4 +63,17 @@ class format(unittest.TestCase): self.assertEqual('4.50GiB', byte(1024*1024*1024*4 + 1024*1024*512)) self.assertEqual('2048.00GiB', byte(1024*1024*1024*1024*2)) + def test_duration(self): + self.assertEqual('04:20:00', duration(4*60*60+20*60)) + self.assertEqual('04:20:00h', duration(4*60*60+20*60, unit=True)) + self.assertEqual('04:20h', duration(4*60*60+20*60, compact=True, unit=True)) + + self.assertEqual('20:00', duration(20*60)) + self.assertEqual('20:00m', duration(20*60, unit=True)) + self.assertEqual('20:00m', duration(20*60, compact=True, unit=True)) + + self.assertEqual('00:20', duration(20)) + self.assertEqual('00:20m', duration(20, unit=True)) + self.assertEqual('00:20m', duration(20, compact=True, unit=True)) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/util/format.py b/util/format.py index 141ef32..5ed29f6 100644 --- a/util/format.py +++ b/util/format.py @@ -30,19 +30,19 @@ def byte(val, fmt='{:.2f}'): val /= 1024.0 return '{}GiB'.format(fmt).format(val*1024.0) -def duration(duration, shorten=False, suffix=False): +def duration(duration, compact=False, unit=False): duration = int(duration) minutes, seconds = divmod(duration, 60) hours, minutes = divmod(minutes, 60) suf = 'm' res = '{:02d}:{:02d}'.format(minutes, seconds) if hours > 0: - if shorten: + if compact: res = '{:02d}:{:02d}'.format(hours, minutes) else: res = '{:02d}:{}'.format(hours, res) suf = 'h' - return '{}{}'.format(res, suf if suffix else '') + return '{}{}'.format(res, suf if unit else '') # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4