~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-24 18:26:21 UTC
  • mfrom: (4567 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4568.
  • Revision ID: john@arbash-meinel.com-20090724182621-68s2jhoqf3pn72n7
Merge bzr.dev 4567 to resolve 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
22
import os
23
23
import sys
24
24
import time
34
34
 
35
35
""")
36
36
 
37
 
from bzrlib.ui import CLIUIFactory
38
 
 
39
 
 
40
 
class TextUIFactory(CLIUIFactory):
 
37
from bzrlib.ui import (
 
38
    UIFactory,
 
39
    NullProgressView,
 
40
    )
 
41
 
 
42
 
 
43
class TextUIFactory(UIFactory):
41
44
    """A UI factory for Text user interefaces."""
42
45
 
43
46
    def __init__(self,
44
 
                 bar_type=None,
45
47
                 stdin=None,
46
48
                 stdout=None,
47
49
                 stderr=None):
51
53
                         letting the bzrlib.progress.ProgressBar factory auto
52
54
                         select.   Deprecated.
53
55
        """
54
 
        super(TextUIFactory, self).__init__(stdin=stdin,
55
 
                stdout=stdout, stderr=stderr)
56
 
        if bar_type:
57
 
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
58
 
                % "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
59
62
        # paints progress, network activity, etc
60
 
        self._progress_view = self._make_progress_view()
 
63
        self._progress_view = self.make_progress_view()
61
64
        
62
65
    def clear_term(self):
63
66
        """Prepare the terminal for output.
70
73
        # to clear it.  We might need to separately check for the case of
71
74
        self._progress_view.clear()
72
75
 
73
 
    def _make_progress_view(self):
74
 
        if os.environ.get('BZR_PROGRESS_BAR') in ('text', None, ''):
 
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):
75
148
            return TextProgressView(self.stderr)
76
149
        else:
77
150
            return NullProgressView()
81
154
        self.clear_term()
82
155
        self.stdout.write(msg + '\n')
83
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
 
84
170
    def report_transport_activity(self, transport, byte_count, direction):
85
171
        """Called by transports as they do IO.
86
172
 
105
191
        self._progress_view.clear()
106
192
 
107
193
 
108
 
class NullProgressView(object):
109
 
    """Soak up and ignore progress information."""
110
 
 
111
 
    def clear(self):
112
 
        pass
113
 
 
114
 
    def show_progress(self, task):
115
 
        pass
116
 
 
117
 
    def show_transport_activity(self, transport, direction, byte_count):
118
 
        pass
119
 
    
120
 
 
121
194
class TextProgressView(object):
122
195
    """Display of progress bar and other information on a tty.
123
196