Skip to content

Commit 69880e1

Browse files
committed
All Widgets inherit from WidthMixinWidget
1 parent 86a28c9 commit 69880e1

1 file changed

Lines changed: 35 additions & 16 deletions

File tree

progressbar/widgets.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class WidthWidgetMixin(object):
8686
'''Mixing to make sure widgets are only visible if the screen is within a
8787
specified size range so the progressbar fits on both large and small
8888
screens..
89+
90+
The widgets are only visible if the screen is within a
91+
specified size range so the progressbar fits on both large and small
92+
screens.
93+
94+
Variables available:
95+
- min_width: Only display the widget if at least `min_width` is left
96+
- max_width: Only display the widget if at most `max_width` is left
8997
'''
9098

9199
def __init__(self, min_width=None, max_width=None, **kwargs):
@@ -154,7 +162,7 @@ class TimeSensitiveWidgetBase(WidgetBase):
154162
INTERVAL = datetime.timedelta(milliseconds=100)
155163

156164

157-
class FormatLabel(FormatWidgetMixin, WidthWidgetMixin):
165+
class FormatLabel(FormatWidgetMixin, WidthWidgetMixin, WidgetBase):
158166
'''Displays a formatted label
159167
160168
>>> label = FormatLabel('%(value)s', min_width=5, max_width=10)
@@ -196,6 +204,7 @@ class FormatLabel(FormatWidgetMixin, WidthWidgetMixin):
196204
def __init__(self, format, **kwargs):
197205
FormatWidgetMixin.__init__(self, format=format, **kwargs)
198206
WidthWidgetMixin.__init__(self, **kwargs)
207+
WidgetBase.__init__(self, **kwargs)
199208

200209
def __call__(self, progress, data, **kwargs):
201210
if not self.check_size(progress):
@@ -416,7 +425,7 @@ def __call__(self, progress, data):
416425
return ETA.__call__(self, progress, data, value=value, elapsed=elapsed)
417426

418427

419-
class DataSize(FormatWidgetMixin):
428+
class DataSize(FormatWidgetMixin, WidthWidgetMixin):
420429
'''
421430
Widget for showing an amount of data transferred/processed.
422431
@@ -433,6 +442,7 @@ def __init__(
433442
self.unit = unit
434443
self.prefixes = prefixes
435444
FormatWidgetMixin.__init__(self, format=format, **kwargs)
445+
WidthWidgetMixin.__init__(self, **kwargs)
436446

437447
def __call__(self, progress, data):
438448
value = data[self.variable]
@@ -448,7 +458,7 @@ def __call__(self, progress, data):
448458
return FormatWidgetMixin.__call__(self, progress, data)
449459

450460

451-
class FileTransferSpeed(FormatWidgetMixin, TimeSensitiveWidgetBase):
461+
class FileTransferSpeed(FormatWidgetMixin, WidthWidgetMixin, TimeSensitiveWidgetBase):
452462
'''
453463
WidgetBase for showing the transfer speed (useful for file transfers).
454464
'''
@@ -462,6 +472,7 @@ def __init__(
462472
self.prefixes = prefixes
463473
self.inverse_format = inverse_format
464474
FormatWidgetMixin.__init__(self, format=format, **kwargs)
475+
WidthWidgetMixin.__init__(self, **kwargs)
465476
TimeSensitiveWidgetBase.__init__(self, **kwargs)
466477

467478
def _speed(self, value, elapsed):
@@ -507,7 +518,7 @@ def __call__(self, progress, data):
507518
return FileTransferSpeed.__call__(self, progress, data, value, elapsed)
508519

509520

510-
class AnimatedMarker(TimeSensitiveWidgetBase):
521+
class AnimatedMarker(WidthWidgetMixin, TimeSensitiveWidgetBase):
511522
'''An animated marker for the progress bar which defaults to appear as if
512523
it were rotating.
513524
'''
@@ -516,6 +527,7 @@ def __init__(self, markers='|/-\\', default=None, fill='', **kwargs):
516527
self.markers = markers
517528
self.default = default or markers[0]
518529
self.fill = create_marker(fill) if fill else None
530+
WidthWidgetMixin.__init__(self, **kwargs)
519531
WidgetBase.__init__(self, **kwargs)
520532

521533
def __call__(self, progress, data, width=None):
@@ -548,19 +560,21 @@ def __call__(self, progress, data, width=None):
548560
RotatingMarker = AnimatedMarker
549561

550562

551-
class Counter(FormatWidgetMixin, WidgetBase):
563+
class Counter(FormatWidgetMixin, WidthWidgetMixin, WidgetBase):
552564
'''Displays the current count'''
553565

554566
def __init__(self, format='%(value)d', **kwargs):
555567
FormatWidgetMixin.__init__(self, format=format, **kwargs)
568+
WidthWidgetMixin.__init__(self, **kwargs)
556569
WidgetBase.__init__(self, format=format, **kwargs)
557570

558571

559-
class Percentage(FormatWidgetMixin, WidgetBase):
572+
class Percentage(FormatWidgetMixin, WidthWidgetMixin, WidgetBase):
560573
'''Displays the current percentage as a number with a percent sign.'''
561574

562575
def __init__(self, format='%(percentage)3d%%', **kwargs):
563576
FormatWidgetMixin.__init__(self, format=format, **kwargs)
577+
WidthWidgetMixin.__init__(self, **kwargs)
564578
WidgetBase.__init__(self, format=format, **kwargs)
565579

566580
def __call__(self, progress, data, format=None):
@@ -572,15 +586,16 @@ def __call__(self, progress, data, format=None):
572586
return FormatWidgetMixin.__call__(self, progress, data)
573587

574588

575-
class SimpleProgress(FormatWidgetMixin, WidgetBase):
589+
class SimpleProgress(FormatWidgetMixin, WidthWidgetMixin, WidgetBase):
576590
'''Returns progress as a count of the total (e.g.: "5 of 47")'''
577591

578592
DEFAULT_FORMAT = '%(value_s)s of %(max_value_s)s'
579593

580-
def __init__(self, format=DEFAULT_FORMAT, max_width=None, **kwargs):
581-
self.max_width = dict(default=max_width)
594+
def __init__(self, format=DEFAULT_FORMAT, **kwargs):
582595
FormatWidgetMixin.__init__(self, format=format, **kwargs)
596+
WidthWidgetMixin.__init__(self, **kwargs)
583597
WidgetBase.__init__(self, format=format, **kwargs)
598+
self.max_width_cache = dict(default=self.max_width)
584599

585600
def __call__(self, progress, data, format=None):
586601
# If max_value is not available, display N/A
@@ -600,7 +615,7 @@ def __call__(self, progress, data, format=None):
600615

601616
# Guess the maximum width from the min and max value
602617
key = progress.min_value, progress.max_value
603-
max_width = self.max_width.get(key, self.max_width['default'])
618+
max_width = self.max_width_cache.get(key, self.max_width)
604619
if not max_width:
605620
temporary_data = data.copy()
606621
for value in key:
@@ -613,7 +628,7 @@ def __call__(self, progress, data, format=None):
613628
if width: # pragma: no branch
614629
max_width = max(max_width or 0, width)
615630

616-
self.max_width[key] = max_width
631+
self.max_width_cache[key] = max_width
617632

618633
# Adjust the output to have a consistent size in all cases
619634
if max_width: # pragma: no branch
@@ -622,7 +637,7 @@ def __call__(self, progress, data, format=None):
622637
return formatted
623638

624639

625-
class Bar(AutoWidthWidgetBase):
640+
class Bar(WidthWidgetMixin, AutoWidthWidgetBase):
626641
'''A progress bar which stretches to fill the line.'''
627642

628643
def __init__(self, marker='#', left='|', right='|', fill=' ',
@@ -644,6 +659,7 @@ def __init__(self, marker='#', left='|', right='|', fill=' ',
644659
self.fill = string_or_lambda(fill)
645660
self.fill_left = fill_left
646661

662+
WidthWidgetMixin.__init__(self, **kwargs)
647663
AutoWidthWidgetBase.__init__(self, **kwargs)
648664

649665
def __call__(self, progress, data, width):
@@ -711,14 +727,15 @@ def __call__(self, progress, data, width):
711727
return left + marker + right
712728

713729

714-
class FormatCustomText(FormatWidgetMixin, WidthWidgetMixin):
730+
class FormatCustomText(FormatWidgetMixin, WidthWidgetMixin, WidgetBase):
715731
mapping = {}
716732

717733
def __init__(self, format, mapping=mapping, **kwargs):
718734
self.format = format
719735
self.mapping = mapping
720736
FormatWidgetMixin.__init__(self, format=format, **kwargs)
721737
WidthWidgetMixin.__init__(self, **kwargs)
738+
WidgetBase.__init__(self, **kwargs)
722739

723740
def update_mapping(self, **mapping):
724741
self.mapping.update(mapping)
@@ -728,11 +745,11 @@ def __call__(self, progress, data):
728745
self, progress, self.mapping, self.format)
729746

730747

731-
class DynamicMessage(FormatWidgetMixin, WidgetBase):
748+
class DynamicMessage(FormatWidgetMixin, WidthWidgetMixin, WidgetBase):
732749
'''Displays a custom variable.'''
733750

734751
def __init__(self, name, format='{name}: {formatted_value}',
735-
width=6, precision=3):
752+
width=6, precision=3, **kwargs):
736753
'''Creates a DynamicMessage associated with the given name.'''
737754
self.format = format
738755
self.width = width
@@ -744,6 +761,7 @@ def __init__(self, name, format='{name}: {formatted_value}',
744761
'DynamicMessage(): argument must be single word')
745762

746763
self.name = name
764+
WidthWidgetMixin.__init__(self, **kwargs)
747765

748766
def __call__(self, progress, data):
749767
value = data['dynamic_messages'][self.name]
@@ -766,14 +784,15 @@ def __call__(self, progress, data):
766784
return self.format.format(**context)
767785

768786

769-
class CurrentTime(FormatWidgetMixin, TimeSensitiveWidgetBase):
787+
class CurrentTime(FormatWidgetMixin, WidthWidgetMixin, TimeSensitiveWidgetBase):
770788
'''Widget which displays the current (date)time with seconds resolution.'''
771789
INTERVAL = datetime.timedelta(seconds=1)
772790

773791
def __init__(self, format='Current Time: %(current_time)s',
774792
microseconds=False, **kwargs):
775793
self.microseconds = microseconds
776794
FormatWidgetMixin.__init__(self, format=format, **kwargs)
795+
WidthWidgetMixin.__init__(self, **kwargs)
777796
TimeSensitiveWidgetBase.__init__(self, **kwargs)
778797

779798
def __call__(self, progress, data):

0 commit comments

Comments
 (0)