~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-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (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)