173
150
# the progress output. It may be there is a model-level use for
174
151
# saying "this task's not active at the moment" but I don't see it. --
176
if self.progress_view:
177
self.progress_view.clear()
153
self.ui_factory.clear_term()
156
@deprecated_function(deprecated_in((1, 16, 0)))
157
def ProgressBar(to_file=None, **kwargs):
158
"""Abstract factory"""
161
requested_bar_type = os.environ.get('BZR_PROGRESS_BAR')
162
# An value of '' or not set reverts to standard processing
163
if requested_bar_type in (None, ''):
164
if _supports_progress(to_file):
165
return TTYProgressBar(to_file=to_file, **kwargs)
179
self.ui_factory.clear_term()
182
# NOTE: This is also deprecated; you should provide a ProgressView instead.
167
return DummyProgress(to_file=to_file, **kwargs)
169
# Minor sanitation to prevent spurious errors
170
requested_bar_type = requested_bar_type.lower().strip()
171
# TODO: jam 20060710 Arguably we shouldn't raise an exception
172
# but should instead just disable progress bars if we
173
# don't recognize the type
174
if requested_bar_type not in _progress_bar_types:
175
raise errors.InvalidProgressBarType(requested_bar_type,
176
_progress_bar_types.keys())
177
return _progress_bar_types[requested_bar_type](to_file=to_file, **kwargs)
183
180
class _BaseProgressBar(object):
185
182
def __init__(self,
255
253
return DummyProgress(**kwargs)
256
class DotsProgressBar(_BaseProgressBar):
258
@deprecated_function(deprecated_in((1, 16, 0)))
259
def __init__(self, **kwargs):
260
_BaseProgressBar.__init__(self, **kwargs)
267
def update(self, msg=None, current_cnt=None, total_cnt=None):
268
if msg and msg != self.last_msg:
270
self.to_file.write('\n')
271
self.to_file.write(msg + ': ')
274
self.to_file.write('.')
278
self.to_file.write('\n')
281
def child_update(self, message, current, total):
285
class TTYProgressBar(_BaseProgressBar):
286
"""Progress bar display object.
288
Several options are available to control the display. These can
289
be passed as parameters to the constructor or assigned at any time:
292
Show percentage complete.
294
Show rotating baton. This ticks over on every update even
295
if the values don't change.
297
Show predicted time-to-completion.
301
Show numerical counts.
303
The output file should be in line-buffered or unbuffered mode.
307
@deprecated_function(deprecated_in((1, 16, 0)))
308
def __init__(self, **kwargs):
309
from bzrlib.osutils import terminal_width
310
_BaseProgressBar.__init__(self, **kwargs)
312
self.width = terminal_width()
313
self.last_updates = []
314
self._max_last_updates = 10
315
self.child_fraction = 0
316
self._have_output = False
318
def throttle(self, old_msg):
319
"""Return True if the bar was updated too recently"""
320
# time.time consistently takes 40/4000 ms = 0.01 ms.
321
# time.clock() is faster, but gives us CPU time, not wall-clock time
323
if self.start_time is not None and (now - self.start_time) < 1:
325
if old_msg != self.last_msg:
327
interval = now - self.last_update
329
if interval < self.MIN_PAUSE:
332
self.last_updates.append(now - self.last_update)
333
# Don't let the queue grow without bound
334
self.last_updates = self.last_updates[-self._max_last_updates:]
335
self.last_update = now
339
self.update(self.last_msg, self.last_cnt, self.last_total,
342
def child_update(self, message, current, total):
343
if current is not None and total != 0:
344
child_fraction = float(current) / total
345
if self.last_cnt is None:
347
elif self.last_cnt + child_fraction <= self.last_total:
348
self.child_fraction = child_fraction
349
if self.last_msg is None:
353
def update(self, msg, current_cnt=None, total_cnt=None,
355
"""Update and redraw progress bar.
360
if total_cnt is None:
361
total_cnt = self.last_total
366
if current_cnt > total_cnt:
367
total_cnt = current_cnt
369
## # optional corner case optimisation
370
## # currently does not seem to fire so costs more than saved.
371
## # trivial optimal case:
372
## # NB if callers are doing a clear and restore with
373
## # the saved values, this will prevent that:
374
## # in that case add a restore method that calls
375
## # _do_update or some such
376
## if (self.last_msg == msg and
377
## self.last_cnt == current_cnt and
378
## self.last_total == total_cnt and
379
## self.child_fraction == child_fraction):
385
old_msg = self.last_msg
386
# save these for the tick() function
388
self.last_cnt = current_cnt
389
self.last_total = total_cnt
390
self.child_fraction = child_fraction
392
# each function call takes 20ms/4000 = 0.005 ms,
393
# but multiple that by 4000 calls -> starts to cost.
394
# so anything to make this function call faster
395
# will improve base 'diff' time by up to 0.1 seconds.
396
if self.throttle(old_msg):
399
if self.show_eta and self.start_time and self.last_total:
400
eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
401
self.last_total, last_updates = self.last_updates)
402
eta_str = " " + str_tdelta(eta)
406
if self.show_spinner:
407
spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '
411
# always update this; it's also used for the bar
414
if self.show_pct and self.last_total and self.last_cnt:
415
pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
416
pct_str = ' (%5.1f%%)' % pct
420
if not self.show_count:
422
elif self.last_cnt is None:
424
elif self.last_total is None:
425
count_str = ' %i' % (self.last_cnt)
427
# make both fields the same size
428
t = '%i' % (self.last_total)
429
c = '%*i' % (len(t), self.last_cnt)
430
count_str = ' ' + c + '/' + t
433
# progress bar, if present, soaks up all remaining space
434
cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
435
- len(eta_str) - len(count_str) - 3
438
# number of markers highlighted in bar
439
markers = int(round(float(cols) *
440
(self.last_cnt + self.child_fraction) / self.last_total))
441
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
443
# don't know total, so can't show completion.
444
# so just show an expanded spinning thingy
445
m = self.spin_pos % cols
446
ms = (' ' * m + '*').ljust(cols)
448
bar_str = '[' + ms + '] '
454
m = spin_str + bar_str + self.last_msg + count_str \
456
self.to_file.write('\r%-*.*s' % (self.width - 1, self.width - 1, m))
457
self._have_output = True
458
#self.to_file.flush()
461
if self._have_output:
462
self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
463
self._have_output = False
464
#self.to_file.flush()
467
class ChildProgress(_BaseProgressBar):
468
"""A progress indicator that pushes its data to the parent"""
470
@deprecated_function(deprecated_in((1, 16, 0)))
471
def __init__(self, _stack, **kwargs):
472
_BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
473
self.parent = _stack.top()
476
self.child_fraction = 0
479
def update(self, msg, current_cnt=None, total_cnt=None):
480
self.current = current_cnt
481
if total_cnt is not None:
482
self.total = total_cnt
484
self.child_fraction = 0
487
def child_update(self, message, current, total):
488
if current is None or total == 0:
489
self.child_fraction = 0
491
self.child_fraction = float(current) / total
495
if self.current is None:
498
count = self.current+self.child_fraction
499
if count > self.total:
501
mutter('clamping count of %d to %d' % (count, self.total))
503
self.parent.child_update(self.message, count, self.total)
508
def note(self, *args, **kwargs):
509
self.parent.note(*args, **kwargs)
258
512
def str_tdelta(delt):