@@ -146,6 +146,11 @@ class PartitionTable(Enum):
146146 MBR = 'msdos'
147147
148148
149+ class Units (Enum ):
150+ BINARY = 'binary'
151+ DECIMAL = 'decimal'
152+
153+
149154class Unit (Enum ):
150155 B = 1 # byte
151156 kB = 1000 ** 1 # kilobyte
@@ -176,6 +181,10 @@ def get_all_units() -> list[str]:
176181 def get_si_units () -> list [Unit ]:
177182 return [u for u in Unit if 'i' not in u .name and u .name != 'sectors' ]
178183
184+ @staticmethod
185+ def get_binary_units () -> list [Unit ]:
186+ return [u for u in Unit if 'i' in u .name or u .name == 'B' ]
187+
179188
180189@dataclass
181190class SectorSize :
@@ -278,8 +287,32 @@ def format_size(
278287 return f'{ target_size .value } { target_unit .name } '
279288 return f'{ target_size .value } '
280289
281- def format_highest (self , include_unit : bool = True ) -> str :
290+ def binary_unit_highest (self , include_unit : bool = True ) -> str :
291+ binary_units = Unit .get_binary_units ()
292+
293+ size = float (self ._normalize ())
294+ unit = Unit .KiB
295+ base_value = unit .value
296+
297+ for binary_unit in binary_units :
298+ unit = binary_unit
299+ if size < base_value :
300+ break
301+ size /= base_value
302+
303+ formatted_size = f"{ size :.1f} "
304+
305+ if formatted_size .endswith ('.0' ):
306+ formatted_size = formatted_size [:- 2 ]
307+
308+ if not include_unit :
309+ return formatted_size
310+
311+ return f'{ formatted_size } { unit .name } '
312+
313+ def si_unit_highest (self , include_unit : bool = True ) -> str :
282314 si_units = Unit .get_si_units ()
315+
283316 all_si_values = [self .convert (si ) for si in si_units ]
284317 filtered = filter (lambda x : x .value >= 1 , all_si_values )
285318
@@ -291,6 +324,12 @@ def format_highest(self, include_unit: bool = True) -> str:
291324 return f'{ si_value .value } { si_value .unit .name } '
292325 return f'{ si_value .value } '
293326
327+ def format_highest (self , include_unit : bool = True , units : Units = Units .BINARY ) -> str :
328+ if units == Units .BINARY :
329+ return self .binary_unit_highest (include_unit )
330+ else :
331+ return self .si_unit_highest (include_unit )
332+
294333 def _normalize (self ) -> int :
295334 """
296335 will normalize the value of the unit to Byte
0 commit comments