diff --git a/csppfetch/__init__.py b/csppfetch/__init__.py index ca815c89ce169d2eb792a6eb73085f10d2f8766b..94e6b87356c844cb70321af9ea50b7d891883826 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.", ]