37
48
"""Create a TextUIFactory.
39
50
:param bar_type: The type of progress bar to create. It defaults to
40
51
letting the bzrlib.progress.ProgressBar factory auto
43
super(TextUIFactory, self).__init__()
44
self._bar_type = bar_type
46
self.stdout = sys.stdout
50
self.stderr = sys.stderr
54
super(TextUIFactory, self).__init__(stdin=stdin,
55
stdout=stdout, stderr=stderr)
57
symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
58
% "bar_type parameter")
59
# paints progress, network activity, etc
60
self._progress_view = TextProgressView(self.stderr)
54
62
def prompt(self, prompt):
55
63
"""Emit prompt on the CLI."""
56
self.stdout.write(prompt + "? [y/n]:")
58
@deprecated_method(zero_eight)
59
def progress_bar(self):
60
"""See UIFactory.nested_progress_bar()."""
61
# this in turn is abstract, and creates either a tty or dots
62
# bar depending on what we think of the terminal
63
return bzrlib.progress.ProgressBar()
65
def get_password(self, prompt='', **kwargs):
66
"""Prompt the user for a password.
68
:param prompt: The prompt to present the user
69
:param kwargs: Arguments which will be expanded into the prompt.
70
This lets front ends display different things if
72
:return: The password string, return None if the user
75
prompt = (prompt % kwargs).encode(sys.stdout.encoding, 'replace')
78
return getpass.getpass(prompt)
79
except KeyboardInterrupt:
82
def nested_progress_bar(self):
83
"""Return a nested progress bar.
85
The actual bar type returned depends on the progress module which
86
may return a tty or dots bar depending on the terminal.
88
if self._progress_bar_stack is None:
89
self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
91
return self._progress_bar_stack.get_nested()
64
self.stdout.write(prompt)
93
66
def clear_term(self):
94
67
"""Prepare the terminal for output.
96
69
This will, clear any progress bars, and leave the cursor at the
97
70
leftmost position."""
98
if self._progress_bar_stack is None:
71
# XXX: If this is preparing to write to stdout, but that's for example
72
# directed into a file rather than to the terminal, and the progress
73
# bar _is_ going to the terminal, we shouldn't need
74
# to clear it. We might need to separately check for the case of
75
self._progress_view.clear()
78
"""Write an already-formatted message, clearing the progress bar if necessary."""
80
self.stdout.write(msg + '\n')
82
def report_transport_activity(self, transport, byte_count, direction):
83
"""Called by transports as they do IO.
85
This may update a progress bar, spinner, or similar display.
86
By default it does nothing.
88
self._progress_view.show_transport_activity(byte_count)
90
def show_progress(self, task):
91
"""A task has been updated and wants to be displayed.
93
self._progress_view.show_progress(task)
95
def progress_finished(self, task):
96
CLIUIFactory.progress_finished(self, task)
97
if not self._task_stack:
98
# finished top-level task
99
self._progress_view.clear()
102
class TextProgressView(object):
103
"""Display of progress bar and other information on a tty.
105
This shows one line of text, including possibly a network indicator, spinner,
106
progress bar, message, etc.
108
One instance of this is created and held by the UI, and fed updates when a
109
task wants to be painted.
111
Transports feed data to this through the ui_factory object.
114
def __init__(self, term_file):
115
self._term_file = term_file
116
# true when there's output on the screen we may need to clear
117
self._have_output = False
118
# XXX: We could listen for SIGWINCH and update the terminal width...
119
self._width = osutils.terminal_width()
120
self._last_transport_msg = ''
122
# time we last repainted the screen
123
self._last_repaint = 0
124
# time we last got information about transport activity
125
self._transport_update_time = 0
126
self._task_fraction = None
127
self._last_task = None
128
self._total_byte_count = 0
129
self._bytes_since_update = 0
131
def _show_line(self, s):
133
self._term_file.write('\r%-*.*s\r' % (n, n, s))
136
if self._have_output:
138
self._have_output = False
140
def _render_bar(self):
141
# return a string for the progress bar itself
142
if (self._last_task is not None) and self._last_task.show_bar:
143
spin_str = r'/-\|'[self._spin_pos % 4]
145
f = self._task_fraction or 0
147
# number of markers highlighted in bar
148
markers = int(round(float(cols) * f)) - 1
149
bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
151
elif (self._last_task is None) or self._last_task.show_spinner:
152
spin_str = r'/-\|'[self._spin_pos % 4]
154
return spin_str + ' '
158
def _format_task(self, task):
159
if not task.show_count:
161
elif task.total_cnt is not None:
162
s = ' %d/%d' % (task.current_cnt, task.total_cnt)
163
elif task.current_cnt is not None:
164
s = ' %d' % (task.current_cnt)
167
self._task_fraction = task._overall_completion_fraction()
168
# compose all the parent messages
171
while t._parent_task:
178
bar_string = self._render_bar()
180
task_msg = self._format_task(self._last_task)
183
trans = self._last_transport_msg
184
if trans and task_msg:
191
self._have_output = True
193
def show_progress(self, task):
194
self._last_task = task
196
if now < self._last_repaint + 0.1:
100
overall_pb = self._progress_bar_stack.bottom()
101
if overall_pb is not None:
198
if now > self._transport_update_time + 5:
199
# no recent activity; expire it
200
self._last_transport_msg = ''
201
self._last_repaint = now
204
def show_transport_activity(self, byte_count):
205
"""Called by transports as they do IO.
207
This may update a progress bar, spinner, or similar display.
208
By default it does nothing.
210
# XXX: Probably there should be a transport activity model, and that
211
# too should be seen by the progress view, rather than being poked in
213
self._total_byte_count += byte_count
214
self._bytes_since_update += byte_count
216
if self._transport_update_time is None:
217
self._transport_update_time = now
218
elif now >= (self._transport_update_time + 0.2):
219
# guard against clock stepping backwards, and don't update too
221
rate = self._bytes_since_update / (now - self._transport_update_time)
222
msg = ("%6dkB @ %4dkB/s" %
223
(self._total_byte_count>>10, int(rate)>>10,))
224
self._transport_update_time = now
225
self._last_repaint = now
226
self._bytes_since_update = 0
227
self._last_transport_msg = msg