152
150
own_fraction = 0.0
153
151
return self._parent_task._overall_completion_fraction(own_fraction)
155
@deprecated_method(deprecated_in((2, 1, 0)))
156
153
def note(self, fmt_string, *args):
157
"""Record a note without disrupting the progress bar.
159
Deprecated: use ui_factory.note() instead or bzrlib.trace. Note that
160
ui_factory.note takes just one string as the argument, not a format
161
string and arguments.
154
"""Record a note without disrupting the progress bar."""
155
# XXX: shouldn't be here; put it in mutter or the ui instead
164
157
self.ui_factory.note(fmt_string % args)
166
159
self.ui_factory.note(fmt_string)
169
# TODO: deprecate this method; the model object shouldn't be concerned
170
# with whether it's shown or not. Most callers use this because they
171
# want to write some different non-progress output to the screen, but
172
# they should probably instead use a stream that's synchronized with
173
# the progress output. It may be there is a model-level use for
174
# saying "this task's not active at the moment" but I don't see it. --
162
# XXX: shouldn't be here; put it in mutter or the ui instead
176
163
if self.progress_view:
177
164
self.progress_view.clear()
179
166
self.ui_factory.clear_term()
169
@deprecated_function(deprecated_in((1, 16, 0)))
170
def ProgressBar(to_file=None, **kwargs):
171
"""Abstract factory"""
174
requested_bar_type = os.environ.get('BZR_PROGRESS_BAR')
175
# An value of '' or not set reverts to standard processing
176
if requested_bar_type in (None, ''):
177
if _supports_progress(to_file):
178
return TTYProgressBar(to_file=to_file, **kwargs)
180
return DummyProgress(to_file=to_file, **kwargs)
182
# Minor sanitation to prevent spurious errors
183
requested_bar_type = requested_bar_type.lower().strip()
184
# TODO: jam 20060710 Arguably we shouldn't raise an exception
185
# but should instead just disable progress bars if we
186
# don't recognize the type
187
if requested_bar_type not in _progress_bar_types:
188
raise errors.InvalidProgressBarType(requested_bar_type,
189
_progress_bar_types.keys())
190
return _progress_bar_types[requested_bar_type](to_file=to_file, **kwargs)
182
193
# NOTE: This is also deprecated; you should provide a ProgressView instead.
183
194
class _BaseProgressBar(object):
255
267
return DummyProgress(**kwargs)
270
class DotsProgressBar(_BaseProgressBar):
272
@deprecated_function(deprecated_in((1, 16, 0)))
273
def __init__(self, **kwargs):
274
_BaseProgressBar.__init__(self, **kwargs)
281
def update(self, msg=None, current_cnt=None, total_cnt=None):
282
if msg and msg != self.last_msg:
284
self.to_file.write('\n')
285
self.to_file.write(msg + ': ')
288
self.to_file.write('.')
292
self.to_file.write('\n')
295
def child_update(self, message, current, total):
299
class TTYProgressBar(_BaseProgressBar):
300
"""Progress bar display object.
302
Several options are available to control the display. These can
303
be passed as parameters to the constructor or assigned at any time:
306
Show percentage complete.
308
Show rotating baton. This ticks over on every update even
309
if the values don't change.
311
Show predicted time-to-completion.
315
Show numerical counts.
317
The output file should be in line-buffered or unbuffered mode.
321
@deprecated_function(deprecated_in((1, 16, 0)))
322
def __init__(self, **kwargs):
323
from bzrlib.osutils import terminal_width
324
_BaseProgressBar.__init__(self, **kwargs)
326
self.width = terminal_width()
327
self.last_updates = []
328
self._max_last_updates = 10
329
self.child_fraction = 0
330
self._have_output = False
332
def throttle(self, old_msg):
333
"""Return True if the bar was updated too recently"""
334
# time.time consistently takes 40/4000 ms = 0.01 ms.
335
# time.clock() is faster, but gives us CPU time, not wall-clock time
337
if self.start_time is not None and (now - self.start_time) < 1:
339
if old_msg != self.last_msg:
341
interval = now - self.last_update
343
if interval < self.MIN_PAUSE:
346
self.last_updates.append(now - self.last_update)
347
# Don't let the queue grow without bound
348
self.last_updates = self.last_updates[-self._max_last_updates:]
349
self.last_update = now
353
self.update(self.last_msg, self.last_cnt, self.last_total,
356
def child_update(self, message, current, total):
357
if current is not None and total != 0:
358
child_fraction = float(current) / total
359
if self.last_cnt is None:
361
elif self.last_cnt + child_fraction <= self.last_total:
362
self.child_fraction = child_fraction
363
if self.last_msg is None:
367
def update(self, msg, current_cnt=None, total_cnt=None,
369
"""Update and redraw progress bar.
374
if total_cnt is None:
375
total_cnt = self.last_total
380
if current_cnt > total_cnt:
381
total_cnt = current_cnt
383
## # optional corner case optimisation
384
## # currently does not seem to fire so costs more than saved.
385
## # trivial optimal case:
386
## # NB if callers are doing a clear and restore with
387
## # the saved values, this will prevent that:
388
## # in that case add a restore method that calls
389
## # _do_update or some such
390
## if (self.last_msg == msg and
391
## self.last_cnt == current_cnt and
392
## self.last_total == total_cnt and
393
## self.child_fraction == child_fraction):
399
old_msg = self.last_msg
400
# save these for the tick() function
402
self.last_cnt = current_cnt
403
self.last_total = total_cnt
404
self.child_fraction = child_fraction
406
# each function call takes 20ms/4000 = 0.005 ms,
407
# but multiple that by 4000 calls -> starts to cost.
408
# so anything to make this function call faster
409
# will improve base 'diff' time by up to 0.1 seconds.
410
if self.throttle(old_msg):
413
if self.show_eta and self.start_time and self.last_total:
414
eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
415
self.last_total, last_updates = self.last_updates)
416
eta_str = " " + str_tdelta(eta)
420
if self.show_spinner:
421
spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '
425
# always update this; it's also used for the bar
428
if self.show_pct and self.last_total and self.last_cnt:
429
pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
430
pct_str = ' (%5.1f%%)' % pct
434
if not self.show_count:
436
elif self.last_cnt is None:
438
elif self.last_total is None:
439
count_str = ' %i' % (self.last_cnt)
441
# make both fields the same size
442
t = '%i' % (self.last_total)
443
c = '%*i' % (len(t), self.last_cnt)
444
count_str = ' ' + c + '/' + t
447
# progress bar, if present, soaks up all remaining space
448
cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
449
- len(eta_str) - len(count_str) - 3
452
# number of markers highlighted in bar
453
markers = int(round(float(cols) *
454
(self.last_cnt + self.child_fraction) / self.last_total))
455
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
457
# don't know total, so can't show completion.
458
# so just show an expanded spinning thingy
459
m = self.spin_pos % cols
460
ms = (' ' * m + '*').ljust(cols)
462
bar_str = '[' + ms + '] '
468
m = spin_str + bar_str + self.last_msg + count_str \
470
self.to_file.write('\r%-*.*s' % (self.width - 1, self.width - 1, m))
471
self._have_output = True
472
#self.to_file.flush()
475
if self._have_output:
476
self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
477
self._have_output = False
478
#self.to_file.flush()
483
class ChildProgress(_BaseProgressBar):
484
"""A progress indicator that pushes its data to the parent"""
486
@deprecated_function(deprecated_in((1, 16, 0)))
487
def __init__(self, _stack, **kwargs):
488
_BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
489
self.parent = _stack.top()
492
self.child_fraction = 0
495
def update(self, msg, current_cnt=None, total_cnt=None):
496
self.current = current_cnt
497
if total_cnt is not None:
498
self.total = total_cnt
500
self.child_fraction = 0
503
def child_update(self, message, current, total):
504
if current is None or total == 0:
505
self.child_fraction = 0
507
self.child_fraction = float(current) / total
511
if self.current is None:
514
count = self.current+self.child_fraction
515
if count > self.total:
517
mutter('clamping count of %d to %d' % (count, self.total))
519
self.parent.child_update(self.message, count, self.total)
524
def note(self, *args, **kwargs):
525
self.parent.note(*args, **kwargs)
258
528
def str_tdelta(delt):