Skip to content

Commit 0dca2aa

Browse files
committed
Add a bar that shows a label in the center.
Include a specialization with a percentage in the center.
1 parent 0eae5ef commit 0dca2aa

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

progressbar/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
VariableMixin,
2727
MultiRangeBar,
2828
MultiProgressBar,
29+
FormatLabelBar,
30+
PercentageLabelBar,
2931
Variable,
3032
DynamicMessage,
3133
FormatCustomText,
@@ -72,6 +74,8 @@
7274
'VariableMixin',
7375
'MultiRangeBar',
7476
'MultiProgressBar',
77+
'FormatLabelBar',
78+
'PercentageLabelBar',
7579
'Variable',
7680
'DynamicMessage',
7781
'FormatCustomText',

progressbar/widgets.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,37 @@ def get_values(self, progress, data):
909909
return ranges
910910

911911

912+
class FormatLabelBar(FormatLabel, Bar):
913+
'''A bar which has a formatted label in the center.'''
914+
def __init__(self, format, **kwargs):
915+
FormatLabel.__init__(self, format, **kwargs)
916+
Bar.__init__(self, **kwargs)
917+
918+
def __call__(self, progress, data, width, format=None):
919+
center = FormatLabel.__call__(self, progress, data, format=format)
920+
bar = Bar.__call__(self, progress, data, width)
921+
922+
# Aligns the center of the label to the center of the bar
923+
center_len = progress.custom_len(center)
924+
center_left = int((width - center_len) / 2)
925+
center_right = center_left + center_len
926+
return bar[:center_left] + center + bar[center_right:]
927+
928+
929+
class PercentageLabelBar(Percentage, FormatLabelBar):
930+
'''A bar which displays the current percentage in the center.'''
931+
# %3d adds an extra space that makes it look off-center
932+
# %2d keeps the label somewhat consistently in-place
933+
def __init__(self, format='%(percentage)2d%%', na='N/A%%', **kwargs):
934+
Percentage.__init__(self, format, na=na, **kwargs)
935+
FormatLabelBar.__init__(self, format, **kwargs)
936+
937+
def __call__(self, progress, data, width, format=None):
938+
return FormatLabelBar.__call__(
939+
self, progress, data, width,
940+
format=Percentage.get_format(self, progress, data, format=None))
941+
942+
912943
class Variable(FormatWidgetMixin, VariableMixin, WidgetBase):
913944
'''Displays a custom variable.'''
914945

tests/test_monitor_progress.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ def test_no_line_breaks(testdir):
186186
]
187187

188188

189+
def test_percentage_label_bar(testdir):
190+
result = testdir.runpython(testdir.makepyfile(_create_script(
191+
widgets='[progressbar.PercentageLabelBar()]',
192+
line_breaks=False,
193+
items=list(range(5)),
194+
)))
195+
pprint.pprint(result.stderr.lines, width=70)
196+
assert result.stderr.lines == [
197+
u'',
198+
u'| 0% |',
199+
u'|########### 20% |',
200+
u'|####################### 40% |',
201+
u'|###########################60%#### |',
202+
u'|###########################80%################ |',
203+
u'|###########################100%###########################|',
204+
u'',
205+
u'|###########################100%###########################|'
206+
]
207+
208+
189209
def test_colors(testdir):
190210
kwargs = dict(
191211
items=range(1),

0 commit comments

Comments
 (0)