[util/format] Tests and minor renaming

This commit is contained in:
Tobias Witek 2020-03-07 13:33:28 +01:00
parent 0f96f2727b
commit 8efa101380
4 changed files with 18 additions and 4 deletions

View file

@ -7,3 +7,4 @@
- engine.input.register_callback is now core.input.register - engine.input.register_callback is now core.input.register
- update() only has a single parameter (self) (no widgets anymore) - update() only has a single parameter (self) (no widgets anymore)
- bumblebee.util.format stuff moved into util.format (byte, aslist, asbool, etc.) - bumblebee.util.format stuff moved into util.format (byte, aslist, asbool, etc.)
- util.format.duration -> shorten -> compact, suffix -> unit

View file

@ -61,7 +61,7 @@ class Module(core.module.Module):
return '' return ''
except Exception: except Exception:
return '' 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): def capacity(self, widget):
widget.set('capacity', -1) widget.set('capacity', -1)

View file

@ -63,4 +63,17 @@ class format(unittest.TestCase):
self.assertEqual('4.50GiB', byte(1024*1024*1024*4 + 1024*1024*512)) self.assertEqual('4.50GiB', byte(1024*1024*1024*4 + 1024*1024*512))
self.assertEqual('2048.00GiB', byte(1024*1024*1024*1024*2)) 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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -30,19 +30,19 @@ def byte(val, fmt='{:.2f}'):
val /= 1024.0 val /= 1024.0
return '{}GiB'.format(fmt).format(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) duration = int(duration)
minutes, seconds = divmod(duration, 60) minutes, seconds = divmod(duration, 60)
hours, minutes = divmod(minutes, 60) hours, minutes = divmod(minutes, 60)
suf = 'm' suf = 'm'
res = '{:02d}:{:02d}'.format(minutes, seconds) res = '{:02d}:{:02d}'.format(minutes, seconds)
if hours > 0: if hours > 0:
if shorten: if compact:
res = '{:02d}:{:02d}'.format(hours, minutes) res = '{:02d}:{:02d}'.format(hours, minutes)
else: else:
res = '{:02d}:{}'.format(hours, res) res = '{:02d}:{}'.format(hours, res)
suf = 'h' suf = 'h'
return '{}{}'.format(res, suf if suffix else '') return '{}{}'.format(res, suf if unit else '')
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4