From 5e8da6afa8f22eb6a7f7153ec51e7eb1f190c719 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Mon, 4 Apr 2022 16:48:36 -0300 Subject: [PATCH] [view] improvement: displaying a '+' for positive update sizes (upgrade summary) --- CHANGELOG.md | 3 +++ bauh/commons/view_utils.py | 5 +++-- bauh/view/qt/thread.py | 13 ++++++++----- tests/common/test_view_utils.py | 9 +++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed6f93aa..1c5def80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Improvements - General - code refactoring + +- UI + - update summary: displaying a '+' for positive sizes (previously the sign was only displayed for negative numbers) (#250)[https://github.com/vinifmor/bauh/issues/250] ### Fixes - Debian diff --git a/bauh/commons/view_utils.py b/bauh/commons/view_utils.py index 3d0791f0..a094774d 100644 --- a/bauh/commons/view_utils.py +++ b/bauh/commons/view_utils.py @@ -21,7 +21,7 @@ def new_select(label: str, tip: Optional[str], id_: str, opts: Iterable[Tuple[Op capitalize_label=capitalize_label) -def get_human_size_str(size) -> Optional[str]: +def get_human_size_str(size, positive_sign: bool = False) -> Optional[str]: if type(size) in (int, float, str): int_size = abs(int(size)) @@ -34,4 +34,5 @@ def get_human_size_str(size) -> Optional[str]: if size_unit < 1024: size_unit = size_unit if size > 0 else size_unit * -1 - return f'{int(size_unit)} {unit}' if unit == 'B' else f'{size_unit:.2f} {unit}' + size_str = f'{int(size_unit)} {unit}' if unit == 'B' else f'{size_unit:.2f} {unit}' + return f'+{size_str}' if positive_sign and size_unit > 0 else size_str diff --git a/bauh/view/qt/thread.py b/bauh/view/qt/thread.py index adece32f..30d17c90 100644 --- a/bauh/view/qt/thread.py +++ b/bauh/view/qt/thread.py @@ -238,7 +238,8 @@ class UpgradeSelected(AsyncAction): self.internet_checker = internet_checker self.screen_width = screen_width - def _req_as_option(self, req: UpgradeRequirement, tooltip: bool = True, custom_tooltip: str = None, required_size: bool = True, display_sizes: bool = True) -> InputOption: + def _req_as_option(self, req: UpgradeRequirement, tooltip: bool = True, custom_tooltip: str = None, required_size: bool = True, display_sizes: bool = True, + positive_size_symbol: bool = False) -> InputOption: if req.pkg.installed: icon_path = req.pkg.get_disk_icon_path() @@ -253,7 +254,7 @@ class UpgradeSelected(AsyncAction): size_str = None if display_sizes: size_str = '{}: {}'.format(self.i18n['size'].capitalize(), - '?' if req.extra_size is None else get_human_size_str(req.extra_size)) + '?' if req.extra_size is None else get_human_size_str(req.extra_size, positive_size_symbol)) if required_size and req.extra_size != req.required_size: size_str += ' ( {}: {} )'.format(self.i18n['action.update.pkg.required_size'].capitalize(), '?' if req.required_size is None else get_human_size_str(req.required_size)) @@ -317,7 +318,7 @@ class UpgradeSelected(AsyncAction): return FormComponent(label=lb, components=comps) def _gen_to_update_form(self, reqs: List[UpgradeRequirement]) -> Tuple[FormComponent, Tuple[int, int]]: - opts = [self._req_as_option(r, tooltip=False) for r in reqs] + opts = [self._req_as_option(r, tooltip=False, positive_size_symbol=True) for r in reqs] comps = [MultipleSelectComponent(label='', options=opts, default_options=set(opts))] required_size, extra_size = self._sum_pkgs_size(reqs) @@ -325,7 +326,7 @@ class UpgradeSelected(AsyncAction): self.i18n['amount'].capitalize(), len(opts), self.i18n['size'].capitalize(), - '?' if extra_size is None else get_human_size_str(extra_size), + '?' if extra_size is None else get_human_size_str(extra_size, True), self.i18n['action.update.pkg.required_size'].capitalize(), '?' if required_size is None else get_human_size_str(required_size)) @@ -489,7 +490,9 @@ class UpgradeSelected(AsyncAction): extra_size += updates_size[1] comps.append(updates_form) - extra_size_text = '{}: {}'.format(self.i18n['action.update.total_size'].capitalize(), get_human_size_str(extra_size)) + extra_size_text = '{}: {}'.format(self.i18n['action.update.total_size'].capitalize(), + get_human_size_str(extra_size, True)) + req_size_text = '{}: {}'.format(self.i18n['action.update.required_size'].capitalize(), get_human_size_str(required_size)) comps.insert(0, TextComponent('{} | {}'.format(extra_size_text, req_size_text), size=14)) diff --git a/tests/common/test_view_utils.py b/tests/common/test_view_utils.py index 063bf2b2..6a59c17a 100644 --- a/tests/common/test_view_utils.py +++ b/tests/common/test_view_utils.py @@ -33,3 +33,12 @@ class GetHumanSizeStrTest(TestCase): def test__must_properly_convert_to_Pb(self): self.assertEqual('1.00 Pb', get_human_size_str(1125899906842624)) self.assertEqual('57.30 Pb', get_human_size_str(64514064662082350)) + + def test__must_concatenate_the_plus_sign_if_positive_sign_is_true_and_value_is_positive(self): + self.assertEqual('+1023 B', get_human_size_str(1023, positive_sign=True)) + self.assertEqual('+1.00 Kb', get_human_size_str(1024, positive_sign=True)) + + def test__must_not_concatenate_the_plus_sign_if_positive_sign_is_true_and_value_is_negative(self): + self.assertEqual('-1023 B', get_human_size_str(-1023, positive_sign=True)) + self.assertEqual('-1.00 Kb', get_human_size_str(-1024, positive_sign=True)) +