From 3232973611ea86c8c578c31a23046bfe2d213d47 Mon Sep 17 00:00:00 2001
From: Alan De Smet <alan.desmet@ssec.wisc.edu>
Date: Fri, 6 Aug 2021 12:18:17 -0500
Subject: [PATCH] Nicely format file size totals

---
 csppfetch/__init__.py | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/csppfetch/__init__.py b/csppfetch/__init__.py
index ca815c8..94e6b87 100644
--- a/csppfetch/__init__.py
+++ b/csppfetch/__init__.py
@@ -95,6 +95,28 @@ class FileSet:
                 )
 
 
+def human_bytes(total_bytes):
+    step_size = 1024
+    steps = "Ki Mi Gi Ti Pi Ei".split()
+    # If you prefer the non-binary SI prefixes
+    # step_size = 1000
+    #     steps = [""]+list("kMGTPE")
+    if total_bytes < step_size:
+        return f"{total_bytes} B"
+    value = total_bytes
+    step = -1
+    while value >= step_size:
+        step += 1
+        value /= step_size
+    value_str = f"{value:f}"[:4]
+    if value_str[-1] == ".":
+        value_str = value_str[:-1]
+
+    return f"{value_str} {steps[step]}B"
+
+
+
+
 
 class DownloadStatistics: 
     FIELDS = [
@@ -118,16 +140,17 @@ class DownloadStatistics:
     def total_time(self):
         return self.end - self.start
 
+
     def report(self):
         self.finish()
         speed = self.downloaded_size/(self.total_time().total_seconds())
         ret = [
             f"Downloaded {self.downloaded_files} files "
-                f"totalling {self.downloaded_size:,} bytes.",
+                f"totalling {human_bytes(self.downloaded_size)}.",
             f"Pulled {self.cache_hits} files from cache, "
-                f"saving {self.cache_size:,} bytes of download.",
+                f"saving {human_bytes(self.cache_size)} of download.",
             f"Deleted {self.deleted_files} files, "
-                f"freeing {self.deleted_size:,} bytes of space.",
+                f"freeing {human_bytes(self.deleted_size)} of space.",
             f"Took {self.total_time()}.",
             f"Average download speed of {speed:,.0f} bytes per second.",
         ]
-- 
GitLab