/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

  • Committer: John Arbash Meinel
  • Date: 2009-07-29 21:35:05 UTC
  • mfrom: (4576 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4577.
  • Revision ID: john@arbash-meinel.com-20090729213505-tkqsvy1zfpocu75w
Merge bzr.dev 4576 in prep for NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
 
19
18
"""Text UI, write output to the console.
20
19
"""
21
20
 
 
21
import getpass
 
22
import os
22
23
import sys
23
24
import time
24
25
import warnings
33
34
 
34
35
""")
35
36
 
36
 
from bzrlib.ui import CLIUIFactory
37
 
 
38
 
 
39
 
class TextUIFactory(CLIUIFactory):
 
37
from bzrlib.ui import (
 
38
    UIFactory,
 
39
    NullProgressView,
 
40
    )
 
41
 
 
42
 
 
43
class TextUIFactory(UIFactory):
40
44
    """A UI factory for Text user interefaces."""
41
45
 
42
46
    def __init__(self,
43
 
                 bar_type=None,
44
47
                 stdin=None,
45
48
                 stdout=None,
46
49
                 stderr=None):
50
53
                         letting the bzrlib.progress.ProgressBar factory auto
51
54
                         select.   Deprecated.
52
55
        """
53
 
        super(TextUIFactory, self).__init__(stdin=stdin,
54
 
                stdout=stdout, stderr=stderr)
55
 
        if bar_type:
56
 
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
57
 
                % "bar_type parameter")
 
56
        super(TextUIFactory, self).__init__()
 
57
        # TODO: there's no good reason not to pass all three streams, maybe we
 
58
        # should deprecate the default values...
 
59
        self.stdin = stdin
 
60
        self.stdout = stdout
 
61
        self.stderr = stderr
58
62
        # paints progress, network activity, etc
59
 
        self._progress_view = TextProgressView(self.stderr)
60
 
 
 
63
        self._progress_view = self.make_progress_view()
 
64
        
61
65
    def clear_term(self):
62
66
        """Prepare the terminal for output.
63
67
 
69
73
        # to clear it.  We might need to separately check for the case of
70
74
        self._progress_view.clear()
71
75
 
 
76
    def get_boolean(self, prompt):
 
77
        while True:
 
78
            self.prompt(prompt + "? [y/n]: ")
 
79
            line = self.stdin.readline().lower()
 
80
            if line in ('y\n', 'yes\n'):
 
81
                return True
 
82
            elif line in ('n\n', 'no\n'):
 
83
                return False
 
84
            elif line in ('', None):
 
85
                # end-of-file; possibly should raise an error here instead
 
86
                return None
 
87
 
 
88
    def get_non_echoed_password(self):
 
89
        isatty = getattr(self.stdin, 'isatty', None)
 
90
        if isatty is not None and isatty():
 
91
            # getpass() ensure the password is not echoed and other
 
92
            # cross-platform niceties
 
93
            password = getpass.getpass('')
 
94
        else:
 
95
            # echo doesn't make sense without a terminal
 
96
            password = self.stdin.readline()
 
97
            if not password:
 
98
                password = None
 
99
            elif password[-1] == '\n':
 
100
                password = password[:-1]
 
101
        return password
 
102
 
 
103
    def get_password(self, prompt='', **kwargs):
 
104
        """Prompt the user for a password.
 
105
 
 
106
        :param prompt: The prompt to present the user
 
107
        :param kwargs: Arguments which will be expanded into the prompt.
 
108
                       This lets front ends display different things if
 
109
                       they so choose.
 
110
        :return: The password string, return None if the user
 
111
                 canceled the request.
 
112
        """
 
113
        prompt += ': '
 
114
        self.prompt(prompt, **kwargs)
 
115
        # There's currently no way to say 'i decline to enter a password'
 
116
        # as opposed to 'my password is empty' -- does it matter?
 
117
        return self.get_non_echoed_password()
 
118
 
 
119
    def get_username(self, prompt, **kwargs):
 
120
        """Prompt the user for a username.
 
121
 
 
122
        :param prompt: The prompt to present the user
 
123
        :param kwargs: Arguments which will be expanded into the prompt.
 
124
                       This lets front ends display different things if
 
125
                       they so choose.
 
126
        :return: The username string, return None if the user
 
127
                 canceled the request.
 
