/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

Merge cleanup into shell-like-tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Text UI, write output to the console.
19
19
"""
20
20
 
 
21
import codecs
21
22
import getpass
22
23
import os
23
24
import sys
146
147
        else:
147
148
            return NullProgressView()
148
149
 
 
150
    def _make_output_stream_explicit(self, encoding, encoding_type):
 
151
        if encoding_type == 'exact':
 
152
            # force sys.stdout to be binary stream on win32; 
 
153
            # NB: this leaves the file set in that mode; may cause problems if
 
154
            # one process tries to do binary and then text output
 
155
            if sys.platform == 'win32':
 
156
                fileno = getattr(self.stdout, 'fileno', None)
 
157
                if fileno:
 
158
                    import msvcrt
 
159
                    msvcrt.setmode(fileno(), os.O_BINARY)
 
160
            return TextUIOutputStream(self, self.stdout)
 
161
        else:
 
162
            encoded_stdout = codecs.getwriter(encoding)(self.stdout,
 
163
                errors=encoding_type)
 
164
            # For whatever reason codecs.getwriter() does not advertise its encoding
 
165
            # it just returns the encoding of the wrapped file, which is completely
 
166
            # bogus. So set the attribute, so we can find the correct encoding later.
 
167
            encoded_stdout.encoding = encoding
 
168
            return TextUIOutputStream(self, encoded_stdout)
 
169
 
149
170
    def note(self, msg):
150
171
        """Write an already-formatted message, clearing the progress bar if necessary."""
151
172
        self.clear_term()
234
255
 
235
256
    def _show_line(self, s):
236
257
        # sys.stderr.write("progress %r\n" % s)
237
 
        n = self._width - 1
238
 
        self._term_file.write('\r%-*.*s\r' % (n, n, s))
 
258
        if self._width is not None:
 
259
            n = self._width - 1
 
260
            s = '%-*.*s' % (n, n, s)
 
261
        self._term_file.write('\r' + s + '\r')
239
262
 
240
263
    def clear(self):
241
264
        if self._have_output:
365
388
            self._bytes_since_update = 0
366
389
            self._last_transport_msg = msg
367
390
            self._repaint()
 
391
 
 
392
 
 
393
class TextUIOutputStream(object):
 
394
    """Decorates an output stream so that the terminal is cleared before writing.
 
395
 
 
396
    This is supposed to ensure that the progress bar does not conflict with bulk
 
397
    text output.
 
398
    """
 
399
    # XXX: this does not handle the case of writing part of a line, then doing
 
400
    # progress bar output: the progress bar will probably write over it.
 
401
    # one option is just to buffer that text until we have a full line;
 
402
    # another is to save and restore it
 
403
 
 
404
    # XXX: might need to wrap more methods
 
405
 
 
406
    def __init__(self, ui_factory, wrapped_stream):
 
407
        self.ui_factory = ui_factory
 
408
        self.wrapped_stream = wrapped_stream
 
409
        # this does no transcoding, but it must expose the underlying encoding
 
410
        # because some callers need to know what can be written - see for
 
411
        # example unescape_for_display.
 
412
        self.encoding = getattr(wrapped_stream, 'encoding', None)
 
413
 
 
414
    def flush(self):
 
415
        self.ui_factory.clear_term()
 
416
        self.wrapped_stream.flush()
 
417
 
 
418
    def write(self, to_write):
 
419
        self.ui_factory.clear_term()
 
420
        self.wrapped_stream.write(to_write)
 
421
 
 
422
    def writelines(self, lines):
 
423
        self.ui_factory.clear_term()
 
424
        self.wrapped_stream.writelines(lines)