44
49
"""Create a TextUIFactory.
46
:param bar_type: The type of progress bar to create. It defaults to
51
:param bar_type: The type of progress bar to create. It defaults to
47
52
letting the bzrlib.progress.ProgressBar factory auto
50
super(TextUIFactory, self).__init__()
51
self._bar_type = bar_type
53
self.stdout = sys.stdout
57
self.stderr = sys.stderr
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")
60
# paints progress, network activity, etc
61
self._progress_view = TextProgressView(self.stderr)
61
63
def prompt(self, prompt):
62
64
"""Emit prompt on the CLI."""
63
65
self.stdout.write(prompt)
65
def nested_progress_bar(self):
66
"""Return a nested progress bar.
68
The actual bar type returned depends on the progress module which
69
may return a tty or dots bar depending on the terminal.
71
if self._progress_bar_stack is None:
72
self._progress_bar_stack = progress.ProgressBarStack(
74
return self._progress_bar_stack.get_nested()
76
67
def clear_term(self):
77
68
"""Prepare the terminal for output.
79
70
This will, clear any progress bars, and leave the cursor at the
80
71
leftmost position."""
81
if self._progress_bar_stack is None:
72
# XXX: If this is preparing to write to stdout, but that's for example
73
# directed into a file rather than to the terminal, and the progress
74
# bar _is_ going to the terminal, we shouldn't need
75
# to clear it. We might need to separately check for the case of
76
self._progress_view.clear()
79
"""Write an already-formatted message, clearing the progress bar if necessary."""
81
self.stdout.write(msg + '\n')
83
def report_transport_activity(self, transport, byte_count, direction):
84
"""Called by transports as they do IO.
86
This may update a progress bar, spinner, or similar display.
87
By default it does nothing.
89
self._progress_view._show_transport_activity(transport,
90
direction, byte_count)
92
def _progress_updated(self, task):
93
"""A task has been updated and wants to be displayed.
95
if not self._task_stack:
96
warnings.warn("%r updated but no tasks are active" %
98
elif task != self._task_stack[-1]:
99
warnings.warn("%r is not the top progress task %r" %
100
(task, self._task_stack[-1]))
101
self._progress_view.show_progress(task)
103
def _progress_all_finished(self):
104
self._progress_view.clear()
107
class TextProgressView(object):
108
"""Display of progress bar and other information on a tty.
110
This shows one line of text, including possibly a network indicator, spinner,
111
progress bar, message, etc.
113
One instance of this is created and held by the UI, and fed updates when a
114
task wants to be painted.
116
Transports feed data to this through the ui_factory object.
118
The Progress views can comprise a tree with _parent_task pointers, but
119
this only prints the stack from the nominated current task up to the root.
122
def __init__(self, term_file):
123
self._term_file = term_file
124
# true when there's output on the screen we may need to clear
125
self._have_output = False
126
# XXX: We could listen for SIGWINCH and update the terminal width...
127
self._width = osutils.terminal_width()
128
self._last_transport_msg = ''
130
# time we last repainted the screen
131
self._last_repaint = 0
132
# time we last got information about transport activity
133
self._transport_update_time = 0
134
self._last_task = None
135
self._total_byte_count = 0
136
self._bytes_since_update = 0
138
def _show_line(self, s):
140
self._term_file.write('\r%-*.*s\r' % (n, n, s))
143
if self._have_output:
145
self._have_output = False
147
def _render_bar(self):
148
# return a string for the progress bar itself
149
if (self._last_task is None) or self._last_task.show_bar:
150
# If there's no task object, we show space for the bar anyhow.
151
# That's because most invocations of bzr will end showing progress
152
# at some point, though perhaps only after doing some initial IO.
153
# It looks better to draw the progress bar initially rather than
154
# to have what looks like an incomplete progress bar.
155
spin_str = r'/-\|'[self._spin_pos % 4]
158
if self._last_task is None:
159
completion_fraction = 0
161
completion_fraction = \
162
self._last_task._overall_completion_fraction() or 0
163
markers = int(round(float(cols) * completion_fraction)) - 1
164
bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
166
elif self._last_task.show_spinner:
167
# The last task wanted just a spinner, no bar
168
spin_str = r'/-\|'[self._spin_pos % 4]
170
return spin_str + ' '
174
def _format_task(self, task):
175
if not task.show_count:
177
elif task.current_cnt is not None and task.total_cnt is not None:
178
s = ' %d/%d' % (task.current_cnt, task.total_cnt)
179
elif task.current_cnt is not None:
180
s = ' %d' % (task.current_cnt)
183
# compose all the parent messages
186
while t._parent_task:
192
def _render_line(self):
193
bar_string = self._render_bar()
195
task_msg = self._format_task(self._last_task)
198
trans = self._last_transport_msg
201
return (bar_string + trans + task_msg)
204
s = self._render_line()
206
self._have_output = True
208
def show_progress(self, task):
209
"""Called by the task object when it has changed.
211
:param task: The top task object; its parents are also included
214
must_update = task is not self._last_task
215
self._last_task = task
217
if (not must_update) and (now < self._last_repaint + 0.1):
83
overall_pb = self._progress_bar_stack.bottom()
84
if overall_pb is not None:
219
if now > self._transport_update_time + 10:
220
# no recent activity; expire it
221
self._last_transport_msg = ''
222
self._last_repaint = now
225
def _show_transport_activity(self, transport, direction, byte_count):
226
"""Called by transports via the ui_factory, as they do IO.
228
This may update a progress bar, spinner, or similar display.
229
By default it does nothing.
231
# XXX: Probably there should be a transport activity model, and that
232
# too should be seen by the progress view, rather than being poked in
234
self._total_byte_count += byte_count
235
self._bytes_since_update += byte_count
237
if self._transport_update_time is None:
238
self._transport_update_time = now
239
elif now >= (self._transport_update_time + 0.5):
240
# guard against clock stepping backwards, and don't update too
242
rate = self._bytes_since_update / (now - self._transport_update_time)
243
scheme = getattr(transport, '_scheme', None) or repr(transport)
244
if direction == 'read':
246
elif direction == 'write':
250
msg = ("%.7s %s %6dKB %5dKB/s" %
251
(scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
252
self._transport_update_time = now
253
self._last_repaint = now
254
self._bytes_since_update = 0
255
self._last_transport_msg = msg