From f16d560268134222c4f9068b63df2d4b29af714c Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 18 Sep 2015 14:50:29 +0200 Subject: fb.py: Humanize sizes in history output Signed-off-by: Florian Pritz --- fb.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/fb.py b/fb.py index 40648d0..6b1fd69 100755 --- a/fb.py +++ b/fb.py @@ -38,6 +38,22 @@ def print_table(table): print("| " + " | ".join("{:{}}".format(x, col_width[i]) for i, x in enumerate(line)) + " |") +def humanize_bytes(num): + suffix = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] + boundary = 2048.0 + + for unit in suffix: + if abs(num) < boundary: + break + num /= 1024.0 + + if unit == "B": + format = "%.0f%s" + else: + format = "%.2f%s" + + return format % (num, unit) + @contextlib.contextmanager def make_temp_directory(): temp_dir = tempfile.mkdtemp() @@ -592,8 +608,8 @@ class FBClient: i['mimetype'], datetime.datetime.fromtimestamp(int(i['date'])).strftime(timeFormat), i['hash'], - i['filesize' - ]] for i in items] + humanize_bytes(int(i['filesize'])) + ] for i in items] print_table(itemsTable) def display_version(self): -- cgit v1.2.3-24-g4f1b