diff --git a/README b/README index 34907fc..e581309 100644 --- a/README +++ b/README @@ -78,6 +78,7 @@ Parameters - ``zfill`` bar zero fill symbol (default: -) - ``decimals`` positive number of decimals in percent complete (default: 1) +- ``formatter`` a function that formats the count and goal, useful for handling large numbers like file sizes Any Questions? Report a Bug? Enhancements? ------------------------------------------ diff --git a/README.md b/README.md index e72a565..58a4a74 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ manager.progress_2.stop('stop progress 2') * `fill` bar fill symbol (default: █) * `zfill` bar zero fill symbol (default: -) * `decimals` positive number of decimals in percent complete (default: 1) +* `formatter` a function that formats the count and goal, useful for handling large numbers like file sizes ## Any Questions? Report a Bug? Enhancements? diff --git a/README.rst b/README.rst index 41a131c..544a146 100644 --- a/README.rst +++ b/README.rst @@ -132,6 +132,7 @@ Parameters - ``zfill`` bar zero fill symbol (default: -) - ``decimals`` positive number of decimals in percent complete (default: 1) +- ``formatter`` a function that formats the count and goal, useful for handling large numbers like file sizes Any Questions? Report a Bug? Enhancements? ------------------------------------------ diff --git a/cli_progressbar/progressbar.py b/cli_progressbar/progressbar.py index c3f0699..3188502 100644 --- a/cli_progressbar/progressbar.py +++ b/cli_progressbar/progressbar.py @@ -10,19 +10,17 @@ class Progress: def __init__(self, goal: int = -1): self.goal = goal self.status = '' - self.showing_status = '' - self.longest_status = 0 self.bar_len = 60 self.fill = '█' self.zfill = '-' self.decimals = 1 + self.formatter = lambda x: x + self.last_len = 0 def set_status(self, status: str): if status == self.status: return self.status = status - self.longest_status = max(len(status), self.longest_status) - self.showing_status = status + ' ' * (self.longest_status - len(status)) def update(self, count: int, status: str = ''): self.set_status(status) @@ -32,9 +30,13 @@ def update(self, count: int, status: str = ''): percents = round(100.0 * count / float(self.goal), self.decimals) bar = self.fill * filled_len + self.zfill * (self.bar_len - filled_len) - text = '[%s] %s%s | %s/%s' % (bar, percents, '%', count, self.goal) - if self.showing_status: - text += ' | %s' % self.showing_status + text = '[%s] %s%s | %s/%s' % (bar, percents, '%', self.formatter(count), self.formatter(self.goal)) + if self.status: + text += ' | %s' % self.status + + padding = ' ' * (self.last_len - len(text)) + self.last_len = len(text) + text += padding pos = getattr(self, 'pos', 0) self.move_to(pos)