48
47
"""Create a TextUIFactory.
50
:param bar_type: The type of progress bar to create. It defaults to
49
:param bar_type: The type of progress bar to create. It defaults to
51
50
letting the bzrlib.progress.ProgressBar factory auto
54
super(TextUIFactory, self).__init__()
55
self._bar_type = bar_type
57
self.stdout = sys.stdout
61
self.stderr = sys.stderr
65
def prompt(self, prompt):
66
"""Emit prompt on the CLI."""
67
self.stdout.write(prompt)
69
@deprecated_method(zero_eight)
70
def progress_bar(self):
71
"""See UIFactory.nested_progress_bar()."""
72
# this in turn is abstract, and creates either a tty or dots
73
# bar depending on what we think of the terminal
74
return progress.ProgressBar()
76
def nested_progress_bar(self):
77
"""Return a nested progress bar.
79
The actual bar type returned depends on the progress module which
80
may return a tty or dots bar depending on the terminal.
82
if self._progress_bar_stack is None:
83
self._progress_bar_stack = progress.ProgressBarStack(
85
return self._progress_bar_stack.get_nested()
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")
58
# paints progress, network activity, etc
59
self._progress_view = TextProgressView(self.stderr)
87
61
def clear_term(self):
88
62
"""Prepare the terminal for output.
90
64
This will, clear any progress bars, and leave the cursor at the
91
65
leftmost position."""
92
if self._progress_bar_stack is None:
66
# XXX: If this is preparing to write to stdout, but that's for example
67
# directed into a file rather than to the terminal, and the progress
68
# bar _is_ going to the terminal, we shouldn't need
69
# to clear it. We might need to separately check for the case of
70
self._progress_view.clear()
73
"""Write an already-formatted message, clearing the progress bar if necessary."""
75
self.stdout.write(msg + '\n')
77
def report_transport_activity(self, transport, byte_count, direction):
78
"""Called by transports as they do IO.
80
This may update a progress bar, spinner, or similar display.
81
By default it does nothing.
83
self._progress_view._show_transport_activity(transport,
84
direction, byte_count)
86
def _progress_updated(self, task):
87
"""A task has been updated and wants to be displayed.
89
if not self._task_stack:
90
warnings.warn("%r updated but no tasks are active" %
92
elif task != self._task_stack[-1]:
93
warnings.warn("%r is not the top progress task %r" %
94
(task, self._task_stack[-1]))
95
self._progress_view.show_progress(task)
97
def _progress_all_finished(self):
98
self._progress_view.clear()
101
class TextProgressView(object):
102
"""Display of progress bar and other information on a tty.
104
This shows one line of text, including possibly a network indicator, spinner,
105
progress bar, message, etc.
107
One instance of this is created and held by the UI, and fed updates when a
108
task wants to be painted.
110
Transports feed data to this through the ui_factory object.
112
The Progress views can comprise a tree with _parent_task pointers, but
113
this only prints the stack from the nominated current task up to the root.
116
def __init__(self, term_file):
117
self._term_file = term_file
118
# true when there's output on the screen we may need to clear
119
self._have_output = False
120
# XXX: We could listen for SIGWINCH and update the terminal width...
121
self._width = osutils.terminal_width()
122
self._last_transport_msg = ''
124
# time we last repainted the screen
125
self._last_repaint = 0
126
# time we last got information about transport activity
127
self._transport_update_time = 0
128
self._last_task = None
129
self._total_byte_count = 0
130
self._bytes_since_update = 0
132
def _show_line(self, s):
134
self._term_file.write('\r%-*.*s\r' % (n, n, s))
137
if self._have_output:
139
self._have_output = False
141
def _render_bar(self):
142
# return a string for the progress bar itself
143
if (self._last_task is None) or self._last_task.show_bar:
144
# If there's no task object, we show space for the bar anyhow.
145
# That's because most invocations of bzr will end showing progress
146
# at some point, though perhaps only after doing some initial IO.
147
# It looks better to draw the progress bar initially rather than
148
# to have what looks like an incomplete progress bar.
149
spin_str = r'/-\|'[self._spin_pos % 4]
152
if self._last_task is None:
153
completion_fraction = 0
155
completion_fraction = \
156
self._last_task._overall_completion_fraction() or 0
157
markers = int(round(float(cols) * completion_fraction)) - 1
158
bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
160
elif self._last_task.show_spinner:
161
# The last task wanted just a spinner, no bar
162
spin_str = r'/-\|'[self._spin_pos % 4]
164
return spin_str + ' '
168
def _format_task(self, task):
169
if not task.show_count:
171
elif task.current_cnt is not None and task.total_cnt is not None:
172
s = ' %d/%d' % (task.current_cnt, task.total_cnt)
173
elif task.current_cnt is not None:
174
s = ' %d' % (task.current_cnt)
177
# compose all the parent messages
180
while t._parent_task:
186
def _render_line(self):
187
bar_string = self._render_bar()
189
task_msg = self._format_task(self._last_task)
192
trans = self._last_transport_msg
195
return (bar_string + trans + task_msg)
198
s = self._render_line()
200
self._have_output = True
202
def show_progress(self, task):
203
"""Called by the task object when it has changed.
205
:param task: The top task object; its parents are also included
208
must_update = task is not self._last_task
209
self._last_task = task
211
if (not must_update) and (now < self._last_repaint + 0.1):
94
overall_pb = self._progress_bar_stack.bottom()
95
if overall_pb is not None:
213
if now > self._transport_update_time + 10:
214
# no recent activity; expire it
215
self._last_transport_msg = ''
216
self._last_repaint = now
219
def _show_transport_activity(self, transport, direction, byte_count):
220
"""Called by transports via the ui_factory, as they do IO.
222
This may update a progress bar, spinner, or similar display.
223
By default it does nothing.
225
# XXX: Probably there should be a transport activity model, and that
226
# too should be seen by the progress view, rather than being poked in
228
self._total_byte_count += byte_count
229
self._bytes_since_update += byte_count
231
if self._transport_update_time is None:
232
self._transport_update_time = now
233
elif now >= (self._transport_update_time + 0.5):
234
# guard against clock stepping backwards, and don't update too
236
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,))
246
self._transport_update_time = now
247
self._last_repaint = now
248
self._bytes_since_update = 0
249
self._last_transport_msg = msg