142
149
self.show_bar = show_bar
143
150
self.show_count = show_count
144
151
self._stack = _stack
153
self.MIN_PAUSE = 0.1 # seconds
156
self.start_time = now
157
# next update should not throttle
158
self.last_update = now - self.MIN_PAUSE - 1
146
160
def finished(self):
147
161
"""Return this bar to its progress stack."""
152
166
def note(self, fmt_string, *args, **kwargs):
153
167
"""Record a note without disrupting the progress bar."""
168
bzrlib.ui.ui_factory.clear_term()
155
169
self.to_messages_file.write(fmt_string % args)
156
170
self.to_messages_file.write('\n')
239
252
self.spin_pos = 0
240
253
self.width = terminal_width()
241
254
self.start_time = None
242
self.last_update = None
243
255
self.last_updates = deque()
244
256
self.child_fraction = 0
247
259
def throttle(self):
248
260
"""Return True if the bar was updated too recently"""
250
if self.start_time is None:
251
self.start_time = self.last_update = now
254
interval = now - self.last_update
255
if interval > 0 and interval < self.MIN_PAUSE:
261
# time.time consistently takes 40/4000 ms = 0.01 ms.
262
# but every single update to the pb invokes it.
263
# so we use time.clock which takes 20/4000 ms = 0.005ms
264
# on the downside, time.clock() appears to have approximately
265
# 10ms granularity, so we treat a zero-time change as 'throttled.'
268
interval = now - self.last_update
270
if interval < self.MIN_PAUSE:
258
273
self.last_updates.append(now - self.last_update)
259
274
self.last_update = now
281
296
def update(self, msg, current_cnt=None, total_cnt=None,
282
297
child_fraction=0):
283
298
"""Update and redraw progress bar."""
284
self.child_fraction = child_fraction
286
300
if current_cnt < 0:
289
303
if current_cnt > total_cnt:
290
304
total_cnt = current_cnt
306
## # optional corner case optimisation
307
## # currently does not seem to fire so costs more than saved.
308
## # trivial optimal case:
309
## # NB if callers are doing a clear and restore with
310
## # the saved values, this will prevent that:
311
## # in that case add a restore method that calls
312
## # _do_update or some such
313
## if (self.last_msg == msg and
314
## self.last_cnt == current_cnt and
315
## self.last_total == total_cnt and
316
## self.child_fraction == child_fraction):
292
319
old_msg = self.last_msg
293
320
# save these for the tick() function
294
321
self.last_msg = msg
295
322
self.last_cnt = current_cnt
296
323
self.last_total = total_cnt
324
self.child_fraction = child_fraction
326
# each function call takes 20ms/4000 = 0.005 ms,
327
# but multiple that by 4000 calls -> starts to cost.
328
# so anything to make this function call faster
329
# will improve base 'diff' time by up to 0.1 seconds.
298
330
if old_msg == self.last_msg and self.throttle():
301
if self.show_eta and self.start_time and total_cnt:
302
eta = get_eta(self.start_time, current_cnt+child_fraction,
303
total_cnt, last_updates = self.last_updates)
333
if self.show_eta and self.start_time and self.last_total:
334
eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
335
self.last_total, last_updates = self.last_updates)
304
336
eta_str = " " + str_tdelta(eta)
313
345
# always update this; it's also used for the bar
314
346
self.spin_pos += 1
316
if self.show_pct and total_cnt and current_cnt:
317
pct = 100.0 * ((current_cnt + child_fraction) / total_cnt)
348
if self.show_pct and self.last_total and self.last_cnt:
349
pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
318
350
pct_str = ' (%5.1f%%)' % pct
322
354
if not self.show_count:
324
elif current_cnt is None:
356
elif self.last_cnt is None:
326
elif total_cnt is None:
327
count_str = ' %i' % (current_cnt)
358
elif self.last_total is None:
359
count_str = ' %i' % (self.last_cnt)
329
361
# make both fields the same size
330
t = '%i' % (total_cnt)
331
c = '%*i' % (len(t), current_cnt)
362
t = '%i' % (self.last_total)
363
c = '%*i' % (len(t), self.last_cnt)
332
364
count_str = ' ' + c + '/' + t
334
366
if self.show_bar:
335
367
# progress bar, if present, soaks up all remaining space
336
cols = self.width - 1 - len(msg) - len(spin_str) - len(pct_str) \
368
cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
337
369
- len(eta_str) - len(count_str) - 3
340
372
# number of markers highlighted in bar
341
373
markers = int(round(float(cols) *
342
(current_cnt + child_fraction) / total_cnt))
374
(self.last_cnt + self.child_fraction) / self.last_total))
343
375
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
345
377
# don't know total, so can't show completion.
356
m = spin_str + bar_str + msg + count_str + pct_str + eta_str
388
m = spin_str + bar_str + self.last_msg + count_str + pct_str + eta_str
358
390
assert len(m) < self.width
359
391
self.to_file.write('\r' + m.ljust(self.width - 1))
395
427
count = self.current+self.child_fraction
396
428
if count > self.total:
397
mutter('clamping count of %d to %d' % (count, self.total))
430
mutter('clamping count of %d to %d' % (count, self.total))
398
431
count = self.total
399
432
self.parent.child_update(self.message, count, self.total)