1
# Copyright (C) 2005-2011 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:
114
114
password = password[:-1]
117
def get_password(self, prompt=u'', **kwargs):
117
def get_password(self, prompt='', **kwargs):
118
118
"""Prompt the user for a password.
120
120
:param prompt: The prompt to present the user
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():
198
198
:param kwargs: Dictionary of arguments to insert into the prompt,
199
199
to allow UIs to reformat the prompt.
201
if type(prompt) != unicode:
202
raise ValueError("prompt %r not a unicode string" % prompt)
204
202
# See <https://launchpad.net/bugs/365891>
205
203
prompt = prompt % kwargs
232
230
def show_warning(self, msg):
233
231
self.clear_term()
234
if isinstance(msg, unicode):
235
te = osutils.get_terminal_encoding()
236
msg = msg.encode(te, 'replace')
237
232
self.stderr.write("bzr: warning: %s\n" % msg)
239
234
def _progress_updated(self, task):
243
238
warnings.warn("%r updated but no tasks are active" %
245
240
elif task != self._task_stack[-1]:
246
# We used to check it was the top task, but it's hard to always
247
# get this right and it's not necessarily useful: any actual
248
# problems will be evident in use
249
#warnings.warn("%r is not the top progress task %r" %
250
# (task, self._task_stack[-1]))
241
warnings.warn("%r is not the top progress task %r" %
242
(task, self._task_stack[-1]))
252
243
self._progress_view.show_progress(task)
254
245
def _progress_all_finished(self):
255
246
self._progress_view.clear()
257
def show_user_warning(self, warning_id, **message_args):
258
"""Show a text message to the user.
260
Explicitly not for warnings about bzr apis, deprecations or internals.
262
# eventually trace.warning should migrate here, to avoid logging and
263
# be easier to test; that has a lot of test fallout so for now just
264
# new code can call this
265
if warning_id not in self.suppressed_warnings:
266
self.stderr.write(self.format_user_warning(warning_id, message_args) +
270
249
class TextProgressView(object):
271
250
"""Display of progress bar and other information on a tty.
302
281
# correspond reliably to overall command progress
303
282
self.enable_bar = False
305
def _avail_width(self):
306
# we need one extra space for terminals that wrap on last char
307
w = osutils.terminal_width()
313
284
def _show_line(self, s):
314
285
# sys.stderr.write("progress %r\n" % s)
315
width = self._avail_width()
286
width = osutils.terminal_width()
316
287
if width is not None:
288
# we need one extra space for terminals that wrap on last char
317
290
s = '%-*.*s' % (width, width, s)
318
291
self._term_file.write('\r' + s + '\r')
358
331
def _format_task(self, task):
359
"""Format task-specific parts of progress bar.
361
:returns: (text_part, counter_part) both unicode strings.
363
332
if not task.show_count:
365
334
elif task.current_cnt is not None and task.total_cnt is not None:
375
344
t = t._parent_task
377
346
m = t.msg + ':' + m
380
349
def _render_line(self):
381
350
bar_string = self._render_bar()
382
351
if self._last_task:
383
task_part, counter_part = self._format_task(self._last_task)
352
task_msg = self._format_task(self._last_task)
385
task_part = counter_part = ''
386
355
if self._last_task and not self._last_task.show_transport_activity:
389
358
trans = self._last_transport_msg
390
# the bar separates the transport activity from the message, so even
391
# if there's no bar or spinner, we must show something if both those
393
if (task_part or trans) and not bar_string:
395
# preferentially truncate the task message if we don't have enough
397
avail_width = self._avail_width()
398
if avail_width is not None:
399
# if terminal avail_width is unknown, don't truncate
400
current_len = len(bar_string) + len(trans) + len(task_part) + len(counter_part)
401
gap = current_len - avail_width
403
task_part = task_part[:-gap-2] + '..'
404
s = trans + bar_string + task_part + counter_part
405
if avail_width is not None:
406
if len(s) < avail_width:
407
s = s.ljust(avail_width)
408
elif len(s) > avail_width:
361
return (bar_string + trans + task_msg)
412
363
def _repaint(self):
413
364
s = self._render_line()
466
417
elif now >= (self._transport_update_time + 0.5):
467
418
# guard against clock stepping backwards, and don't update too
469
rate = (self._bytes_since_update
470
/ (now - self._transport_update_time))
471
# using base-10 units (see HACKING.txt).
472
msg = ("%6dkB %5dkB/s " %
473
(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,))
474
423
self._transport_update_time = now
475
424
self._last_repaint = now
476
425
self._bytes_since_update = 0
486
435
transfer_time = 0.001
487
436
bps = self._total_byte_count / transfer_time
489
# using base-10 units (see HACKING.txt).
490
msg = ('Transferred: %.0fkB'
491
' (%.1fkB/s r:%.0fkB w:%.0fkB'
492
% (self._total_byte_count / 1000.,
494
self._bytes_by_direction['read'] / 1000.,
495
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.,
497
445
if self._bytes_by_direction['unknown'] > 0:
498
msg += ' u:%.0fkB)' % (
499
self._bytes_by_direction['unknown'] / 1000.
446
msg += ' u:%.0fK)' % (
447
self._bytes_by_direction['unknown'] / 1024.