~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
"""Text UI, write output to the console.
19
19
"""
20
20
 
21
 
from __future__ import absolute_import
22
 
 
 
21
import codecs
 
22
import getpass
23
23
import os
24
24
import sys
25
25
import time
 
26
import warnings
26
27
 
27
28
from bzrlib.lazy_import import lazy_import
28
29
lazy_import(globals(), """
29
 
import codecs
30
 
import getpass
31
 
import warnings
32
 
 
33
30
from bzrlib import (
34
 
    config,
35
31
    debug,
36
32
    progress,
37
33
    osutils,
 
34
    symbol_versioning,
38
35
    trace,
39
36
    )
40
37
 
46
43
    )
47
44
 
48
45
 
49
 
class _ChooseUI(object):
50
 
 
51
 
    """ Helper class for choose implementation.
52
 
    """
53
 
 
54
 
    def __init__(self, ui, msg, choices, default):
55
 
        self.ui = ui
56
 
        self._setup_mode()
57
 
        self._build_alternatives(msg, choices, default)
58
 
 
59
 
    def _setup_mode(self):
60
 
        """Setup input mode (line-based, char-based) and echo-back.
61
 
 
62
 
        Line-based input is used if the BZR_TEXTUI_INPUT environment
63
 
        variable is set to 'line-based', or if there is no controlling
64
 
        terminal.
65
 
        """
66
 
        if os.environ.get('BZR_TEXTUI_INPUT') != 'line-based' and \
67
 
           self.ui.stdin == sys.stdin and self.ui.stdin.isatty():
68
 
            self.line_based = False
69
 
            self.echo_back = True
70
 
        else:
71
 
            self.line_based = True
72
 
            self.echo_back = not self.ui.stdin.isatty()
73
 
 
74
 
    def _build_alternatives(self, msg, choices, default):
75
 
        """Parse choices string.
76
 
 
77
 
        Setup final prompt and the lists of choices and associated
78
 
        shortcuts.
79
 
        """
80
 
        index = 0
81
 
        help_list = []
82
 
        self.alternatives = {}
83
 
        choices = choices.split('\n')
84
 
        if default is not None and default not in range(0, len(choices)):
85
 
            raise ValueError("invalid default index")
86
 
        for c in choices:
87
 
            name = c.replace('&', '').lower()
88
 
            choice = (name, index)
89
 
            if name in self.alternatives:
90
 
                raise ValueError("duplicated choice: %s" % name)
91
 
            self.alternatives[name] = choice
92
 
            shortcut = c.find('&')
93
 
            if -1 != shortcut and (shortcut + 1) < len(c):
94
 
                help = c[:shortcut]
95
 
                help += '[' + c[shortcut + 1] + ']'
96
 
                help += c[(shortcut + 2):]
97
 
                shortcut = c[shortcut + 1]
98
 
            else:
99
 
                c = c.replace('&', '')
100
 
                shortcut = c[0]
101
 
                help = '[%s]%s' % (shortcut, c[1:])
102
 
            shortcut = shortcut.lower()
103
 
            if shortcut in self.alternatives:
104
 
                raise ValueError("duplicated shortcut: %s" % shortcut)
105
 
            self.alternatives[shortcut] = choice
106
 
            # Add redirections for default.
107
 
            if index == default:
108
 
                self.alternatives[''] = choice
109
 
                self.alternatives['\r'] = choice
110
 
            help_list.append(help)
111
 
            index += 1
112
 
 
113
 
        self.prompt = u'%s (%s): ' % (msg, ', '.join(help_list))
114
 
 
115
 
    def _getline(self):
116
 
        line = self.ui.stdin.readline()
117
 
        if '' == line:
118
 
            raise EOFError
119
 
        return line.strip()
120
 
 
121
 
    def _getchar(self):
122
 
        char = osutils.getchar()
123
 
        if char == chr(3): # INTR
124
 
            raise KeyboardInterrupt
125
 
        if char == chr(4): # EOF (^d, C-d)
126
 
            raise EOFError
127
 
        return char
128
 
 
129
 
    def interact(self):
130
 
        """Keep asking the user until a valid choice is made.
131
 
        """
132
 
        if self.line_based:
133
 
            getchoice = self._getline
134
 
        else:
135
 
            getchoice = self._getchar
136
 
        iter = 0
137
 
        while True:
138
 
            iter += 1
139
 
            if 1 == iter or self.line_based:
140
 
                self.ui.prompt(self.prompt)
141
 
            try:
142
 
                choice = getchoice()
143
 
            except EOFError:
144
 
                self.ui.stderr.write('\n')
145
 
                return None
146
 
            except KeyboardInterrupt:
147
 
                self.ui.stderr.write('\n')
148
 
                raise KeyboardInterrupt
149
 
            choice = choice.lower()
150
 
            if choice not in self.alternatives:
151
 
                # Not a valid choice, keep on asking.
152
 
                continue
153
 
            name, index = self.alternatives[choice]
154
 
            if self.echo_back:
155
 
                self.ui.stderr.write(name + '\n')
156
 
            return index
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
 
 
165
46
class TextUIFactory(UIFactory):
166
 
    """A UI factory for Text user interfaces."""
 
47
    """A UI factory for Text user interefaces."""
167
48
 
168
49
    def __init__(self,
169
50
                 stdin=None,
180
61
        # paints progress, network activity, etc
181
62
        self._progress_view = self.make_progress_view()
182
63
 
183
 
    def choose(self, msg, choices, default=None):
184
 
        """Prompt the user for a list of alternatives.
185
 
 
186
 
        Support both line-based and char-based editing.
187
 
 
188
 
        In line-based mode, both the shortcut and full choice name are valid
189
 
        answers, e.g. for choose('prompt', '&yes\n&no'): 'y', ' Y ', ' yes',
190
 
        'YES ' are all valid input lines for choosing 'yes'.
191
 
 
192
 
        An empty line, when in line-based mode, or pressing enter in char-based
193
 
        mode will select the default choice (if any).
194
 
 
195
 
        Choice is echoed back if:
196
 
        - input is char-based; which means a controlling terminal is available,
197
 
          and osutils.getchar is used
198
 
        - input is line-based, and no controlling terminal is available
199
 
        """
200
 
 
201
 
        choose_ui = _ChooseUI(self, msg, choices, default)
202
 
        return choose_ui.interact()
203
 
 
204
64
    def be_quiet(self, state):
205
65
        if state and not self._quiet:
206
66
            self.clear_term()
218
78
        # to clear it.  We might need to separately check for the case of
219
79
        self._progress_view.clear()
220
80
 
 
81
    def get_boolean(self, prompt):
 
82
        while True:
 
83
            self.prompt(prompt + "? [y/n]: ")
 
84
            line = self.stdin.readline().lower()
 
85
            if line in ('y\n', 'yes\n'):
 
86
                return True
 
87
            elif line in ('n\n', 'no\n'):
 
88
                return False
 
89
            elif line in ('', None):
 
90
                # end-of-file; possibly should raise an error here instead
 
91
                return None
 
92
 
221
93
    def get_integer(self, prompt):
222
94
        while True:
223
95
            self.prompt(prompt)
238
110
            password = self.stdin.readline()
239
111
            if not password:
240
112
                password = None
241
 
            else:
242
 
                password = password.decode(self.stdin.encoding)
243
 
 
244
 
                if password[-1] == '\n':
245
 
                    password = password[:-1]
 
113
            elif password[-1] == '\n':
 
114
                password = password[:-1]
246
115
        return password
247
116
 
248
 
    def get_password(self, prompt=u'', **kwargs):
 
117
    def get_password(self, prompt='', **kwargs):
249
118
        """Prompt the user for a password.
250
119
 
251
120
        :param prompt: The prompt to present the user
276
145
        username = self.stdin.readline()
277
146
        if not username:
278
147
            username = None
279
 
        else:
280
 
            username = username.decode(self.stdin.encoding)
281
 
            if username[-1] == '\n':
282
 
                username = username[:-1]
 
148
        elif username[-1] == '\n':
 
149
            username = username[:-1]
283
150
        return username
284
151
 
285
152
    def make_progress_view(self):
291
158
        # do that.  otherwise, guess based on $TERM and tty presence.
292
159
        if self.is_quiet():
293
160
            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()
 
161
        elif os.environ.get('BZR_PROGRESS_BAR') == 'text':
 
162
            return TextProgressView(self.stderr)
 
163
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
 
164
            return NullProgressView()
 
165
        elif progress._supports_progress(self.stderr):
 
166
            return TextProgressView(self.stderr)
 
167
        else:
 
168
            return NullProgressView()
302
169
 
303
170
    def _make_output_stream_explicit(self, encoding, encoding_type):
304
171
        if encoding_type == 'exact':
331
198
        :param kwargs: Dictionary of arguments to insert into the prompt,
332
199
            to allow UIs to reformat the prompt.
333
200
        """
334
 
        if type(prompt) != unicode:
335
 
            raise ValueError("prompt %r not a unicode string" % prompt)
336
201
        if kwargs:
337
202
            # See <https://launchpad.net/bugs/365891>
338
203
            prompt = prompt % kwargs
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')
 
204
        prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
346
205
        self.clear_term()
347
 
        self.stdout.flush()
348
206
        self.stderr.write(prompt)
349
207
 
350
208
    def report_transport_activity(self, transport, byte_count, direction):
422
280
    this only prints the stack from the nominated current task up to the root.
423
281
    """
424
282
 
425
 
    def __init__(self, term_file, encoding=None, errors="replace"):
 
283
    def __init__(self, term_file):
426
284
        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
432
285
        # true when there's output on the screen we may need to clear
433
286
        self._have_output = False
434
287
        self._last_transport_msg = ''
455
308
        else:
456
309
            return w - 1
457
310
 
458
 
    def _show_line(self, u):
459
 
        s = u.encode(self._encoding, self._encoding_errors)
 
311
    def _show_line(self, s):
 
312
        # sys.stderr.write("progress %r\n" % s)
460
313
        width = self._avail_width()
461
314
        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.
464
315
            s = '%-*.*s' % (width, width, s)
465
316
        self._term_file.write('\r' + s + '\r')
466
317