60
62
self.stderr = stderr
61
63
# paints progress, network activity, etc
62
64
self._progress_view = self.make_progress_view()
65
# hook up the signals to watch for terminal size changes
64
68
def be_quiet(self, state):
65
69
if state and not self._quiet:
153
157
"""Construct and return a new ProgressView subclass for this UI.
155
159
# with --quiet, never any progress view
156
# <https://bugs.launchpad.net/bzr/+bug/320035>. Otherwise if the
160
# <https://bugs.edge.launchpad.net/bzr/+bug/320035>. Otherwise if the
157
161
# user specifically requests either text or no progress bars, always
158
162
# do that. otherwise, guess based on $TERM and tty presence.
159
163
if self.is_quiet():
230
234
def show_warning(self, msg):
231
235
self.clear_term()
232
if isinstance(msg, unicode):
233
te = osutils.get_terminal_encoding()
234
msg = msg.encode(te, 'replace')
235
236
self.stderr.write("bzr: warning: %s\n" % msg)
237
238
def _progress_updated(self, task):
241
242
warnings.warn("%r updated but no tasks are active" %
243
244
elif task != self._task_stack[-1]:
244
# We used to check it was the top task, but it's hard to always
245
# get this right and it's not necessarily useful: any actual
246
# problems will be evident in use
247
#warnings.warn("%r is not the top progress task %r" %
248
# (task, self._task_stack[-1]))
245
warnings.warn("%r is not the top progress task %r" %
246
(task, self._task_stack[-1]))
250
247
self._progress_view.show_progress(task)
252
249
def _progress_all_finished(self):
253
250
self._progress_view.clear()
255
def show_user_warning(self, warning_id, **message_args):
256
"""Show a text message to the user.
258
Explicitly not for warnings about bzr apis, deprecations or internals.
260
# eventually trace.warning should migrate here, to avoid logging and
261
# be easier to test; that has a lot of test fallout so for now just
262
# new code can call this
263
if warning_id not in self.suppressed_warnings:
264
self.stderr.write(self.format_user_warning(warning_id, message_args) +
268
253
class TextProgressView(object):
269
254
"""Display of progress bar and other information on a tty.
300
285
# correspond reliably to overall command progress
301
286
self.enable_bar = False
303
def _avail_width(self):
304
# we need one extra space for terminals that wrap on last char
305
w = osutils.terminal_width()
311
288
def _show_line(self, s):
312
289
# sys.stderr.write("progress %r\n" % s)
313
width = self._avail_width()
290
width = osutils.terminal_width()
314
291
if width is not None:
292
# we need one extra space for terminals that wrap on last char
315
294
s = '%-*.*s' % (width, width, s)
316
295
self._term_file.write('\r' + s + '\r')
356
335
def _format_task(self, task):
357
"""Format task-specific parts of progress bar.
359
:returns: (text_part, counter_part) both unicode strings.
361
336
if not task.show_count:
363
338
elif task.current_cnt is not None and task.total_cnt is not None:
373
348
t = t._parent_task
375
350
m = t.msg + ':' + m
378
353
def _render_line(self):
379
354
bar_string = self._render_bar()
380
355
if self._last_task:
381
task_part, counter_part = self._format_task(self._last_task)
356
task_msg = self._format_task(self._last_task)
383
task_part = counter_part = ''
384
359
if self._last_task and not self._last_task.show_transport_activity:
387
362
trans = self._last_transport_msg
388
# the bar separates the transport activity from the message, so even
389
# if there's no bar or spinner, we must show something if both those
391
if (task_part or trans) and not bar_string:
393
# preferentially truncate the task message if we don't have enough
395
avail_width = self._avail_width()
396
if avail_width is not None:
397
# if terminal avail_width is unknown, don't truncate
398
current_len = len(bar_string) + len(trans) + len(task_part) + len(counter_part)
399
gap = current_len - avail_width
401
task_part = task_part[:-gap-2] + '..'
402
s = trans + bar_string + task_part + counter_part
403
if avail_width is not None:
404
if len(s) < avail_width:
405
s = s.ljust(avail_width)
406
elif len(s) > avail_width:
365
return (bar_string + trans + task_msg)
410
367
def _repaint(self):
411
368
s = self._render_line()
464
421
elif now >= (self._transport_update_time + 0.5):
465
422
# guard against clock stepping backwards, and don't update too
467
rate = (self._bytes_since_update
468
/ (now - self._transport_update_time))
469
# using base-10 units (see HACKING.txt).
470
msg = ("%6dkB %5dkB/s " %
471
(self._total_byte_count / 1000, int(rate) / 1000,))
424
rate = self._bytes_since_update / (now - self._transport_update_time)
425
msg = ("%6dKB %5dKB/s" %
426
(self._total_byte_count>>10, int(rate)>>10,))
472
427
self._transport_update_time = now
473
428
self._last_repaint = now
474
429
self._bytes_since_update = 0
484
439
transfer_time = 0.001
485
440
bps = self._total_byte_count / transfer_time
487
# using base-10 units (see HACKING.txt).
488
msg = ('Transferred: %.0fkB'
489
' (%.1fkB/s r:%.0fkB w:%.0fkB'
490
% (self._total_byte_count / 1000.,
492
self._bytes_by_direction['read'] / 1000.,
493
self._bytes_by_direction['write'] / 1000.,
442
msg = ('Transferred: %.0fKiB'
443
' (%.1fK/s r:%.0fK w:%.0fK'
444
% (self._total_byte_count / 1024.,
446
self._bytes_by_direction['read'] / 1024.,
447
self._bytes_by_direction['write'] / 1024.,
495
449
if self._bytes_by_direction['unknown'] > 0:
496
msg += ' u:%.0fkB)' % (
497
self._bytes_by_direction['unknown'] / 1000.
450
msg += ' u:%.0fK)' % (
451
self._bytes_by_direction['unknown'] / 1024.