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):
285
# sys.stderr.write("progress %r\n" % s)
286
width = osutils.terminal_width()
287
if width is not None:
288
# we need one extra space for terminals that wrap on last char
290
s = '%-*.*s' % (width, width, s)
312
291
self._term_file.write('\r' + s + '\r')
352
331
def _format_task(self, task):
353
"""Format task-specific parts of progress bar.
355
:returns: (text_part, counter_part) both unicode strings.
357
332
if not task.show_count:
359
334
elif task.current_cnt is not None and task.total_cnt is not None:
369
344
t = t._parent_task
371
346
m = t.msg + ':' + m
374
349
def _render_line(self):
375
350
bar_string = self._render_bar()
376
351
if self._last_task:
377
task_part, counter_part = self._format_task(self._last_task)
352
task_msg = self._format_task(self._last_task)
379
task_part = counter_part = ''
380
355
if self._last_task and not self._last_task.show_transport_activity:
383
358
trans = self._last_transport_msg
384
# the bar separates the transport activity from the message, so even
385
# if there's no bar or spinner, we must show something if both those
387
if (task_part or trans) and not bar_string:
389
# preferentially truncate the task message if we don't have enough
391
avail_width = self._avail_width()
392
if avail_width is not None:
393
# if terminal avail_width is unknown, don't truncate
394
current_len = len(bar_string) + len(trans) + len(task_part) + len(counter_part)
395
gap = current_len - avail_width
397
task_part = task_part[:-gap-2] + '..'
398
s = trans + bar_string + task_part + counter_part
399
if avail_width is not None:
400
if len(s) < avail_width:
401
s = s.ljust(avail_width)
402
elif len(s) > avail_width:
361
return (bar_string + trans + task_msg)
406
363
def _repaint(self):
407
364
s = self._render_line()
460
417
elif now >= (self._transport_update_time + 0.5):
461
418
# guard against clock stepping backwards, and don't update too
463
rate = (self._bytes_since_update
464
/ (now - self._transport_update_time))
465
# using base-10 units (see HACKING.txt).
466
msg = ("%6dkB %5dkB/s " %
467
(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,))
468
423
self._transport_update_time = now
469
424
self._last_repaint = now
470
425
self._bytes_since_update = 0
480
435
transfer_time = 0.001
481
436
bps = self._total_byte_count / transfer_time
483
# using base-10 units (see HACKING.txt).
484
msg = ('Transferred: %.0fkB'
485
' (%.1fkB/s r:%.0fkB w:%.0fkB'
486
% (self._total_byte_count / 1000.,
488
self._bytes_by_direction['read'] / 1000.,
489
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.,
491
445
if self._bytes_by_direction['unknown'] > 0:
492
msg += ' u:%.0fkB)' % (
493
self._bytes_by_direction['unknown'] / 1000.
446
msg += ' u:%.0fK)' % (
447
self._bytes_by_direction['unknown'] / 1024.