137
156
def make_progress_view(self):
138
157
"""Construct and return a new ProgressView subclass for this UI.
140
# if the user specifically requests either text or no progress bars,
141
# always do that. otherwise, guess based on $TERM and tty presence.
142
if os.environ.get('BZR_PROGRESS_BAR') == 'text':
159
# with --quiet, never any progress view
160
# <https://bugs.edge.launchpad.net/bzr/+bug/320035>. Otherwise if the
161
# user specifically requests either text or no progress bars, always
162
# do that. otherwise, guess based on $TERM and tty presence.
164
return NullProgressView()
165
elif os.environ.get('BZR_PROGRESS_BAR') == 'text':
143
166
return TextProgressView(self.stderr)
144
167
elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
145
168
return NullProgressView()
149
172
return NullProgressView()
174
def _make_output_stream_explicit(self, encoding, encoding_type):
175
if encoding_type == 'exact':
176
# force sys.stdout to be binary stream on win32;
177
# NB: this leaves the file set in that mode; may cause problems if
178
# one process tries to do binary and then text output
179
if sys.platform == 'win32':
180
fileno = getattr(self.stdout, 'fileno', None)
183
msvcrt.setmode(fileno(), os.O_BINARY)
184
return TextUIOutputStream(self, self.stdout)
186
encoded_stdout = codecs.getwriter(encoding)(self.stdout,
187
errors=encoding_type)
188
# For whatever reason codecs.getwriter() does not advertise its encoding
189
# it just returns the encoding of the wrapped file, which is completely
190
# bogus. So set the attribute, so we can find the correct encoding later.
191
encoded_stdout.encoding = encoding
192
return TextUIOutputStream(self, encoded_stdout)
151
194
def note(self, msg):
152
195
"""Write an already-formatted message, clearing the progress bar if necessary."""
153
196
self.clear_term()
315
396
This may update a progress bar, spinner, or similar display.
316
397
By default it does nothing.
318
# XXX: Probably there should be a transport activity model, and that
319
# too should be seen by the progress view, rather than being poked in
321
if not self._have_output:
322
# As a workaround for <https://launchpad.net/bugs/321935> we only
323
# show transport activity when there's already a progress bar
324
# shown, which time the application code is expected to know to
325
# clear off the progress bar when it's going to send some other
326
# output. Eventually it would be nice to have that automatically
399
# XXX: there should be a transport activity model, and that too should
400
# be seen by the progress view, rather than being poked in here.
329
401
self._total_byte_count += byte_count
330
402
self._bytes_since_update += byte_count
403
if self._first_byte_time is None:
404
# Note that this isn't great, as technically it should be the time
405
# when the bytes started transferring, not when they completed.
406
# However, we usually start with a small request anyway.
407
self._first_byte_time = time.time()
408
if direction in self._bytes_by_direction:
409
self._bytes_by_direction[direction] += byte_count
411
self._bytes_by_direction['unknown'] += byte_count
412
if 'no_activity' in debug.debug_flags:
413
# Can be used as a workaround if
414
# <https://launchpad.net/bugs/321935> reappears and transport
415
# activity is cluttering other output. However, thanks to
416
# TextUIOutputStream this shouldn't be a problem any more.
331
418
now = time.time()
419
if self._total_byte_count < 2000:
420
# a little resistance at first, so it doesn't stay stuck at 0
421
# while connecting...
332
423
if self._transport_update_time is None:
333
424
self._transport_update_time = now
334
425
elif now >= (self._transport_update_time + 0.5):
335
426
# guard against clock stepping backwards, and don't update too
337
rate = self._bytes_since_update / (now - self._transport_update_time)
338
msg = ("%6dKB %5dKB/s" %
339
(self._total_byte_count>>10, int(rate)>>10,))
428
rate = (self._bytes_since_update
429
/ (now - self._transport_update_time))
430
# using base-10 units (see HACKING.txt).
431
msg = ("%6dkB %5dkB/s" %
432
(self._total_byte_count / 1000, int(rate) / 1000,))
340
433
self._transport_update_time = now
341
434
self._last_repaint = now
342
435
self._bytes_since_update = 0
343
436
self._last_transport_msg = msg
439
def _format_bytes_by_direction(self):
440
if self._first_byte_time is None:
443
transfer_time = time.time() - self._first_byte_time
444
if transfer_time < 0.001:
445
transfer_time = 0.001
446
bps = self._total_byte_count / transfer_time
448
# using base-10 units (see HACKING.txt).
449
msg = ('Transferred: %.0fkB'
450
' (%.1fkB/s r:%.0fkB w:%.0fkB'
451
% (self._total_byte_count / 1000.,
453
self._bytes_by_direction['read'] / 1000.,
454
self._bytes_by_direction['write'] / 1000.,
456
if self._bytes_by_direction['unknown'] > 0:
457
msg += ' u:%.0fkB)' % (
458
self._bytes_by_direction['unknown'] / 1000.
464
def log_transport_activity(self, display=False):
465
msg = self._format_bytes_by_direction()
467
if display and self._total_byte_count > 0:
469
self._term_file.write(msg + '\n')
472
class TextUIOutputStream(object):
473
"""Decorates an output stream so that the terminal is cleared before writing.
475
This is supposed to ensure that the progress bar does not conflict with bulk
478
# XXX: this does not handle the case of writing part of a line, then doing
479
# progress bar output: the progress bar will probably write over it.
480
# one option is just to buffer that text until we have a full line;
481
# another is to save and restore it
483
# XXX: might need to wrap more methods
485
def __init__(self, ui_factory, wrapped_stream):
486
self.ui_factory = ui_factory
487
self.wrapped_stream = wrapped_stream
488
# this does no transcoding, but it must expose the underlying encoding
489
# because some callers need to know what can be written - see for
490
# example unescape_for_display.
491
self.encoding = getattr(wrapped_stream, 'encoding', None)
494
self.ui_factory.clear_term()
495
self.wrapped_stream.flush()
497
def write(self, to_write):
498
self.ui_factory.clear_term()
499
self.wrapped_stream.write(to_write)
501
def writelines(self, lines):
502
self.ui_factory.clear_term()
503
self.wrapped_stream.writelines(lines)