~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:13:13 UTC
  • mfrom: (6614.2.2 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201191313-wdfvmfff1djde6oq
(vila) Release 2.7.0 (Vincent Ladeuil)

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
from __future__ import absolute_import
 
22
 
21
23
import os
22
24
import sys
23
25
import time
29
31
import warnings
30
32
 
31
33
from bzrlib import (
 
34
    config,
32
35
    debug,
33
36
    progress,
34
37
    osutils,
153
156
            return index
154
157
 
155
158
 
 
159
opt_progress_bar = config.Option(
 
160
    'progress_bar', help='Progress bar type.',
 
161
    default_from_env=['BZR_PROGRESS_BAR'], default=None,
 
162
    invalid='error')
 
163
 
 
164
 
156
165
class TextUIFactory(UIFactory):
157
 
    """A UI factory for Text user interefaces."""
 
166
    """A UI factory for Text user interfaces."""
158
167
 
159
168
    def __init__(self,
160
169
                 stdin=None,
229
238
            password = self.stdin.readline()
230
239
            if not password:
231
240
                password = None
232
 
            elif password[-1] == '\n':
233
 
                password = password[:-1]
 
241
            else:
 
242
                password = password.decode(self.stdin.encoding)
 
243
 
 
244
                if password[-1] == '\n':
 
245
                    password = password[:-1]
234
246
        return password
235
247
 
236
248
    def get_password(self, prompt=u'', **kwargs):
264
276
        username = self.stdin.readline()
265
277
        if not username:
266
278
            username = None
267
 
        elif username[-1] == '\n':
268
 
            username = username[:-1]
 
279
        else:
 
280
            username = username.decode(self.stdin.encoding)
 
281
            if username[-1] == '\n':
 
282
                username = username[:-1]
269
283
        return username
270
284
 
271
285
    def make_progress_view(self):
277
291
        # do that.  otherwise, guess based on $TERM and tty presence.
278
292
        if self.is_quiet():
279
293
            return NullProgressView()
280
 
        elif os.environ.get('BZR_PROGRESS_BAR') == 'text':
281
 
            return TextProgressView(self.stderr)
282
 
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
283
 
            return NullProgressView()
284
 
        elif progress._supports_progress(self.stderr):
285
 
            return TextProgressView(self.stderr)
286
 
        else:
287
 
            return NullProgressView()
 
294
        pb_type = config.GlobalStack().get('progress_bar')
 
295
        if pb_type == 'none': # Explicit requirement
 
296
            return NullProgressView()
 
297
        if (pb_type == 'text' # Explicit requirement
 
298
            or progress._supports_progress(self.stderr)): # Guess
 
299
            return TextProgressView(self.stderr)
 
300
        # No explicit requirement and no successful guess
 
301
        return NullProgressView()
288
302
 
289
303
    def _make_output_stream_explicit(self, encoding, encoding_type):
290
304
        if encoding_type == 'exact':
322
336
        if kwargs:
323
337
            # See <https://launchpad.net/bugs/365891>
324
338
            prompt = prompt % kwargs
325
 
        prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
 
339
        try:
 
340
            prompt = prompt.encode(self.stderr.encoding)
 
341
        except (UnicodeError, AttributeError):
 
342
            # If stderr has no encoding attribute or can't properly encode,
 
343
            # fallback to terminal encoding for robustness (better display
 
344
            # something to the user than aborting with a traceback).
 
345
            prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
326
346
        self.clear_term()
327
347
        self.stdout.flush()
328
348
        self.stderr.write(prompt)
402
422
    this only prints the stack from the nominated current task up to the root.
403
423
    """
404
424
 
405
 
    def __init__(self, term_file):
 
425
    def __init__(self, term_file, encoding=None, errors="replace"):
406
426
        self._term_file = term_file
 
427
        if encoding is None:
 
428
            self._encoding = getattr(term_file, "encoding", None) or "ascii"
 
429
        else:
 
430
            self._encoding = encoding
 
431
        self._encoding_errors = errors
407
432
        # true when there's output on the screen we may need to clear
408
433
        self._have_output = False
409
434
        self._last_transport_msg = ''
430
455
        else:
431
456
            return w - 1
432
457
 
433
 
    def _show_line(self, s):
434
 
        # sys.stderr.write("progress %r\n" % s)
 
458
    def _show_line(self, u):
 
459
        s = u.encode(self._encoding, self._encoding_errors)
435
460
        width = self._avail_width()
436
461
        if width is not None:
 
462
            # GZ 2012-03-28: Counting bytes is wrong for calculating width of
 
463
            #                text but better than counting codepoints.
437
464
            s = '%-*.*s' % (width, width, s)
438
465
        self._term_file.write('\r' + s + '\r')
439
466