summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-09-18 14:50:29 +0200
committerFlorian Pritz <bluewind@xinu.at>2015-09-18 14:50:29 +0200
commitf16d560268134222c4f9068b63df2d4b29af714c (patch)
treed0508c651eb0555a23de6562a6c3e4be2f44a008
parente533fb2a3b34c4283a6228ecb2f4900369bed250 (diff)
fb.py: Humanize sizes in history output
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rwxr-xr-xfb.py20
1 files 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):