36
from bzrlib.ui import CLIUIFactory
39
class TextUIFactory(CLIUIFactory):
38
from bzrlib.ui import (
44
class TextUIFactory(UIFactory):
40
45
"""A UI factory for Text user interefaces."""
47
51
"""Create a TextUIFactory.
49
:param bar_type: The type of progress bar to create. It defaults to
50
letting the bzrlib.progress.ProgressBar factory auto
53
:param bar_type: The type of progress bar to create. Deprecated
54
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")
56
super(TextUIFactory, self).__init__()
57
# TODO: there's no good reason not to pass all three streams, maybe we
58
# should deprecate the default values...
58
62
# paints progress, network activity, etc
59
self._progress_view = TextProgressView(self.stderr)
63
self._progress_view = self.make_progress_view()
61
65
def clear_term(self):
62
66
"""Prepare the terminal for output.
69
73
# to clear it. We might need to separately check for the case of
70
74
self._progress_view.clear()
76
def get_boolean(self, prompt):
78
self.prompt(prompt + "? [y/n]: ")
79
line = self.stdin.readline().lower()
80
if line in ('y\n', 'yes\n'):
82
elif line in ('n\n', 'no\n'):
84
elif line in ('', None):
85
# end-of-file; possibly should raise an error here instead
88
def get_non_echoed_password(self):
89
isatty = getattr(self.stdin, 'isatty', None)
90
if isatty is not None and isatty():
91
# getpass() ensure the password is not echoed and other
92
# cross-platform niceties
93
password = getpass.getpass('')
95
# echo doesn't make sense without a terminal
96
password = self.stdin.readline()
99
elif password[-1] == '\n':
100
password = password[:-1]
103
def get_password(self, prompt='', **kwargs):
104
"""Prompt the user for a password.
106
:param prompt: The prompt to present the user
107
:param kwargs: Arguments which will be expanded into the prompt.
108
This lets front ends display different things if
110
:return: The password string, return None if the user
111
canceled the request.
114
self.prompt(prompt, **kwargs)
115
# There's currently no way to say 'i decline to enter a password'
116
# as opposed to 'my password is empty' -- does it matter?
117
return self.get_non_echoed_password()
119
def get_username(self, prompt, **kwargs):
120
"""Prompt the user for a username.
122
:param prompt: The prompt to present the user
123
:param kwargs: Arguments which will be expanded into the prompt.
124
This lets front ends display different things if
126
:return: The username string, return None if the user
127
canceled the request.
130
self.prompt(prompt, **kwargs)
131
username = self.stdin.readline()
134
elif username[-1] == '\n':
135
username = username[:-1]
138
def make_progress_view(self):
139
"""Construct and return a new ProgressView subclass for this UI.
141
# if the user specifically requests either text or no progress bars,
142
# always do that. otherwise, guess based on $TERM and tty presence.
143
if os.environ.get('BZR_PROGRESS_BAR') == 'text':
144
return TextProgressView(self.stderr)
145
elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
146
return NullProgressView()
147
elif progress._supports_progress(self.stderr):
148
return TextProgressView(self.stderr)
150
return NullProgressView()
72
152
def note(self, msg):
73
153
"""Write an already-formatted message, clearing the progress bar if necessary."""
75
155
self.stdout.write(msg + '\n')
157
def prompt(self, prompt, **kwargs):
158
"""Emit prompt on the CLI.
160
:param kwargs: Dictionary of arguments to insert into the prompt,
161
to allow UIs to reformat the prompt.
164
# See <https://launchpad.net/bugs/365891>
165
prompt = prompt % kwargs
166
prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
168
self.stderr.write(prompt)
77
170
def report_transport_activity(self, transport, byte_count, direction):
78
171
"""Called by transports as they do IO.
80
173
This may update a progress bar, spinner, or similar display.
81
174
By default it does nothing.
83
self._progress_view._show_transport_activity(transport,
176
self._progress_view.show_transport_activity(transport,
84
177
direction, byte_count)
86
179
def _progress_updated(self, task):
225
329
# XXX: Probably there should be a transport activity model, and that
226
330
# too should be seen by the progress view, rather than being poked in
332
if not self._have_output:
333
# As a workaround for <https://launchpad.net/bugs/321935> we only
334
# show transport activity when there's already a progress bar
335
# shown, which time the application code is expected to know to
336
# clear off the progress bar when it's going to send some other
337
# output. Eventually it would be nice to have that automatically
228
340
self._total_byte_count += byte_count
229
341
self._bytes_since_update += byte_count
230
342
now = time.time()
343
if self._total_byte_count < 2000:
344
# a little resistance at first, so it doesn't stay stuck at 0
345
# while connecting...
231
347
if self._transport_update_time is None:
232
348
self._transport_update_time = now
233
349
elif now >= (self._transport_update_time + 0.5):
234
350
# guard against clock stepping backwards, and don't update too
236
352
rate = self._bytes_since_update / (now - self._transport_update_time)
237
scheme = getattr(transport, '_scheme', None) or repr(transport)
238
if direction == 'read':
240
elif direction == 'write':
244
msg = ("%.7s %s %6dKB %5dKB/s" %
245
(scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
353
msg = ("%6dKB %5dKB/s" %
354
(self._total_byte_count>>10, int(rate)>>10,))
246
355
self._transport_update_time = now
247
356
self._last_repaint = now
248
357
self._bytes_since_update = 0
249
358
self._last_transport_msg = msg