Skip to content

Commit 005d8a1

Browse files
committed
Merge branch 'release/3.55.0'
2 parents 308096a + e5d37e3 commit 005d8a1

File tree

7 files changed

+108
-2
lines changed

7 files changed

+108
-2
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ of widgets:
6060
- `FormatCustomText <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#FormatCustomText>`_
6161
- `FormatLabel <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#FormatLabel>`_
6262
- `FormatLabelBar <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#FormatLabel>`_
63+
- `GranularBar <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#GranularBar>`_
6364
- `Percentage <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#Percentage>`_
6465
- `PercentageLabelBar <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#PercentageLabelBar>`_
6566
- `ReverseBar <http://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/widgets.html#ReverseBar>`_

examples.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ def multi_progress_bar_example(left=True):
177177
time.sleep(0.02)
178178

179179

180+
@example
181+
def granular_progress_example():
182+
widgets = [
183+
progressbar.GranularBar(markers=" ▏▎▍▌▋▊▉█", left='', right='|'),
184+
progressbar.GranularBar(markers=" ▁▂▃▄▅▆▇█", left='', right='|'),
185+
progressbar.GranularBar(markers=" ▖▌▛█", left='', right='|'),
186+
progressbar.GranularBar(markers=" ░▒▓█", left='', right='|'),
187+
progressbar.GranularBar(markers=" ⡀⡄⡆⡇⣇⣧⣷⣿", left='', right='|'),
188+
progressbar.GranularBar(markers=" .oO", left='', right=''),
189+
]
190+
for i in progressbar.progressbar(range(100), widgets=widgets):
191+
time.sleep(0.03)
192+
193+
180194
@example
181195
def percentage_label_bar_example():
182196
widgets = [progressbar.PercentageLabelBar()]

progressbar/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
long running operations.
2020
'''.strip().split())
2121
__email__ = '[email protected]'
22-
__version__ = '3.54.0'
22+
__version__ = '3.55.0'
2323
__license__ = 'BSD'
2424
__copyright__ = 'Copyright 2015 Rick van Hattem (Wolph)'
2525
__url__ = 'https://github.com/WoLpH/python-progressbar'

progressbar/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
VariableMixin,
2727
MultiRangeBar,
2828
MultiProgressBar,
29+
GranularBar,
2930
FormatLabelBar,
3031
PercentageLabelBar,
3132
Variable,
@@ -74,6 +75,7 @@
7475
'VariableMixin',
7576
'MultiRangeBar',
7677
'MultiProgressBar',
78+
'GranularBar',
7779
'FormatLabelBar',
7880
'PercentageLabelBar',
7981
'Variable',

progressbar/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def __init__(self, target, capturing=False, listeners=set()):
192192
self.listeners = listeners
193193
self.needs_clear = False
194194

195-
def isatty(self):
195+
def isatty(self): # pragma: no cover
196196
return self.target.isatty()
197197

198198
def write(self, value):

progressbar/widgets.py

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

911911

912+
class GranularMarkers:
913+
smooth = ' ▏▎▍▌▋▊▉█'
914+
bar = ' ▁▂▃▄▅▆▇█'
915+
snake = ' ▖▌▛█'
916+
fade_in = ' ░▒▓█'
917+
dots = ' ⡀⡄⡆⡇⣇⣧⣷⣿'
918+
growing_circles = ' .oO'
919+
920+
921+
class GranularBar(AutoWidthWidgetBase):
922+
'''A progressbar that can display progress at a sub-character granularity
923+
by using multiple marker characters.
924+
925+
Examples of markers:
926+
- Smooth: ` ▏▎▍▌▋▊▉█` (default)
927+
- Bar: ` ▁▂▃▄▅▆▇█`
928+
- Snake: ` ▖▌▛█`
929+
- Fade in: ` ░▒▓█`
930+
- Dots: ` ⡀⡄⡆⡇⣇⣧⣷⣿`
931+
- Growing circles: ` .oO`
932+
933+
The markers can be accessed through GranularMarkers. GranularMarkers.dots
934+
for example
935+
'''
936+
937+
def __init__(self, markers=GranularMarkers.smooth, left='|', right='|',
938+
**kwargs):
939+
'''Creates a customizable progress bar.
940+
941+
markers - string of characters to use as granular progress markers. The
942+
first character should represent 0% and the last 100%.
943+
Ex: ` .oO`.
944+
left - string or callable object to use as a left border
945+
right - string or callable object to use as a right border
946+
'''
947+
self.markers = markers
948+
self.left = string_or_lambda(left)
949+
self.right = string_or_lambda(right)
950+
951+
AutoWidthWidgetBase.__init__(self, **kwargs)
952+
953+
def __call__(self, progress, data, width):
954+
left = converters.to_unicode(self.left(progress, data, width))
955+
right = converters.to_unicode(self.right(progress, data, width))
956+
width -= progress.custom_len(left) + progress.custom_len(right)
957+
958+
if progress.max_value is not base.UnknownLength \
959+
and progress.max_value > 0:
960+
percent = progress.value / progress.max_value
961+
else:
962+
percent = 0
963+
964+
num_chars = percent * width
965+
966+
marker = self.markers[-1] * int(num_chars)
967+
968+
marker_idx = int((num_chars % 1) * (len(self.markers) - 1))
969+
if marker_idx:
970+
marker += self.markers[marker_idx]
971+
972+
marker = converters.to_unicode(marker)
973+
974+
# Make sure we ignore invisible characters when filling
975+
width += len(marker) - progress.custom_len(marker)
976+
marker = marker.ljust(width, self.markers[0])
977+
978+
return left + marker + right
979+
980+
912981
class FormatLabelBar(FormatLabel, Bar):
913982
'''A bar which has a formatted label in the center.'''
914983
def __init__(self, format, **kwargs):

tests/test_monitor_progress.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ def test_percentage_label_bar(testdir):
206206
]
207207

208208

209+
def test_granular_bar(testdir):
210+
result = testdir.runpython(testdir.makepyfile(_create_script(
211+
widgets='[progressbar.GranularBar(markers=" .oO")]',
212+
line_breaks=False,
213+
items=list(range(5)),
214+
)))
215+
pprint.pprint(result.stderr.lines, width=70)
216+
assert result.stderr.lines == [
217+
u'',
218+
u'| |',
219+
u'|OOOOOOOOOOO. |',
220+
u'|OOOOOOOOOOOOOOOOOOOOOOO |',
221+
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo |',
222+
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO. |',
223+
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO|',
224+
u'',
225+
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO|'
226+
]
227+
228+
209229
def test_colors(testdir):
210230
kwargs = dict(
211231
items=range(1),

0 commit comments

Comments
 (0)