diff sat_frontends/jp/cmd_file.py @ 3524:584379473925

tools (common/utils): new methods to parse and generate file size with symbols: - `parseSize` lets parse a size which can be either a int, or a string with a symbol like `Mio` - `getSizeMultiplier` returns a tuple of size with it's multiplier symbol - `getHumanSize` returns a string with size and adapted multiplier.
author Goffi <goffi@goffi.org>
date Wed, 05 May 2021 15:37:33 +0200
parents be6d91572633
children 04283582966f
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_file.py	Wed May 05 15:37:21 2021 +0200
+++ b/sat_frontends/jp/cmd_file.py	Wed May 05 15:37:33 2021 +0200
@@ -30,6 +30,7 @@
 from sat_frontends.jp import common
 from sat_frontends.tools import jid
 from sat.tools.common.ansi import ANSI as A
+from sat.tools.common import utils
 from urllib.parse import urlparse
 from pathlib import Path
 import tempfile
@@ -864,15 +865,7 @@
     def _size_filter(self, size, row):
         if not size:
             return ""
-        size = int(size)
-        #  cf. https://stackoverflow.com/a/1094933 (thanks)
-        suffix = "o"
-        for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
-            if abs(size) < 1024.0:
-                return A.color(A.BOLD, "{:.2f}".format(size), unit, suffix)
-            size /= 1024.0
-
-        return A.color(A.BOLD, "{:.2f}".format(size), "Yi", suffix)
+        return A.color(A.BOLD, utils.getHumanSize(size))
 
     def default_output(self, files_data):
         """display files a way similar to ls"""