~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: 2009-07-22 09:03:35 UTC
  • mfrom: (4449.3.47 387717-progress-bar-tty)
  • Revision ID: pqm@pqm.ubuntu.com-20090722090335-m1okio1ev9bwytui
(mbp) various UIFactory cleanups including bug 387717

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
 
34
33
 
35
34
""")
36
35
 
37
 
from bzrlib.ui import CLIUIFactory
38
 
 
39
 
 
40
 
class TextUIFactory(CLIUIFactory):
 
36
from bzrlib.ui import (
 
37
    UIFactory,
 
38
    NullProgressView,
 
39
    )
 
40
 
 
41
 
 
42
class TextUIFactory(UIFactory):
41
43
    """A UI factory for Text user interefaces."""
42
44
 
43
45
    def __init__(self,
44
 
                 bar_type=None,
45
46
                 stdin=None,
46
47
                 stdout=None,
47
48
                 stderr=None):
51
52
                         letting the bzrlib.progress.ProgressBar factory auto
52
53
                         select.   Deprecated.
53
54
        """
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")
 
55
        super(TextUIFactory, self).__init__()
 
56
        # TODO: there's no good reason not to pass all three streams, maybe we
 
57
        # should deprecate the default values...
 
58
        self.stdin = stdin
 
59
        self.stdout = stdout
 
60
        self.stderr = stderr
59
61
        # paints progress, network activity, etc
60
 
        self._progress_view = self._make_progress_view()
 
62
        self._progress_view = self.make_progress_view()
61
63
        
62
64
    def clear_term(self):
63
65
        """Prepare the terminal for output.
70
72
        # to clear it.  We might need to separately check for the case of
71
73
        self._progress_view.clear()
72
74
 
73
 
    def _make_progress_view(self):
74
 
        if os.environ.get('BZR_PROGRESS_BAR') in ('text', None, ''):
 
75
    def get_boolean(self, prompt):
 
76
        while True:
 
77
            self.prompt(prompt + "? [y/n]: ")
 
78
            line = self.stdin.readline().lower()
 
79
            if line in ('y\n', 'yes\n'):
 
80
                return True
 
81
            elif line in ('n\n', 'no\n'):
 
82
                return False
 
83
            elif line in ('', None):
 
84
                # end-of-file; possibly should raise an error here instead
 
85
                return None
 
86
 
 
87
    def get_non_echoed_password(self):
 
88
        isatty = getattr(self.stdin, 'isatty', None)
 
89
        if isatty is not None and isatty():
 
90
            # getpass() ensure the password is not echoed and other
 
91
            # cross-platform niceties
 
92
            password = getpass.getpass('')
 
93
        else:
 
94
            # echo doesn't make sense without a terminal
 
95
            password = self.stdin.readline()
 
96
            if not password:
 
97
                password = None
 
98
            elif password[-1] == '\n':
 
99
                password = password[:-1]
 
100
        return password
 
101
 
 
102
    def get_password(self, prompt='', **kwargs):
 
103
        """Prompt the user for a password.
 
104
 
 
105
        :param prompt: The prompt to present the user
 
106
        :param kwargs: Arguments which will be expanded into the prompt.
 
107
                       This lets front ends display different things if
 
108
                       they so choose.
 
109
        :return: The password string, return None if the user
 
110
                 canceled the request.
 
111
        """
 
112
        prompt += ': '
 
113
        self.prompt(prompt, **kwargs)
 
114
        # There's currently no way to say 'i decline to enter a password'
 
115
        # as opposed to 'my password is empty' -- does it matter?
 
116
        return self.get_non_echoed_password()
 
117
 
 
118
    def get_username(self, prompt, **kwargs):
 
119
        """Prompt the user for a username.
 
120
 
 
121
        :param prompt: The prompt to present the user
 
122
        :param kwargs: Arguments which will be expanded into the prompt.
 
123
                       This lets front ends display different things if
 
124
                       they so choose.
 
125
        :return: The username string, return None if the user
 
126
                 canceled the request.
 
127
        """
 
128
        prompt += ': '
 
129
        self.prompt(prompt, **kwargs)
 
130
        username = self.stdin.readline()
 
131
        if not username:
 
132
            username = None
 
133
        elif username[-1] == '\n':
 
134
            username = username[:-1]
 
135
        return username
 
136
 
 
137
    def make_progress_view(self):
 
138
        """Construct and return a new ProgressView subclass for this UI.
 
139
        """
 
140
        # if the user specifically requests either text or no progress bars,
 
141
        # always do that.  otherwise, guess based on $TERM and tty presence.
 
142
        if os.environ.get('BZR_PROGRESS_BAR') == 'text':
 
143
            return TextProgressView(self.stderr)
 
144
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
 
145
            return NullProgressView()
 
146
        elif progress._supports_progress(self.stderr):
75
147
            return TextProgressView(self.stderr)
76
148
        else:
77
149
            return NullProgressView()
81
153
        self.clear_term()
82
154
        self.stdout.write(msg + '\n')
83
155
 
 
156
    def prompt(self, prompt, **kwargs):
 
157
        """Emit prompt on the CLI.
 
158
        
 
159
        :param kwargs: Dictionary of arguments to insert into the prompt,
 
160
            to allow UIs to reformat the prompt.
 
161
        """
 
162
        if kwargs:
 
163
            # See <https://launchpad.net/bugs/365891>
 
164
            prompt = prompt % kwargs
 
165
        prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
 
166
        self.clear_term()
 
167
        self.stderr.write(prompt)
 
168
 
84
169
    def report_transport_activity(self, transport, byte_count, direction):
85
170
        """Called by transports as they do IO.
86
171
 
105
190
        self._progress_view.clear()
106
191
 
107
192
 
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
193
class TextProgressView(object):
122
194
    """Display of progress bar and other information on a tty.
123
195