diff --git a/examples.py b/examples.py index 47275ccc..e428821a 100644 --- a/examples.py +++ b/examples.py @@ -444,6 +444,7 @@ def dynamic_message(): progressbar.Percentage(), progressbar.Bar(), progressbar.DynamicMessage('loss'), + progressbar.DynamicMessage('username', width=12, precision=12),, ] with progressbar.ProgressBar(max_value=100, widgets=widgets) as bar: min_so_far = 1 @@ -451,7 +452,7 @@ def dynamic_message(): val = random.random() if val < min_so_far: min_so_far = val - bar.update(i, loss=min_so_far) + bar.update(i, loss=min_so_far, username='Some user %02d' % i) @example diff --git a/progressbar/__about__.py b/progressbar/__about__.py index ce2b5b30..28d2dd1b 100644 --- a/progressbar/__about__.py +++ b/progressbar/__about__.py @@ -19,7 +19,7 @@ long running operations. '''.strip().split()) __email__ = 'wolph@wol.ph' -__version__ = '3.40.0' +__version__ = '3.41.0' __license__ = 'BSD' __copyright__ = 'Copyright 2015 Rick van Hattem (Wolph)' __url__ = 'https://github.com/WoLpH/python-progressbar' diff --git a/progressbar/widgets.py b/progressbar/widgets.py index 0bdd46ae..4fffda90 100644 --- a/progressbar/widgets.py +++ b/progressbar/widgets.py @@ -715,8 +715,12 @@ def __call__(self, progress, data): class DynamicMessage(FormatWidgetMixin, WidgetBase): '''Displays a custom variable.''' - def __init__(self, name): + def __init__(self, name, format='{name}: {formatted_value}', + width=6, precision=3): '''Creates a DynamicMessage associated with the given name.''' + self.format = format + self.width = width + self.precision = precision if not isinstance(name, str): raise TypeError('DynamicMessage(): argument must be a string') if len(name.split()) > 1: @@ -726,11 +730,24 @@ def __init__(self, name): self.name = name def __call__(self, progress, data): - val = data['dynamic_messages'][self.name] - if val: - return self.name + ': ' + '{:6.3g}'.format(val) - else: - return self.name + ': ' + 6 * '-' + value = data['dynamic_messages'][self.name] + context = data.copy() + context['value'] = value + context['name'] = self.name + context['width'] = self.width + context['precision'] = self.precision + + try: + context['formatted_value'] = '{value:{width}.{precision}}'.format( + **context) + except (TypeError, ValueError): + if value: + context['formatted_value'] = '{value:{width}}'.format( + **context) + else: + context['formatted_value'] = '-' * self.width + + return self.format.format(**context) class CurrentTime(FormatWidgetMixin, TimeSensitiveWidgetBase): diff --git a/tests/test_custom_widgets.py b/tests/test_custom_widgets.py index a7456de2..ec3f4381 100644 --- a/tests/test_custom_widgets.py +++ b/tests/test_custom_widgets.py @@ -40,11 +40,20 @@ def test_dynamic_message_widget(): progressbar.Bar(), ' (', progressbar.ETA(), ') ', progressbar.DynamicMessage('loss'), + progressbar.DynamicMessage('text'), + progressbar.DynamicMessage('error', precision=None), ] p = progressbar.ProgressBar(widgets=widgets, max_value=1000) p.start() for i in range(0, 200, 5): time.sleep(0.1) - p.update(i + 1, loss=.5) + p.update(i + 1, loss=.5, text='spam', error=1) + + i += 1 + p.update(i, text=None) + i += 1 + p.update(i, text=False) + i += 1 + p.update(i, text=True, error='a') p.finish()