166
184
self.ui_factory.clear_term()
187
@deprecated_function(deprecated_in((1, 16, 0)))
188
def ProgressBar(to_file=None, **kwargs):
189
"""Construct a progress bar.
191
Deprecated; ask the ui_factory for a progress task instead.
195
requested_bar_type = os.environ.get('BZR_PROGRESS_BAR')
196
# An value of '' or not set reverts to standard processing
197
if requested_bar_type in (None, ''):
198
if _supports_progress(to_file):
199
return TTYProgressBar(to_file=to_file, **kwargs)
201
return DummyProgress(to_file=to_file, **kwargs)
203
# Minor sanitation to prevent spurious errors
204
requested_bar_type = requested_bar_type.lower().strip()
205
# TODO: jam 20060710 Arguably we shouldn't raise an exception
206
# but should instead just disable progress bars if we
207
# don't recognize the type
208
if requested_bar_type not in _progress_bar_types:
209
raise errors.InvalidProgressBarType(requested_bar_type,
210
_progress_bar_types.keys())
211
return _progress_bar_types[requested_bar_type](to_file=to_file, **kwargs)
169
214
# NOTE: This is also deprecated; you should provide a ProgressView instead.
170
215
class _BaseProgressBar(object):
284
def note(self, fmt_string, *args, **kwargs):
285
"""See _BaseProgressBar.note()."""
232
287
def child_progress(self, **kwargs):
233
288
return DummyProgress(**kwargs)
291
class DotsProgressBar(_BaseProgressBar):
293
@deprecated_function(deprecated_in((1, 16, 0)))
294
def __init__(self, **kwargs):
295
_BaseProgressBar.__init__(self, **kwargs)
302
def update(self, msg=None, current_cnt=None, total_cnt=None):
303
if msg and msg != self.last_msg:
305
self.to_file.write('\n')
306
self.to_file.write(msg + ': ')
309
self.to_file.write('.')
313
self.to_file.write('\n')
316
def child_update(self, message, current, total):
320
class TTYProgressBar(_BaseProgressBar):
321
"""Progress bar display object.
323
Several options are available to control the display. These can
324
be passed as parameters to the constructor or assigned at any time:
327
Show percentage complete.
329
Show rotating baton. This ticks over on every update even
330
if the values don't change.
332
Show predicted time-to-completion.
336
Show numerical counts.
338
The output file should be in line-buffered or unbuffered mode.
342
@deprecated_function(deprecated_in((1, 16, 0)))
343
def __init__(self, **kwargs):
344
from bzrlib.osutils import terminal_width
345
_BaseProgressBar.__init__(self, **kwargs)
347
self.width = terminal_width()
348
self.last_updates = []
349
self._max_last_updates = 10
350
self.child_fraction = 0
351
self._have_output = False
353
def throttle(self, old_msg):
354
"""Return True if the bar was updated too recently"""
355
# time.time consistently takes 40/4000 ms = 0.01 ms.
356
# time.clock() is faster, but gives us CPU time, not wall-clock time
358
if self.start_time is not None and (now - self.start_time) < 1:
360
if old_msg != self.last_msg:
362
interval = now - self.last_update
364
if interval < self.MIN_PAUSE:
367
self.last_updates.append(now - self.last_update)
368
# Don't let the queue grow without bound
369
self.last_updates = self.last_updates[-self._max_last_updates:]
370
self.last_update = now
374
self.update(self.last_msg, self.last_cnt, self.last_total,
377
def child_update(self, message, current, total):
378
if current is not None and total != 0:
379
child_fraction = float(current) / total
380
if self.last_cnt is None:
382
elif self.last_cnt + child_fraction <= self.last_total:
383
self.child_fraction = child_fraction
384
if self.last_msg is None:
388
def update(self, msg, current_cnt=None, total_cnt=None,
390
"""Update and redraw progress bar.
395
if total_cnt is None:
396
total_cnt = self.last_total
401
if current_cnt > total_cnt:
402
total_cnt = current_cnt
404
## # optional corner case optimisation
405
## # currently does not seem to fire so costs more than saved.
406
## # trivial optimal case:
407
## # NB if callers are doing a clear and restore with
408
## # the saved values, this will prevent that:
409
## # in that case add a restore method that calls
410
## # _do_update or some such
411
## if (self.last_msg == msg and
412
## self.last_cnt == current_cnt and
413
## self.last_total == total_cnt and
414
## self.child_fraction == child_fraction):
420
old_msg = self.last_msg
421
# save these for the tick() function
423
self.last_cnt = current_cnt
424
self.last_total = total_cnt
425
self.child_fraction = child_fraction
427
# each function call takes 20ms/4000 = 0.005 ms,
428
# but multiple that by 4000 calls -> starts to cost.
429
# so anything to make this function call faster
430
# will improve base 'diff' time by up to 0.1 seconds.
431
if self.throttle(old_msg):
434
if self.show_eta and self.start_time and self.last_total:
435
eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
436
self.last_total, last_updates = self.last_updates)
437
eta_str = " " + str_tdelta(eta)
441
if self.show_spinner:
442
spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '
446
# always update this; it's also used for the bar
449
if self.show_pct and self.last_total and self.last_cnt:
450
pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
451
pct_str = ' (%5.1f%%)' % pct
455
if not self.show_count:
457
elif self.last_cnt is None:
459
elif self.last_total is None:
460
count_str = ' %i' % (self.last_cnt)
462
# make both fields the same size
463
t = '%i' % (self.last_total)
464
c = '%*i' % (len(t), self.last_cnt)
465
count_str = ' ' + c + '/' + t
468
# progress bar, if present, soaks up all remaining space
469
cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
470
- len(eta_str) - len(count_str) - 3
473
# number of markers highlighted in bar
474
markers = int(round(float(cols) *
475
(self.last_cnt + self.child_fraction) / self.last_total))
476
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
478
# don't know total, so can't show completion.
479
# so just show an expanded spinning thingy
480
m = self.spin_pos % cols
481
ms = (' ' * m + '*').ljust(cols)
483
bar_str = '[' + ms + '] '
489
m = spin_str + bar_str + self.last_msg + count_str \
491
self.to_file.write('\r%-*.*s' % (self.width - 1, self.width - 1, m))
492
self._have_output = True
493
#self.to_file.flush()
496
if self._have_output:
497
self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
498
self._have_output = False
499
#self.to_file.flush()
504
class ChildProgress(_BaseProgressBar):
505
"""A progress indicator that pushes its data to the parent"""
507
@deprecated_function(deprecated_in((1, 16, 0)))
508
def __init__(self, _stack, **kwargs):
509
_BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
510
self.parent = _stack.top()
513
self.child_fraction = 0
516
def update(self, msg, current_cnt=None, total_cnt=None):
517
self.current = current_cnt
518
if total_cnt is not None:
519
self.total = total_cnt
521
self.child_fraction = 0
524
def child_update(self, message, current, total):
525
if current is None or total == 0:
526
self.child_fraction = 0
528
self.child_fraction = float(current) / total
532
if self.current is None:
535
count = self.current+self.child_fraction
536
if count > self.total:
538
mutter('clamping count of %d to %d' % (count, self.total))
540
self.parent.child_update(self.message, count, self.total)
545
def note(self, *args, **kwargs):
546
self.parent.note(*args, **kwargs)
236
549
def str_tdelta(delt):