50
52
:param bar_type: The type of progress bar to create. Deprecated
51
53
and ignored; a TextProgressView is always used.
53
super(TextUIFactory, self).__init__(stdin=stdin,
54
stdout=stdout, stderr=stderr)
56
symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
57
% "bar_type parameter")
55
super(TextUIFactory, self).__init__()
56
# TODO: there's no good reason not to pass all three streams, maybe we
57
# should deprecate the default values...
58
61
# paints progress, network activity, etc
59
self._progress_view = self._make_progress_view()
62
self._progress_view = self.make_progress_view()
61
64
def clear_term(self):
62
65
"""Prepare the terminal for output.
69
72
# to clear it. We might need to separately check for the case of
70
73
self._progress_view.clear()
72
def _make_progress_view(self):
73
if os.environ.get('BZR_PROGRESS_BAR') in ('text', None, ''):
75
def get_boolean(self, prompt):
77
self.prompt(prompt + "? [y/n]: ")
78
line = self.stdin.readline().lower()
79
if line in ('y\n', 'yes\n'):
81
elif line in ('n\n', 'no\n'):
83
elif line in ('', None):
84
# end-of-file; possibly should raise an error here instead
87
def get_non_echoed_password(self):
88
isatty = getattr(self.stdin, 'isatty', None)
89
if isatty is not None and isatty():
90
# getpass() ensure the password is not echoed and other
91
# cross-platform niceties
92
password = getpass.getpass('')
94
# echo doesn't make sense without a terminal
95
password = self.stdin.readline()
98
elif password[-1] == '\n':
99
password = password[:-1]
102
def get_password(self, prompt='', **kwargs):
103
"""Prompt the user for a password.
105
:param prompt: The prompt to present the user
106
:param kwargs: Arguments which will be expanded into the prompt.
107
This lets front ends display different things if
109
:return: The password string, return None if the user
110
canceled the request.
113
self.prompt(prompt, **kwargs)
114
# There's currently no way to say 'i decline to enter a password'
115
# as opposed to 'my password is empty' -- does it matter?
116
return self.get_non_echoed_password()
118
def get_username(self, prompt, **kwargs):
119
"""Prompt the user for a username.
121
:param prompt: The prompt to present the user
122
:param kwargs: Arguments which will be expanded into the prompt.
123
This lets front ends display different things if
125
:return: The username string, return None if the user
126
canceled the request.
129
self.prompt(prompt, **kwargs)
130
username = self.stdin.readline()
133
elif username[-1] == '\n':
134
username = username[:-1]
137
def make_progress_view(self):
138
"""Construct and return a new ProgressView subclass for this UI.
140
# if the user specifically requests either text or no progress bars,
141
# always do that. otherwise, guess based on $TERM and tty presence.
142
if os.environ.get('BZR_PROGRESS_BAR') == 'text':
143
return TextProgressView(self.stderr)
144
elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
145
return NullProgressView()
146
elif progress._supports_progress(self.stderr):
74
147
return TextProgressView(self.stderr)
76
149
return NullProgressView()
81
154
self.stdout.write(msg + '\n')
156
def prompt(self, prompt, **kwargs):
157
"""Emit prompt on the CLI.
159
:param kwargs: Dictionary of arguments to insert into the prompt,
160
to allow UIs to reformat the prompt.
163
# See <https://launchpad.net/bugs/365891>
164
prompt = prompt % kwargs
165
prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
167
self.stderr.write(prompt)
83
169
def report_transport_activity(self, transport, byte_count, direction):
84
170
"""Called by transports as they do IO.
104
190
self._progress_view.clear()
107
class NullProgressView(object):
108
"""Soak up and ignore progress information."""
113
def show_progress(self, task):
116
def show_transport_activity(self, transport, direction, byte_count):
120
193
class TextProgressView(object):
121
194
"""Display of progress bar and other information on a tty.
244
322
# XXX: Probably there should be a transport activity model, and that
245
323
# too should be seen by the progress view, rather than being poked in
325
if not self._have_output:
326
# As a workaround for <https://launchpad.net/bugs/321935> we only
327
# show transport activity when there's already a progress bar
328
# shown, which time the application code is expected to know to
329
# clear off the progress bar when it's going to send some other
330
# output. Eventually it would be nice to have that automatically
247
333
self._total_byte_count += byte_count
248
334
self._bytes_since_update += byte_count
249
335
now = time.time()
336
if self._total_byte_count < 2000:
337
# a little resistance at first, so it doesn't stay stuck at 0
338
# while connecting...
250
340
if self._transport_update_time is None:
251
341
self._transport_update_time = now
252
342
elif now >= (self._transport_update_time + 0.5):
253
343
# guard against clock stepping backwards, and don't update too
255
345
rate = self._bytes_since_update / (now - self._transport_update_time)
256
scheme = getattr(transport, '_scheme', None) or repr(transport)
257
if direction == 'read':
259
elif direction == 'write':
263
msg = ("%.7s %s %6dKB %5dKB/s" %
264
(scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
346
msg = ("%6dKB %5dKB/s" %
347
(self._total_byte_count>>10, int(rate)>>10,))
265
348
self._transport_update_time = now
266
349
self._last_repaint = now
267
350
self._bytes_since_update = 0
268
351
self._last_transport_msg = msg