Skip to content

Commit fb8a8dd

Browse files
committed
Made all widgets min-width/max-width configurable. THanks to @ritze
1 parent ca42c43 commit fb8a8dd

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

progressbar/bar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def _format_widgets(self):
486486

487487
for index, widget in enumerate(self.widgets):
488488
if isinstance(widget, widgets.WidgetBase) \
489-
and not widget.is_fitting(self):
489+
and not widget.check_size(self):
490490
continue
491491
elif isinstance(widget, widgets.AutoWidthWidgetBase):
492492
result.append(widget)

progressbar/widgets.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,45 @@ def __call__(self, progress, data, format=None):
8282
raise
8383

8484

85-
class WidgetBase(object):
85+
class WidthWidgetMixin(object):
86+
'''Mixing to make sure widgets are only visible if the screen is within a
87+
specified size range so the progressbar fits on both large and small
88+
screens..
89+
90+
Variables available:
91+
- min_width: Only display the widget if at least `min_width` is left
92+
- max_width: Only display the widget if at most `max_width` is left
93+
94+
>>> class Progress(object):
95+
... term_width = 0
96+
97+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
98+
False
99+
>>> Progress.term_width = 5
100+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
101+
True
102+
>>> Progress.term_width = 10
103+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
104+
True
105+
>>> Progress.term_width = 11
106+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
107+
False
108+
'''
109+
110+
def __init__(self, min_width=None, max_width=None, **kwargs):
111+
self.min_width = min_width
112+
self.max_width = max_width
113+
114+
def check_size(self, progress):
115+
if self.min_width and self.min_width > progress.term_width:
116+
return False
117+
elif self.max_width and self.max_width < progress.term_width:
118+
return False
119+
else:
120+
return True
121+
122+
123+
class WidgetBase(WidthWidgetMixin):
86124
__metaclass__ = abc.ABCMeta
87125
'''The base class for all widgets
88126
@@ -108,25 +146,13 @@ class WidgetBase(object):
108146
with a lower one
109147
'''
110148

111-
def __init__(self, min_width=None, max_width=None, **kwargs):
112-
self.min_width = min_width
113-
self.max_width = max_width
114-
115149
@abc.abstractmethod
116150
def __call__(self, progress, data):
117151
'''Updates the widget.
118152
119153
progress - a reference to the calling ProgressBar
120154
'''
121155

122-
def is_fitting(self, progress):
123-
if self.min_width and self.min_width > progress.term_width:
124-
return False
125-
elif self.max_width and self.max_width < progress.term_width:
126-
return False
127-
else:
128-
return True
129-
130156

131157
class AutoWidthWidgetBase(WidgetBase):
132158
'''The base class for all variable width widgets.
@@ -160,7 +186,6 @@ class FormatLabel(FormatWidgetMixin, WidgetBase):
160186
>>> label = FormatLabel('%(value)s', min_width=5, max_width=10)
161187
>>> class Progress(object):
162188
... pass
163-
164189
>>> label = FormatLabel('{value} :: {value:^6}', new_style=True)
165190
>>> str(label(Progress, dict(value='test')))
166191
'test :: test '

0 commit comments

Comments
 (0)