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