1
# Copyright (C) 2005, 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Text UI, write output to the console.
25
from bzrlib.lazy_import import lazy_import
26
lazy_import(globals(), """
37
from bzrlib.ui import CLIUIFactory
40
class TextUIFactory(CLIUIFactory):
41
"""A UI factory for Text user interefaces."""
48
"""Create a TextUIFactory.
50
:param bar_type: The type of progress bar to create. It defaults to
51
letting the bzrlib.progress.ProgressBar factory auto
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)
62
def prompt(self, prompt):
63
"""Emit prompt on the CLI."""
64
self.stdout.write(prompt)
67
"""Prepare the terminal for output.
69
This will, clear any progress bars, and leave the cursor at the
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:
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