152
129
own_fraction = 0.0
153
130
return self._parent_task._overall_completion_fraction(own_fraction)
155
@deprecated_method(deprecated_in((2, 1, 0)))
156
132
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.
133
"""Record a note without disrupting the progress bar."""
134
# XXX: shouldn't be here; put it in mutter or the ui instead
164
136
self.ui_factory.note(fmt_string % args)
166
138
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. --
176
if self.progress_view:
177
self.progress_view.clear()
141
# XXX: shouldn't be here; put it in mutter or the ui instead
142
self.ui_factory.clear_term()
145
@deprecated_function(deprecated_in((1, 16, 0)))
146
def ProgressBar(to_file=None, **kwargs):
147
"""Construct a progress bar.
149
Deprecated; ask the ui_factory for a progress task instead.
153
requested_bar_type = os.environ.get('BZR_PROGRESS_BAR')
154
# An value of '' or not set reverts to standard processing
155
if requested_bar_type in (None, ''):
156
if _supports_progress(to_file):
157
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.
159
return DummyProgress(to_file=to_file, **kwargs)
161
# Minor sanitation to prevent spurious errors
162
requested_bar_type = requested_bar_type.lower().strip()
163
# TODO: jam 20060710 Arguably we shouldn't raise an exception
164
# but should instead just disable progress bars if we
165
# don't recognize the type
166
if requested_bar_type not in _progress_bar_types:
167
raise errors.InvalidProgressBarType(requested_bar_type,
168
_progress_bar_types.keys())
169
return _progress_bar_types[requested_bar_type](to_file=to_file, **kwargs)
183
172
class _BaseProgressBar(object):
185
174
def __init__(self,
255
245
return DummyProgress(**kwargs)
248
class DotsProgressBar(_BaseProgressBar):
250
@deprecated_function(deprecated_in((1, 16, 0)))
251
def __init__(self, **kwargs):
252
_BaseProgressBar.__init__(self, **kwargs)
259
def update(self, msg=None, current_cnt=None, total_cnt=None):
260
if msg and msg != self.last_msg:
262
self.to_file.write('\n')
263
self.to_file.write(msg + ': ')
266
self.to_file.write('.')
270
self.to_file.write('\n')
273
def child_update(self, message, current, total):
277
class TTYProgressBar(_BaseProgressBar):
278
"""Progress bar display object.
280
Several options are available to control the display. These can
281
be passed as parameters to the constructor or assigned at any time:
284
Show percentage complete.
286
Show rotating baton. This ticks over on every update even
287
if the values don't change.
289
Show predicted time-to-completion.
293
Show numerical counts.
295
The output file should be in line-buffered or unbuffered mode.
299
@deprecated_function(deprecated_in((1, 16, 0)))
300
def __init__(self, **kwargs):
301
from bzrlib.osutils import terminal_width
302
_BaseProgressBar.__init__(self, **kwargs)
304
self.width = terminal_width()
305
self.last_updates = []
306
self._max_last_updates = 10
307
self.child_fraction = 0
308
self._have_output = False
310
def throttle(self, old_msg):
311
"""Return True if the bar was updated too recently"""
312
# time.time consistently takes 40/4000 ms = 0.01 ms.
313
# time.clock() is faster, but gives us CPU time, not wall-clock time
315
if self.start_time is not None and (now - self.start_time) < 1:
317
if old_msg != self.last_msg:
319
interval = now - self.last_update
321
if interval < self.MIN_PAUSE:
324
self.last_updates.append(now - self.last_update)
325
# Don't let the queue grow without bound
326
self.last_updates = self.last_updates[-self._max_last_updates:]
327
self.last_update = now
331
self.update(self.last_msg, self.last_cnt, self.last_total,
334
def child_update(self, message, current, total):
335
if current is not None and total != 0:
336
child_fraction = float(current) / total
337
if self.last_cnt is None:
339
elif self.last_cnt + child_fraction <= self.last_total:
340
self.child_fraction = child_fraction
341
if self.last_msg is None:
345
def update(self, msg, current_cnt=None, total_cnt=None,
347
"""Update and redraw progress bar.
352
if total_cnt is None:
353
total_cnt = self.last_total
358
if current_cnt > total_cnt:
359
total_cnt = current_cnt
361
## # optional corner case optimisation
362
## # currently does not seem to fire so costs more than saved.
363
## # trivial optimal case:
364
## # NB if callers are doing a clear and restore with
365
## # the saved values, this will prevent that:
366
## # in that case add a restore method that calls
367
## # _do_update or some such
368
## if (self.last_msg == msg and
369
## self.last_cnt == current_cnt and
370
## self.last_total == total_cnt and
371
## self.child_fraction == child_fraction):
377
old_msg = self.last_msg
378
# save these for the tick() function
380
self.last_cnt = current_cnt
381
self.last_total = total_cnt
382
self.child_fraction = child_fraction
384
# each function call takes 20ms/4000 = 0.005 ms,
385
# but multiple that by 4000 calls -> starts to cost.
386
# so anything to make this function call faster
387
# will improve base 'diff' time by up to 0.1 seconds.
388
if self.throttle(old_msg):
391
if self.show_eta and self.start_time and self.last_total:
392
eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
393
self.last_total, last_updates = self.last_updates)
394
eta_str = " " + str_tdelta(eta)
398
if self.show_spinner:
399
spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '
403
# always update this; it's also used for the bar
406
if self.show_pct and self.last_total and self.last_cnt:
407
pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
408
pct_str = ' (%5.1f%%)' % pct
412
if not self.show_count:
414
elif self.last_cnt is None:
416
elif self.last_total is None:
417
count_str = ' %i' % (self.last_cnt)
419
# make both fields the same size
420
t = '%i' % (self.last_total)
421
c = '%*i' % (len(t), self.last_cnt)
422
count_str = ' ' + c + '/' + t
425
# progress bar, if present, soaks up all remaining space
426
cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
427
- len(eta_str) - len(count_str) - 3
430
# number of markers highlighted in bar
431
markers = int(round(float(cols) *
432
(self.last_cnt + self.child_fraction) / self.last_total))
433
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
435
# don't know total, so can't show completion.
436
# so just show an expanded spinning thingy
437
m = self.spin_pos % cols
438
ms = (' ' * m + '*').ljust(cols)
440
bar_str = '[' + ms + '] '
446
m = spin_str + bar_str + self.last_msg + count_str \
448
self.to_file.write('\r%-*.*s' % (self.width - 1, self.width - 1, m))
449
self._have_output = True
450
#self.to_file.flush()
453
if self._have_output:
454
self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
455
self._have_output = False
456
#self.to_file.flush()
459
class ChildProgress(_BaseProgressBar):
460
"""A progress indicator that pushes its data to the parent"""
462
@deprecated_function(deprecated_in((1, 16, 0)))
463
def __init__(self, _stack, **kwargs):
464
_BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
465
self.parent = _stack.top()
468
self.child_fraction = 0
471
def update(self, msg, current_cnt=None, total_cnt=None):
472
self.current = current_cnt
473
if total_cnt is not None:
474
self.total = total_cnt
476
self.child_fraction = 0
479
def child_update(self, message, current, total):
480
if current is None or total == 0:
481
self.child_fraction = 0
483
self.child_fraction = float(current) / total
487
if self.current is None:
490
count = self.current+self.child_fraction
491
if count > self.total:
493
mutter('clamping count of %d to %d' % (count, self.total))
495
self.parent.child_update(self.message, count, self.total)
500
def note(self, *args, **kwargs):
501
self.parent.note(*args, **kwargs)
258
504
def str_tdelta(delt):