|
22 | 22 | __all__ = ['tqdm_custom', 'tcrange'] |
23 | 23 |
|
24 | 24 |
|
| 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 | + |
25 | 34 | class tqdm_custom(tqdm): |
26 | 35 | """ |
27 | 36 | tqdm with nice customizable bar symbols! |
@@ -229,8 +238,54 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', |
229 | 238 |
|
230 | 239 | # no total: no progressbar, ETA, just progress stats |
231 | 240 | 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) |
234 | 289 |
|
235 | 290 | def __init__(self, *args, **kwargs): |
236 | 291 | """ |
|
0 commit comments