~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-01-15 09:21:49 UTC
  • mfrom: (6606.2.1 autodoc-unicode)
  • Revision ID: pqm@pqm.ubuntu.com-20160115092149-z5f4sfq3jvaz0enb
(vila) Fix autodoc runner when LANG=C. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import warnings
32
32
 
33
33
from bzrlib import (
 
34
    config,
34
35
    debug,
35
36
    progress,
36
37
    osutils,
155
156
            return index
156
157
 
157
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
 
158
165
class TextUIFactory(UIFactory):
159
 
    """A UI factory for Text user interefaces."""
 
166
    """A UI factory for Text user interfaces."""
160
167
 
161
168
    def __init__(self,
162
169
                 stdin=None,
231
238
            password = self.stdin.readline()
232
239
            if not password:
233
240
                password = None
234
 
            elif password[-1] == '\n':
235
 
                password = password[:-1]
 
241
            else:
 
242
                password = password.decode(self.stdin.encoding)
 
243
 
 
244
                if password[-1] == '\n':
 
245
                    password = password[:-1]
236
246
        return password
237
247
 
238
248
    def get_password(self, prompt=u'', **kwargs):
266
276
        username = self.stdin.readline()
267
277
        if not username:
268
278
            username = None
269
 
        elif username[-1] == '\n':
270
 
            username = username[:-1]
 
279
        else:
 
280
            username = username.decode(self.stdin.encoding)
 
281
            if username[-1] == '\n':
 
282
                username = username[:-1]
271
283
        return username
272
284
 
273
285
    def make_progress_view(self):
279
291
        # do that.  otherwise, guess based on $TERM and tty presence.
280
292
        if self.is_quiet():
281
293
            return NullProgressView()
282
 
        elif os.environ.get('BZR_PROGRESS_BAR') == 'text':
283
 
            return TextProgressView(self.stderr)
284
 
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
285
 
            return NullProgressView()
286
 
        elif progress._supports_progress(self.stderr):
287
 
            return TextProgressView(self.stderr)
288
 
        else:
289
 
            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()
290
302
 
291
303
    def _make_output_stream_explicit(self, encoding, encoding_type):
292
304
        if encoding_type == 'exact':
324
336
        if kwargs:
325
337
            # See <https://launchpad.net/bugs/365891>
326
338
            prompt = prompt % kwargs
327
 
        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')
328
346
        self.clear_term()
329
347
        self.stdout.flush()
330
348
        self.stderr.write(prompt)
404
422
    this only prints the stack from the nominated current task up to the root.
405
423
    """
406
424
 
407
 
    def __init__(self, term_file):
 
425
    def __init__(self, term_file, encoding=None, errors="replace"):
408
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
409
432
        # true when there's output on the screen we may need to clear
410
433
        self._have_output = False
411
434
        self._last_transport_msg = ''
432
455
        else:
433
456
            return w - 1
434
457
 
435
 
    def _show_line(self, s):
436
 
        # sys.stderr.write("progress %r\n" % s)
 
458
    def _show_line(self, u):
 
459
        s = u.encode(self._encoding, self._encoding_errors)
437
460
        width = self._avail_width()
438
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.
439
464
            s = '%-*.*s' % (width, width, s)
440
465
        self._term_file.write('\r' + s + '\r')
441
466