38
from bzrlib.ui import CLIUIFactory
41
class TextUIFactory(CLIUIFactory):
37
from bzrlib.ui import (
43
class TextUIFactory(UIFactory):
42
44
"""A UI factory for Text user interefaces."""
49
50
"""Create a TextUIFactory.
51
:param bar_type: The type of progress bar to create. It defaults to
52
:param bar_type: The type of progress bar to create. It defaults to
52
53
letting the bzrlib.progress.ProgressBar factory auto
53
54
select. Deprecated.
55
super(TextUIFactory, self).__init__(stdin=stdin,
56
stdout=stdout, stderr=stderr)
58
symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
59
% "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...
60
62
# paints progress, network activity, etc
61
self._progress_view = TextProgressView(self.stderr)
63
def prompt(self, prompt):
64
"""Emit prompt on the CLI."""
65
self.stdout.write(prompt)
63
self._progress_view = self.make_progress_view()
67
65
def clear_term(self):
68
66
"""Prepare the terminal for output.
72
70
# XXX: If this is preparing to write to stdout, but that's for example
73
71
# directed into a file rather than to the terminal, and the progress
74
72
# bar _is_ going to the terminal, we shouldn't need
75
# to clear it. We might need to separately check for the case of
73
# to clear it. We might need to separately check for the case of
76
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()
78
152
def note(self, msg):
79
153
"""Write an already-formatted message, clearing the progress bar if necessary."""
81
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)
83
170
def report_transport_activity(self, transport, byte_count, direction):
84
171
"""Called by transports as they do IO.
86
173
This may update a progress bar, spinner, or similar display.
87
174
By default it does nothing.
89
self._progress_view.show_transport_activity(byte_count)
176
self._progress_view.show_transport_activity(transport,
177
direction, byte_count)
91
179
def _progress_updated(self, task):
92
180
"""A task has been updated and wants to be displayed.
94
if task != self._task_stack[-1]:
182
if not self._task_stack:
183
warnings.warn("%r updated but no tasks are active" %
185
elif task != self._task_stack[-1]:
95
186
warnings.warn("%r is not the top progress task %r" %
96
187
(task, self._task_stack[-1]))
97
188
self._progress_view.show_progress(task)
144
235
def _render_bar(self):
145
236
# return a string for the progress bar itself
146
if (self._last_task is not None) and self._last_task.show_bar:
237
if (self._last_task is None) or self._last_task.show_bar:
238
# If there's no task object, we show space for the bar anyhow.
239
# That's because most invocations of bzr will end showing progress
240
# at some point, though perhaps only after doing some initial IO.
241
# It looks better to draw the progress bar initially rather than
242
# to have what looks like an incomplete progress bar.
147
243
spin_str = r'/-\|'[self._spin_pos % 4]
148
244
self._spin_pos += 1
149
f = self._task_fraction or 0
151
# number of markers highlighted in bar
152
markers = int(round(float(cols) * f)) - 1
246
if self._last_task is None:
247
completion_fraction = 0
249
completion_fraction = \
250
self._last_task._overall_completion_fraction() or 0
251
markers = int(round(float(cols) * completion_fraction)) - 1
153
252
bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
155
elif (self._last_task is None) or self._last_task.show_spinner:
254
elif self._last_task.show_spinner:
255
# The last task wanted just a spinner, no bar
156
256
spin_str = r'/-\|'[self._spin_pos % 4]
157
257
self._spin_pos += 1
158
258
return spin_str + ' '
178
277
m = t.msg + ':' + m
280
def _render_line(self):
182
281
bar_string = self._render_bar()
183
282
if self._last_task:
184
283
task_msg = self._format_task(self._last_task)
187
286
trans = self._last_transport_msg
188
if trans and task_msg:
289
return (bar_string + trans + task_msg)
292
s = self._render_line()
194
293
self._show_line(s)
195
294
self._have_output = True
197
296
def show_progress(self, task):
297
"""Called by the task object when it has changed.
299
:param task: The top task object; its parents are also included
302
must_update = task is not self._last_task
198
303
self._last_task = task
199
304
now = time.time()
200
if now < self._last_repaint + 0.1:
305
if (not must_update) and (now < self._last_repaint + 0.1):
202
if now > self._transport_update_time + 5:
307
if now > self._transport_update_time + 10:
203
308
# no recent activity; expire it
204
309
self._last_transport_msg = ''
205
310
self._last_repaint = now
208
def show_transport_activity(self, byte_count):
209
"""Called by transports as they do IO.
313
def show_transport_activity(self, transport, direction, byte_count):
314
"""Called by transports via the ui_factory, as they do IO.
211
316
This may update a progress bar, spinner, or similar display.
212
317
By default it does nothing.
214
319
# XXX: Probably there should be a transport activity model, and that
215
320
# too should be seen by the progress view, rather than being poked in
322
if not self._have_output:
323
# As a workaround for <https://launchpad.net/bugs/321935> we only
324
# show transport activity when there's already a progress bar
325
# shown, which time the application code is expected to know to
326
# clear off the progress bar when it's going to send some other
327
# output. Eventually it would be nice to have that automatically
217
330
self._total_byte_count += byte_count
218
331
self._bytes_since_update += byte_count
219
332
now = time.time()
220
333
if self._transport_update_time is None:
221
334
self._transport_update_time = now
222
elif now >= (self._transport_update_time + 0.2):
335
elif now >= (self._transport_update_time + 0.5):
223
336
# guard against clock stepping backwards, and don't update too
225
338
rate = self._bytes_since_update / (now - self._transport_update_time)
226
msg = ("%6dkB @ %4dkB/s" %
227
(self._total_byte_count>>10, int(rate)>>10,))
339
msg = ("%6dKB %5dKB/s" %
340
(self._total_byte_count>>10, int(rate)>>10,))
228
341
self._transport_update_time = now
229
342
self._last_repaint = now
230
343
self._bytes_since_update = 0
231
344
self._last_transport_msg = msg