142
142
self.show_bar = show_bar
143
143
self.show_count = show_count
144
144
self._stack = _stack
146
self.MIN_PAUSE = 0.1 # seconds
149
self.start_time = now
150
# next update should not throttle
151
self.last_update = now - self.MIN_PAUSE - 1
146
153
def finished(self):
147
154
"""Return this bar to its progress stack."""
239
245
self.spin_pos = 0
240
246
self.width = terminal_width()
241
247
self.start_time = None
242
self.last_update = None
243
248
self.last_updates = deque()
244
249
self.child_fraction = 0
247
252
def throttle(self):
248
253
"""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:
254
# time.time consistently takes 40/4000 ms = 0.01 ms.
255
# but every single update to the pb invokes it.
256
# so we use time.clock which takes 20/4000 ms = 0.005ms
257
# on the downside, time.clock() appears to have approximately
258
# 10ms granularity, so we treat a zero-time change as 'throttled.'
261
interval = now - self.last_update
263
if interval < self.MIN_PAUSE:
258
266
self.last_updates.append(now - self.last_update)
259
267
self.last_update = now
281
289
def update(self, msg, current_cnt=None, total_cnt=None,
282
290
child_fraction=0):
283
291
"""Update and redraw progress bar."""
284
self.child_fraction = child_fraction
286
293
if current_cnt < 0:
289
296
if current_cnt > total_cnt:
290
297
total_cnt = current_cnt
299
## # optional corner case optimisation
300
## # currently does not seem to fire so costs more than saved.
301
## # trivial optimal case:
302
## # NB if callers are doing a clear and restore with
303
## # the saved values, this will prevent that:
304
## # in that case add a restore method that calls
305
## # _do_update or some such
306
## if (self.last_msg == msg and
307
## self.last_cnt == current_cnt and
308
## self.last_total == total_cnt and
309
## self.child_fraction == child_fraction):
292
312
old_msg = self.last_msg
293
313
# save these for the tick() function
294
314
self.last_msg = msg
295
315
self.last_cnt = current_cnt
296
316
self.last_total = total_cnt
317
self.child_fraction = child_fraction
319
# each function call takes 20ms/4000 = 0.005 ms,
320
# but multiple that by 4000 calls -> starts to cost.
321
# so anything to make this function call faster
322
# will improve base 'diff' time by up to 0.1 seconds.
298
323
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)
326
if self.show_eta and self.start_time and self.last_total:
327
eta = get_eta(self.start_time, self.last_cnt + self.child_fraction,
328
self.last_total, last_updates = self.last_updates)
304
329
eta_str = " " + str_tdelta(eta)
313
338
# always update this; it's also used for the bar
314
339
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)
341
if self.show_pct and self.last_total and self.last_cnt:
342
pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
318
343
pct_str = ' (%5.1f%%)' % pct
322
347
if not self.show_count:
324
elif current_cnt is None:
349
elif self.last_cnt is None:
326
elif total_cnt is None:
327
count_str = ' %i' % (current_cnt)
351
elif self.last_total is None:
352
count_str = ' %i' % (self.last_cnt)
329
354
# make both fields the same size
330
t = '%i' % (total_cnt)
331
c = '%*i' % (len(t), current_cnt)
355
t = '%i' % (self.last_total)
356
c = '%*i' % (len(t), self.last_cnt)
332
357
count_str = ' ' + c + '/' + t
334
359
if self.show_bar:
335
360
# progress bar, if present, soaks up all remaining space
336
cols = self.width - 1 - len(msg) - len(spin_str) - len(pct_str) \
361
cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
337
362
- len(eta_str) - len(count_str) - 3
340
365
# number of markers highlighted in bar
341
366
markers = int(round(float(cols) *
342
(current_cnt + child_fraction) / total_cnt))
367
(self.last_cnt + self.child_fraction) / self.last_total))
343
368
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
345
370
# don't know total, so can't show completion.
356
m = spin_str + bar_str + msg + count_str + pct_str + eta_str
381
m = spin_str + bar_str + self.last_msg + count_str + pct_str + eta_str
358
383
assert len(m) < self.width
359
384
self.to_file.write('\r' + m.ljust(self.width - 1))
395
420
count = self.current+self.child_fraction
396
421
if count > self.total:
397
mutter('clamping count of %d to %d' % (count, self.total))
423
mutter('clamping count of %d to %d' % (count, self.total))
398
424
count = self.total
399
425
self.parent.child_update(self.message, count, self.total)