~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
 
20
20
"""
21
21
 
22
22
import sys
 
23
import time
 
24
import warnings
23
25
 
24
26
from bzrlib.lazy_import import lazy_import
25
27
lazy_import(globals(), """
28
30
from bzrlib import (
29
31
    progress,
30
32
    osutils,
 
33
    symbol_versioning,
31
34
    )
 
35
 
32
36
""")
33
37
 
34
 
from bzrlib.symbol_versioning import (
35
 
    deprecated_method,
36
 
    zero_eight,
37
 
    )
38
38
from bzrlib.ui import CLIUIFactory
39
39
 
40
40
 
43
43
 
44
44
    def __init__(self,
45
45
                 bar_type=None,
 
46
                 stdin=None,
46
47
                 stdout=None,
47
48
                 stderr=None):
48
49
        """Create a TextUIFactory.
49
50
 
50
 
        :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
51
52
                         letting the bzrlib.progress.ProgressBar factory auto
52
 
                         select.
53
 
        """
54
 
        super(TextUIFactory, self).__init__()
55
 
        self._bar_type = bar_type
56
 
        if stdout is None:
57
 
            self.stdout = sys.stdout
58
 
        else:
59
 
            self.stdout = stdout
60
 
        if stderr is None:
61
 
            self.stderr = sys.stderr
62
 
        else:
63
 
            self.stderr = stderr
64
 
 
65
 
    def prompt(self, prompt):
66
 
        """Emit prompt on the CLI."""
67
 
        self.stdout.write(prompt)
68
 
        
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()
75
 
 
76
 
    def nested_progress_bar(self):
77
 
        """Return a nested progress bar.
78
 
        
79
 
        The actual bar type returned depends on the progress module which
80
 
        may return a tty or dots bar depending on the terminal.
81
 
        """
82
 
        if self._progress_bar_stack is None:
83
 
            self._progress_bar_stack = progress.ProgressBarStack(
84
 
                klass=self._bar_type)
85
 
        return self._progress_bar_stack.get_nested()
 
53
                         select.   Deprecated.
 
54
        """
 
55
        super(TextUIFactory, self).__init__(stdin=stdin,
 
56
                stdout=stdout, stderr=stderr)
 
57
        if bar_type:
 
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)
86
62
 
87
63
    def clear_term(self):
88
64
        """Prepare the terminal for output.
89
65
 
90
66
        This will, clear any progress bars, and leave the cursor at the
91
67
        leftmost position."""
92
 
        if self._progress_bar_stack is None:
 
68
        # XXX: If this is preparing to write to stdout, but that's for example
 
69
        # directed into a file rather than to the terminal, and the progress
 
70
        # bar _is_ going to the terminal, we shouldn't need
 
71
        # to clear it.  We might need to separately check for the case of
 
72
        self._progress_view.clear()
 
73
 
 
74
    def note(self, msg):
 
75
        """Write an already-formatted message, clearing the progress bar if necessary."""
 
76
        self.clear_term()
 
77
        self.stdout.write(msg + '\n')
 
78
 
 
79
    def report_transport_activity(self, transport, byte_count, direction):
 
80
        """Called by transports as they do IO.
 
81
 
 
82
        This may update a progress bar, spinner, or similar display.
 
83
        By default it does nothing.
 
84
        """
 
85
        self._progress_view._show_transport_activity(transport,
 
86
            direction, byte_count)
 
87
 
 
88
    def _progress_updated(self, task):
 
89
        """A task has been updated and wants to be displayed.
 
90
        """
 
91
        if not self._task_stack:
 
92
            warnings.warn("%r updated but no tasks are active" %
 
93
                (task,))
 
94
        elif task != self._task_stack[-1]:
 
95
            warnings.warn("%r is not the top progress task %r" %
 
96
                (task, self._task_stack[-1]))
 
97
        self._progress_view.show_progress(task)
 
98
 
 
99
    def _progress_all_finished(self):
 
100
        self._progress_view.clear()
 
101
 
 
102
 
 
103
class TextProgressView(object):
 
104
    """Display of progress bar and other information on a tty.
 
105
 
 
106
    This shows one line of text, including possibly a network indicator, spinner,
 
107
    progress bar, message, etc.
 
108
 
 
109
    One instance of this is created and held by the UI, and fed updates when a
 
110
    task wants to be painted.
 
111
 
 
112
    Transports feed data to this through the ui_factory object.
 
113
 
 
114
    The Progress views can comprise a tree with _parent_task pointers, but
 
115
    this only prints the stack from the nominated current task up to the root.
 
116
    """
 
117
 
 
118
    def __init__(self, term_file):
 
119
        self._term_file = term_file
 
120
        # true when there's output on the screen we may need to clear
 
121
        self._have_output = False
 
122
        # XXX: We could listen for SIGWINCH and update the terminal width...
 
123
        self._width = osutils.terminal_width()
 
124
        self._last_transport_msg = ''
 
125
        self._spin_pos = 0
 
126
        # time we last repainted the screen
 
127
        self._last_repaint = 0
 
128
        # time we last got information about transport activity
 
129
        self._transport_update_time = 0
 
