~lifeless/ubuntu/lucid/bzr/2.1.2-sru

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-07-21 11:25:12 UTC
  • mfrom: (1.4.1 upstream) (8.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090721112512-dzfg8hfhqddf1dwj
* New upstream release.
 + Fixes compatibility with Python 2.4. Closes: #537708

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""Text UI, write output to the console.
20
20
"""
21
21
 
 
22
import os
22
23
import sys
23
24
import time
24
25
import warnings
56
57
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
57
58
                % "bar_type parameter")
58
59
        # paints progress, network activity, etc
59
 
        self._progress_view = TextProgressView(self.stderr)
60
 
 
 
60
        self._progress_view = self._make_progress_view()
 
61
        
61
62
    def clear_term(self):
62
63
        """Prepare the terminal for output.
63
64
 
69
70
        # to clear it.  We might need to separately check for the case of
70
71
        self._progress_view.clear()
71
72
 
 
73
    def _make_progress_view(self):
 
74
        if os.environ.get('BZR_PROGRESS_BAR') in ('text', None, ''):
 
75
            return TextProgressView(self.stderr)
 
76
        else:
 
77
            return NullProgressView()
 
78
 
72
79
    def note(self, msg):
73
80
        """Write an already-formatted message, clearing the progress bar if necessary."""
74
81
        self.clear_term()
80
87
        This may update a progress bar, spinner, or similar display.
81
88
        By default it does nothing.
82
89
        """
83
 
        self._progress_view._show_transport_activity(transport,
 
90
        self._progress_view.show_transport_activity(transport,
84
91
            direction, byte_count)
85
92
 
86
93
    def _progress_updated(self, task):
98
105
        self._progress_view.clear()
99
106
 
100
107
 
 
108
class NullProgressView(object):
 
109
    """Soak up and ignore progress information."""
 
110
 
 
111
    def clear(self):
 
112
        pass
 
113
 
 
114
    def show_progress(self, task):
 
115
        pass
 
116
 
 
117
    def show_transport_activity(self, transport, direction, byte_count):
 
118
        pass
 
119
    
 
120
 
101
121
class TextProgressView(object):
102
122
    """Display of progress bar and other information on a tty.
103
123
 
118
138
        # true when there's output on the screen we may need to clear
119
139
        self._have_output = False
120
140
        # XXX: We could listen for SIGWINCH and update the terminal width...
 
141
        # https://launchpad.net/bugs/316357
121
142
        self._width = osutils.terminal_width()
122
143
        self._last_transport_msg = ''
123
144
        self._spin_pos = 0
216
237
        self._last_repaint = now
217
238
        self._repaint()
218
239
 
219
 
    def _show_transport_activity(self, transport, direction, byte_count):
 
240
    def show_transport_activity(self, transport, direction, byte_count):
220
241
        """Called by transports via the ui_factory, as they do IO.
221
242
 
222
243
        This may update a progress bar, spinner, or similar display.
225
246
        # XXX: Probably there should be a transport activity model, and that
226
247
        # too should be seen by the progress view, rather than being poked in
227
248
        # here.
 
249
        if not self._have_output:
 
250
            # As a workaround for <https://launchpad.net/bugs/321935> we only
 
251
            # show transport activity when there's already a progress bar
 
252
            # shown, which time the application code is expected to know to
 
253
            # clear off the progress bar when it's going to send some other
 
254
            # output.  Eventually it would be nice to have that automatically
 
255
            # synchronized.
 
256
            return
228
257
        self._total_byte_count += byte_count
229
258
        self._bytes_since_update += byte_count
230
259
        now = time.time()
234
263
            # guard against clock stepping backwards, and don't update too
235
264
            # often
236
265
            rate = self._bytes_since_update / (now - self._transport_update_time)
237
 
            scheme = getattr(transport, '_scheme', None) or repr(transport)
238
 
            if direction == 'read':
239
 
                dir_char = '>'
240
 
            elif direction == 'write':
241
 
                dir_char = '<'
242
 
            else:
243
 
                dir_char = ' '
244
 
            msg = ("%.7s %s %6dKB %5dKB/s" %
245
 
                    (scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
 
266
            msg = ("%6dKB %5dKB/s" %
 
267
                    (self._total_byte_count>>10, int(rate)>>10,))
246
268
            self._transport_update_time = now
247
269
            self._last_repaint = now
248
270
            self._bytes_since_update = 0
249
271
            self._last_transport_msg = msg
250
272
            self._repaint()
251
 
 
252