1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005, 2008, 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
60
60
self.stderr = stderr
61
61
# paints progress, network activity, etc
62
62
self._progress_view = self.make_progress_view()
64
64
def be_quiet(self, state):
65
65
if state and not self._quiet:
153
153
"""Construct and return a new ProgressView subclass for this UI.
155
155
# with --quiet, never any progress view
156
# <https://bugs.launchpad.net/bzr/+bug/320035>. Otherwise if the
156
# <https://bugs.edge.launchpad.net/bzr/+bug/320035>. Otherwise if the
157
157
# user specifically requests either text or no progress bars, always
158
158
# do that. otherwise, guess based on $TERM and tty presence.
159
159
if self.is_quiet():
230
230
def show_warning(self, msg):
231
231
self.clear_term()
232
if isinstance(msg, unicode):
233
te = osutils.get_terminal_encoding()
234
msg = msg.encode(te, 'replace')
235
232
self.stderr.write("bzr: warning: %s\n" % msg)
237
234
def _progress_updated(self, task):
241
238
warnings.warn("%r updated but no tasks are active" %
243
240
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]))
241
warnings.warn("%r is not the top progress task %r" %
242
(task, self._task_stack[-1]))
250
243
self._progress_view.show_progress(task)
252
245
def _progress_all_finished(self):
253
246
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
249
class TextProgressView(object):
269
250
"""Display of progress bar and other information on a tty.
300
281
# correspond reliably to overall command progress
301
282
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
284
def _show_line(self, s):
312
285
# sys.stderr.write("progress %r\n" % s)
313
width = self._avail_width()
286
width = osutils.terminal_width()
314
287
if width is not None:
288
# we need one extra space for terminals that wrap on last char
315
290
s = '%-*.*s' % (width, width, s)
316
291
self._term_file.write('\r' + s + '\r')
356
331
def _format_task(self, task):
357
"""Format task-specific parts of progress bar.
359
:returns: (text_part, counter_part) both unicode strings.
361
332
if not task.show_count:
363
334
elif task.current_cnt is not None and task.total_cnt is not None:
373
344
t = t._parent_task
375
346
m = t.msg + ':' + m
378
349
def _render_line(self):
379
350
bar_string = self._render_bar()
380
351
if self._last_task:
381
task_part, counter_part = self._format_task(self._last_task)
352
task_msg = self._format_task(self._last_task)
383
task_part = counter_part = ''
384
355
if self._last_task and not self._last_task.show_transport_activity:
387
358
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:
361
return (bar_string + trans + task_msg)
410
363
def _repaint(self):
411
364
s = self._render_line()
464
417
elif now >= (self._transport_update_time + 0.5):
465
418
# 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,))
420
rate = self._bytes_since_update / (now - self._transport_update_time)
421
msg = ("%6dKB %5dKB/s" %
422
(self._total_byte_count>>10, int(rate)>>10,))
472
423
self._transport_update_time = now
473
424
self._last_repaint = now
474
425
self._bytes_since_update = 0
484
435
transfer_time = 0.001
485
436
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.,
438
msg = ('Transferred: %.0fKiB'
439
' (%.1fK/s r:%.0fK w:%.0fK'
440
% (self._total_byte_count / 1024.,
442
self._bytes_by_direction['read'] / 1024.,
443
self._bytes_by_direction['write'] / 1024.,
495
445
if self._bytes_by_direction['unknown'] > 0:
496
msg += ' u:%.0fkB)' % (
497
self._bytes_by_direction['unknown'] / 1000.
446
msg += ' u:%.0fK)' % (
447
self._bytes_by_direction['unknown'] / 1024.