130
        self._last_task = None
 
131
        self._total_byte_count = 0
 
132
        self._bytes_since_update = 0
 
133
 
 
134
    def _show_line(self, s):
 
135
        n = self._width - 1
 
136
        self._term_file.write('\r%-*.*s\r' % (n, n, s))
 
137
 
 
138
    def clear(self):
 
139
        if self._have_output:
 
140
            self._show_line('')
 
141
        self._have_output = False
 
142
 
 
143
    def _render_bar(self):
 
144
        # return a string for the progress bar itself
 
145
        if (self._last_task is None) or self._last_task.show_bar:
 
146
            # If there's no task object, we show space for the bar anyhow.
 
147
            # That's because most invocations of bzr will end showing progress
 
148
            # at some point, though perhaps only after doing some initial IO.
 
149
            # It looks better to draw the progress bar initially rather than
 
150
            # to have what looks like an incomplete progress bar.
 
151
            spin_str =  r'/-\|'[self._spin_pos % 4]
 
152
            self._spin_pos += 1
 
153
            cols = 20
 
154
            if self._last_task is None:
 
155
                completion_fraction = 0
 
156
            else:
 
157
                completion_fraction = \
 
158
                    self._last_task._overall_completion_fraction() or 0
 
159
            markers = int(round(float(cols) * completion_fraction)) - 1
 
160
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
 
161
            return bar_str
 
162
        elif self._last_task.show_spinner:
 
163
            # The last task wanted just a spinner, no bar
 
164
            spin_str =  r'/-\|'[self._spin_pos % 4]
 
165
            self._spin_pos += 1
 
166
            return spin_str + ' '
 
167
        else:
 
168
            return ''
 
169
 
 
170
    def _format_task(self, task):
 
171
        if not task.show_count:
 
172
            s = ''
 
173
        elif task.current_cnt is not None and task.total_cnt is not None:
 
174
            s = ' %d/%d' % (task.current_cnt, task.total_cnt)
 
175
        elif task.current_cnt is not None:
 
176
            s = ' %d' % (task.current_cnt)
 
177
        else:
 
178
            s = ''
 
179
        # compose all the parent messages
 
180
        t = task
 
181
        m = task.msg
 
182
        while t._parent_task:
 
183
            t = t._parent_task
 
184
            if t.msg:
 
185
                m = t.msg + ':' + m
 
186
        return m + s
 
187
 
 
188
    def _render_line(self):
 
189
        bar_string = self._render_bar()
 
190
        if self._last_task:
 
191
            task_msg = self._format_task(self._last_task)
 
192
        else:
 
193
            task_msg = ''
 
194
        trans = self._last_transport_msg
 
195
        if trans:
 
196
            trans += ' | '
 
197
        return (bar_string + trans + task_msg)
 
198
 
 
199
    def _repaint(self):
 
200
        s = self._render_line()
 
201
        self._show_line(s)
 
202
        self._have_output = True
 
203
 
 
204
    def show_progress(self, task):
 
205
        """Called by the task object when it has changed.
 
206
        
 
207
        :param task: The top task object; its parents are also included 
 
208
            by following links.
 
209
        """
 
210
        must_update = task is not self._last_task
 
211
        self._last_task = task
 
212
        now = time.time()
 
213
        if (not must_update) and (now < self._last_repaint + 0.1):
93
214
            return
94
 
        overall_pb = self._progress_bar_stack.bottom()
95
 
        if overall_pb is not None:
96
 
            overall_pb.clear()
 
215
        if now > self._transport_update_time + 10:
 
216
            # no recent activity; expire it
 
217
            self._last_transport_msg = ''
 
218
        self._last_repaint = now
 
219
        self._repaint()
 
220
 
 
221
    def _show_transport_activity(self, transport, direction, byte_count):
 
222
        """Called by transports via the ui_factory, as they do IO.
 
223
 
 
224
        This may update a progress bar, spinner, or similar display.
 
225
        By default it does nothing.
 
226
        """
 
227
        # XXX: Probably there should be a transport activity model, and that
 
228
        # too should be seen by the progress view, rather than being poked in
 
229
        # here.
 
230
        self._total_byte_count += byte_count
 
231
        self._bytes_since_update += byte_count
 
232
        now = time.time()
 
233
        if self._transport_update_time is None:
 
234
            self._transport_update_time = now
 
235
        elif now >= (self._transport_update_time + 0.5):
 
236
            # guard against clock stepping backwards, and don't update too
 
237
            # often
 
238
            rate = self._bytes_since_update / (now - self._transport_update_time)
 
239
            scheme = getattr(transport, '_scheme', None) or repr(transport)
 
240
            if direction == 'read':
 
241
                dir_char = '>'
 
242
            elif direction == 'write':
 
243
                dir_char = '<'
 
244
            else:
 
245
                dir_char = ' '
 
246
            msg = ("%.7s %s %6dKB %5dKB/s" %
 
247
                    (scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
 
248
            self._transport_update_time = now
 
249
            self._last_repaint = now
 
250
            self._bytes_since_update = 0
 
251
            self._last_transport_msg = msg
 
252
            self._repaint()
 
253
 
 
254