1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005, 2008, 2009 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
def be_quiet(self, state):
65
if state and not self._quiet:
67
UIFactory.be_quiet(self, state)
68
self._progress_view = self.make_progress_view()
70
64
def clear_term(self):
71
65
"""Prepare the terminal for output.
114
108
password = password[:-1]
117
def get_password(self, prompt=u'', **kwargs):
111
def get_password(self, prompt='', **kwargs):
118
112
"""Prompt the user for a password.
120
114
:param prompt: The prompt to present the user
152
146
def make_progress_view(self):
153
147
"""Construct and return a new ProgressView subclass for this UI.
155
# with --quiet, never any progress view
156
# <https://bugs.launchpad.net/bzr/+bug/320035>. Otherwise if the
157
# user specifically requests either text or no progress bars, always
158
# do that. otherwise, guess based on $TERM and tty presence.
160
return NullProgressView()
161
elif os.environ.get('BZR_PROGRESS_BAR') == 'text':
149
# if the user specifically requests either text or no progress bars,
150
# always do that. otherwise, guess based on $TERM and tty presence.
151
if os.environ.get('BZR_PROGRESS_BAR') == 'text':
162
152
return TextProgressView(self.stderr)
163
153
elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
164
154
return NullProgressView()
198
188
:param kwargs: Dictionary of arguments to insert into the prompt,
199
189
to allow UIs to reformat the prompt.
201
if type(prompt) != unicode:
202
raise ValueError("prompt %r not a unicode string" % prompt)
204
192
# See <https://launchpad.net/bugs/365891>
205
193
prompt = prompt % kwargs
232
220
def show_warning(self, msg):
233
221
self.clear_term()
234
if isinstance(msg, unicode):
235
te = osutils.get_terminal_encoding()
236
msg = msg.encode(te, 'replace')
237
222
self.stderr.write("bzr: warning: %s\n" % msg)
239
224
def _progress_updated(self, task):
243
228
warnings.warn("%r updated but no tasks are active" %
245
230
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]))
231
warnings.warn("%r is not the top progress task %r" %
232
(task, self._task_stack[-1]))
252
233
self._progress_view.show_progress(task)
254
235
def _progress_all_finished(self):
255
236
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
239
class TextProgressView(object):
271
240
"""Display of progress bar and other information on a tty.
302
271
# correspond reliably to overall command progress
303
272
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
274
def _show_line(self, s):
314
275
# sys.stderr.write("progress %r\n" % s)
315
width = self._avail_width()
276
width = osutils.terminal_width()
316
277
if width is not None:
278
# we need one extra space for terminals that wrap on last char
317
280
s = '%-*.*s' % (width, width, s)
318
281
self._term_file.write('\r' + s + '\r')
358
321
def _format_task(self, task):
359
"""Format task-specific parts of progress bar.
361
:returns: (text_part, counter_part) both unicode strings.
363
322
if not task.show_count:
365
324
elif task.current_cnt is not None and task.total_cnt is not None:
375
334
t = t._parent_task
377
336
m = t.msg + ':' + m
380
339
def _render_line(self):
381
340
bar_string = self._render_bar()
382
341
if self._last_task:
383
task_part, counter_part = self._format_task(self._last_task)
342
task_msg = self._format_task(self._last_task)
385
task_part = counter_part = ''
386
345
if self._last_task and not self._last_task.show_transport_activity:
389
348
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:
351
return (bar_string + trans + task_msg)
412
353
def _repaint(self):
413
354
s = self._render_line()
466
407
elif now >= (self._transport_update_time + 0.5):
467
408
# 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,))
410
rate = self._bytes_since_update / (now - self._transport_update_time)
411
msg = ("%6dKB %5dKB/s" %
412
(self._total_byte_count>>10, int(rate)>>10,))
474
413
self._transport_update_time = now
475
414
self._last_repaint = now
476
415
self._bytes_since_update = 0
486
425
transfer_time = 0.001
487
426
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.,
428
msg = ('Transferred: %.0fKiB'
429
' (%.1fK/s r:%.0fK w:%.0fK'
430
% (self._total_byte_count / 1024.,
432
self._bytes_by_direction['read'] / 1024.,
433
self._bytes_by_direction['write'] / 1024.,
497
435
if self._bytes_by_direction['unknown'] > 0:
498
msg += ' u:%.0fkB)' % (
499
self._bytes_by_direction['unknown'] / 1000.
436
msg += ' u:%.0fK)' % (
437
self._bytes_by_direction['unknown'] / 1024.