From 831448684cf276b0b266d624f35509382bb2cf34 Mon Sep 17 00:00:00 2001 From: Dark Dragon Date: Tue, 26 May 2020 18:58:31 +0200 Subject: [PATCH] Improve sizeof_fmt() - Use built-in sign handling - Format integer correctly when using biggest unit - Use consistent format inside and outside of loop --- src/borg/helpers/parseformat.py | 15 ++++++++------- src/borg/testsuite/helpers.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/borg/helpers/parseformat.py b/src/borg/helpers/parseformat.py index 0e58ca0a5..01d130536 100644 --- a/src/borg/helpers/parseformat.py +++ b/src/borg/helpers/parseformat.py @@ -271,16 +271,17 @@ def parse_file_size(s): def sizeof_fmt(num, suffix='B', units=None, power=None, sep='', precision=2, sign=False): - prefix = '+' if sign and num > 0 else '' - + sign = '+' if sign else '' + fmt = '{0:{1}.{2}f}{3}{4}{5}' + prec = 0 for unit in units[:-1]: if abs(round(num, precision)) < power: - if isinstance(num, int): - return "{}{}{}{}{}".format(prefix, num, sep, unit, suffix) - else: - return "{}{:3.{}f}{}{}{}".format(prefix, num, precision, sep, unit, suffix) + break num /= float(power) - return "{}{:.{}f}{}{}{}".format(prefix, num, precision, sep, units[-1], suffix) + prec = precision + else: + unit = units[-1] + return fmt.format(num, sign, prec, sep, unit, suffix) def sizeof_fmt_iec(num, suffix='B', sep='', precision=2, sign=False): diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index f8b6fa40b..ebad92cef 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -579,7 +579,7 @@ def test_file_size_precision(): def test_file_size_sign(): si_size_map = { - 0: '0 B', + 0: '+0 B', 1: '+1 B', 1234: '+1.23 kB', -1: '-1 B',