comparison 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
comparison
equal deleted inserted replaced
3523:30779935c0aa 3524:584379473925
28 from sat.tools.common import data_format 28 from sat.tools.common import data_format
29 from sat_frontends.jp.constants import Const as C 29 from sat_frontends.jp.constants import Const as C
30 from sat_frontends.jp import common 30 from sat_frontends.jp import common
31 from sat_frontends.tools import jid 31 from sat_frontends.tools import jid
32 from sat.tools.common.ansi import ANSI as A 32 from sat.tools.common.ansi import ANSI as A
33 from sat.tools.common import utils
33 from urllib.parse import urlparse 34 from urllib.parse import urlparse
34 from pathlib import Path 35 from pathlib import Path
35 import tempfile 36 import tempfile
36 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI 37 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
37 import json 38 import json
862 return name 863 return name
863 864
864 def _size_filter(self, size, row): 865 def _size_filter(self, size, row):
865 if not size: 866 if not size:
866 return "" 867 return ""
867 size = int(size) 868 return A.color(A.BOLD, utils.getHumanSize(size))
868 #  cf. https://stackoverflow.com/a/1094933 (thanks)
869 suffix = "o"
870 for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
871 if abs(size) < 1024.0:
872 return A.color(A.BOLD, "{:.2f}".format(size), unit, suffix)
873 size /= 1024.0
874
875 return A.color(A.BOLD, "{:.2f}".format(size), "Yi", suffix)
876 869
877 def default_output(self, files_data): 870 def default_output(self, files_data):
878 """display files a way similar to ls""" 871 """display files a way similar to ls"""
879 files_data.sort(key=lambda d: d["name"].lower()) 872 files_data.sort(key=lambda d: d["name"].lower())
880 show_header = False 873 show_header = False