Add 'SizeColumn' formatter

Present file sizes in human readable format.

Change-Id: I7e4a013cba7f91e38ba496d3ba7c4a67c4cd81c5
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2022-10-20 18:09:03 +01:00
parent 4bb2e57358
commit 6152b686b7
3 changed files with 21 additions and 0 deletions

View File

@ -58,3 +58,10 @@ class ListDictColumn(columns.FormattableColumn):
def machine_readable(self):
return [dict(x) for x in self._value or []]
class SizeColumn(columns.FormattableColumn):
"""Format column for file size content"""
def human_readable(self):
return utils.format_size(self._value)

View File

@ -109,3 +109,12 @@ class TestListDictColumn(utils.TestCase):
self.assertEqual(type(col.machine_readable()), list)
for x in col.machine_readable():
self.assertEqual(type(x), dict)
class TestSizeColumn(utils.TestCase):
def test_size_column(self):
content = 1576395005
col = format_columns.SizeColumn(content)
self.assertEqual(content, col.machine_readable())
self.assertEqual('1.6G', col.human_readable())

View File

@ -0,0 +1,5 @@
---
features:
- |
A new column formatter, ``SizeFormatter``, is available. This can be used
to format file sizes in human readable format.