Skip to content

Commit f0231e6

Browse files
committed
Add indeterminate progress customization
Signed-off-by: Stephen L. <[email protected]>
1 parent c35629e commit f0231e6

1 file changed

Lines changed: 57 additions & 2 deletions

File tree

tqdm/_tqdm_custom.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
__all__ = ['tqdm_custom', 'tcrange']
2323

2424

25+
def mirror_chars(s):
26+
"""Mirror characters using translation"""
27+
import string
28+
ins = '()<>[]\/{}bd'
29+
outs = ')(><][/\}{db'
30+
trans = string.maketrans(ins,outs)
31+
return s.translate(trans)
32+
33+
2534
class tqdm_custom(tqdm):
2635
"""
2736
tqdm with nice customizable bar symbols!
@@ -229,8 +238,54 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
229238

230239
# no total: no progressbar, ETA, just progress stats
231240
else:
232-
return (prefix if prefix else '') + '{0}{1} [{2}, {3}]'.format(
233-
n_fmt, unit, elapsed_str, rate_fmt)
241+
if bar_format and isinstance(bar_format, dict):
242+
# left and right sides
243+
l_bar = '{0}{1}|'.format(n_fmt, unit)
244+
r_bar = '| {0}/? [{1}, {2}]'.format(n_fmt, elapsed_str, rate_fmt)
245+
246+
# space available for bar's display
247+
N_BARS = max(1, ncols - len(l_bar) - len(r_bar)) if ncols \
248+
else 10
249+
250+
# get ascii or unicode template
251+
if ascii:
252+
c_symb = bar_format['symbols_indeterminate'].get('ascii', ["====="])
253+
else:
254+
c_symb = bar_format['symbols_indeterminate'].get('unicode', ["====="])
255+
# looping symbols: just update the symbol animation at each iteration
256+
if bar_format['symbols_indeterminate'].get('loop', False):
257+
# increment one step in the animation at each step
258+
bar = c_symb[divmod(n, len(c_symb))[1]]
259+
260+
bar_length = N_BARS # avoid the filling
261+
# indeterminate progress bar (cycle from left to right then right to left)
262+
else:
263+
# Get current bar animation based on current iteration
264+
bar = c_symb[divmod(n, len(c_symb))[1]]
265+
# Get left filling space and animation step (right pass or left?)
266+
anim_step, fill_left = divmod(n, (N_BARS - len(bar)))
267+
# If anim_step is odd, then we do left pass (2nd pass)
268+
if divmod(anim_step, 2)[1] == 1:
269+
# Inverse the left filling space (now it's the right space)
270+
fill_left = N_BARS - len(bar) - fill_left
271+
# Reverse the bar string
272+
bar = mirror_chars(bar[::-1])
273+
274+
# Generate bar with left filling space
275+
bar = ' ' * fill_left + bar
276+
bar_length = len(bar)
277+
278+
# Right space padding
279+
full_bar = bar + \
280+
' ' * max(N_BARS - bar_length, 0)
281+
282+
# Piece together the bar parts
283+
return l_bar + full_bar + r_bar
284+
285+
# Standard fast no ETA progress status
286+
else:
287+
return (prefix if prefix else '') + '{0}{1} [{2}, {3}]'.format(
288+
n_fmt, unit, elapsed_str, rate_fmt)
234289

235290
def __init__(self, *args, **kwargs):
236291
"""

0 commit comments

Comments
 (0)