204
203
self._progress_view.show_transport_activity(transport,
205
204
direction, byte_count)
207
def log_transport_activity(self, display=False):
208
"""See UIFactory.log_transport_activity()"""
209
log = getattr(self._progress_view, 'log_transport_activity', None)
213
206
def show_error(self, msg):
214
207
self.clear_term()
215
208
self.stderr.write("bzr: error: %s\n" % msg)
255
248
self._term_file = term_file
256
249
# true when there's output on the screen we may need to clear
257
250
self._have_output = False
251
# XXX: We could listen for SIGWINCH and update the terminal width...
252
# https://launchpad.net/bugs/316357
253
self._width = osutils.terminal_width()
258
254
self._last_transport_msg = ''
259
255
self._spin_pos = 0
260
256
# time we last repainted the screen
264
260
self._last_task = None
265
261
self._total_byte_count = 0
266
262
self._bytes_since_update = 0
267
self._bytes_by_direction = {'unknown': 0, 'read': 0, 'write': 0}
268
self._first_byte_time = None
269
263
self._fraction = 0
270
264
# force the progress bar to be off, as at the moment it doesn't
271
265
# correspond reliably to overall command progress
274
268
def _show_line(self, s):
275
269
# sys.stderr.write("progress %r\n" % s)
276
width = osutils.terminal_width()
277
if width is not None:
278
# we need one extra space for terminals that wrap on last char
280
s = '%-*.*s' % (width, width, s)
270
if self._width is not None:
272
s = '%-*.*s' % (n, n, s)
281
273
self._term_file.write('\r' + s + '\r')
310
302
markers = int(round(float(cols) * completion_fraction)) - 1
311
303
bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
313
elif (self._last_task is None) or self._last_task.show_spinner:
305
elif self._last_task.show_spinner:
314
306
# The last task wanted just a spinner, no bar
315
307
spin_str = r'/-\|'[self._spin_pos % 4]
316
308
self._spin_pos += 1
378
370
This may update a progress bar, spinner, or similar display.
379
371
By default it does nothing.
381
# XXX: there should be a transport activity model, and that too should
382
# be seen by the progress view, rather than being poked in here.
373
# XXX: Probably there should be a transport activity model, and that
374
# too should be seen by the progress view, rather than being poked in
376
if not self._have_output:
377
# As a workaround for <https://launchpad.net/bugs/321935> we only
378
# show transport activity when there's already a progress bar
379
# shown, which time the application code is expected to know to
380
# clear off the progress bar when it's going to send some other
381
# output. Eventually it would be nice to have that automatically
383
384
self._total_byte_count += byte_count
384
385
self._bytes_since_update += byte_count
385
if self._first_byte_time is None:
386
# Note that this isn't great, as technically it should be the time
387
# when the bytes started transferring, not when they completed.
388
# However, we usually start with a small request anyway.
389
self._first_byte_time = time.time()
390
if direction in self._bytes_by_direction:
391
self._bytes_by_direction[direction] += byte_count
393
self._bytes_by_direction['unknown'] += byte_count
394
if 'no_activity' in debug.debug_flags:
395
# Can be used as a workaround if
396
# <https://launchpad.net/bugs/321935> reappears and transport
397
# activity is cluttering other output. However, thanks to
398
# TextUIOutputStream this shouldn't be a problem any more.
400
386
now = time.time()
401
387
if self._total_byte_count < 2000:
402
388
# a little resistance at first, so it doesn't stay stuck at 0
416
402
self._last_transport_msg = msg
419
def _format_bytes_by_direction(self):
420
if self._first_byte_time is None:
423
transfer_time = time.time() - self._first_byte_time
424
if transfer_time < 0.001:
425
transfer_time = 0.001
426
bps = self._total_byte_count / transfer_time
428
msg = ('Transferred: %.0fKiB'
429
' (%.1fK/s r:%.0fK w:%.0fK'
430
% (self._total_byte_count / 1024.,
432
self._bytes_by_direction['read'] / 1024.,
433
self._bytes_by_direction['write'] / 1024.,
435
if self._bytes_by_direction['unknown'] > 0:
436
msg += ' u:%.0fK)' % (
437
self._bytes_by_direction['unknown'] / 1024.
443
def log_transport_activity(self, display=False):
444
msg = self._format_bytes_by_direction()
446
if display and self._total_byte_count > 0:
448
self._term_file.write(msg + '\n')
451
406
class TextUIOutputStream(object):
452
407
"""Decorates an output stream so that the terminal is cleared before writing.