128
        """
 
129
        prompt += ': '
 
130
        self.prompt(prompt, **kwargs)
 
131
        username = self.stdin.readline()
 
132
        if not username:
 
133
            username = None
 
134
        elif username[-1] == '\n':
 
135
            username = username[:-1]
 
136
        return username
 
137
 
 
138
    def make_progress_view(self):
 
139
        """Construct and return a new ProgressView subclass for this UI.
 
140
        """
 
141
        # if the user specifically requests either text or no progress bars,
 
142
        # always do that.  otherwise, guess based on $TERM and tty presence.
 
143
        if os.environ.get('BZR_PROGRESS_BAR') == 'text':
 
144
            return TextProgressView(self.stderr)
 
145
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
 
146
            return NullProgressView()
 
147
        elif progress._supports_progress(self.stderr):
 
148
            return TextProgressView(self.stderr)
 
149
        else:
 
150
            return NullProgressView()
 
151
 
72
152
    def note(self, msg):
73
153
        """Write an already-formatted message, clearing the progress bar if necessary."""
74
154
        self.clear_term()
75
155
        self.stdout.write(msg + '\n')
76
156
 
 
157
    def prompt(self, prompt, **kwargs):
 
158
        """Emit prompt on the CLI.
 
159
        
 
160
        :param kwargs: Dictionary of arguments to insert into the prompt,
 
161
            to allow UIs to reformat the prompt.
 
162
        """
 
163
        if kwargs:
 
164
            # See <https://launchpad.net/bugs/365891>
 
165
            prompt = prompt % kwargs
 
166
        prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
 
167
        self.clear_term()
 
168
        self.stderr.write(prompt)
 
169
 
77
170
    def report_transport_activity(self, transport, byte_count, direction):
78
171
        """Called by transports as they do IO.
79
172
 
80
173
        This may update a progress bar, spinner, or similar display.
81
174
        By default it does nothing.
82
175
        """
83
 
        self._progress_view._show_transport_activity(transport,
 
176
        self._progress_view.show_transport_activity(transport,
84
177
            direction, byte_count)
85
178
 
86
179
    def _progress_updated(self, task):
118
211
        # true when there's output on the screen we may need to clear
119
212
        self._have_output = False
120
213
        # XXX: We could listen for SIGWINCH and update the terminal width...
 
214
        # https://launchpad.net/bugs/316357
121
215
        self._width = osutils.terminal_width()
122
216
        self._last_transport_msg = ''
123
217
        self._spin_pos = 0
216
310
        self._last_repaint = now
217
311
        self._repaint()
218
312
 
219
 
    def _show_transport_activity(self, transport, direction, byte_count):
 
313
    def show_transport_activity(self, transport, direction, byte_count):
220
314
        """Called by transports via the ui_factory, as they do IO.
221
315
 
222
316
        This may update a progress bar, spinner, or similar display.
225
319
        # XXX: Probably there should be a transport activity model, and that
226
320
        # too should be seen by the progress view, rather than being poked in
227
321
        # here.
 
322
        if not self._have_output:
 
323
            # As a workaround for <https://launchpad.net/bugs/321935> we only
 
324
            # show transport activity when there's already a progress bar
 
325
            # shown, which time the application code is expected to know to
 
326
            # clear off the progress bar when it's going to send some other
 
327
            # output.  Eventually it would be nice to have that automatically
 
328
            # synchronized.
 
329
            return
228
330
        self._total_byte_count += byte_count
229
331
        self._bytes_since_update += byte_count
230
332
        now = time.time()
234
336
            # guard against clock stepping backwards, and don't update too
235
337
            # often
236
338
            rate = self._bytes_since_update / (now - self._transport_update_time)
237
 
            scheme = getattr(transport, '_scheme', None) or repr(transport)
238
 
            if direction == 'read':
239
 
                dir_char = '>'
240
 
            elif direction == 'write':
241
 
                dir_char = '<'
242
 
            else:
243
 
                dir_char = ' '
244
 
            msg = ("%.7s %s %6dKB %5dKB/s" %
245
 
                    (scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
 
339
            msg = ("%6dKB %5dKB/s" %
 
340
                    (self._total_byte_count>>10, int(rate)>>10,))
246
341
            self._transport_update_time = now
247
342
            self._last_repaint = now
248
343
            self._bytes_since_update = 0
249
344
            self._last_transport_msg = msg
250
345
            self._repaint()
251
 
 
252