37
from bzrlib.ui import CLIUIFactory
40
class TextUIFactory(CLIUIFactory):
39
from bzrlib.ui import (
45
class TextUIFactory(UIFactory):
41
46
"""A UI factory for Text user interefaces."""
48
52
"""Create a TextUIFactory.
50
:param bar_type: The type of progress bar to create. It defaults to
51
letting the bzrlib.progress.ProgressBar factory auto
54
super(TextUIFactory, self).__init__(stdin=stdin,
55
stdout=stdout, stderr=stderr)
57
symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
58
% "bar_type parameter")
54
super(TextUIFactory, self).__init__()
55
# TODO: there's no good reason not to pass all three streams, maybe we
56
# should deprecate the default values...
59
60
# paints progress, network activity, etc
60
self._progress_view = self._make_progress_view()
61
self._progress_view = self.make_progress_view()
62
63
def clear_term(self):
63
64
"""Prepare the terminal for output.
70
71
# to clear it. We might need to separately check for the case of
71
72
self._progress_view.clear()
73
def _make_progress_view(self):
74
if os.environ.get('BZR_PROGRESS_BAR') in ('text', None, ''):
75
return TextProgressView(self.stderr)
77
return NullProgressView()
74
def get_boolean(self, prompt):
76
self.prompt(prompt + "? [y/n]: ")
77
line = self.stdin.readline().lower()
78
if line in ('y\n', 'yes\n'):
80
elif line in ('n\n', 'no\n'):
82
elif line in ('', None):
83
# end-of-file; possibly should raise an error here instead
86
def get_non_echoed_password(self):
87
isatty = getattr(self.stdin, 'isatty', None)
88
if isatty is not None and isatty():
89
# getpass() ensure the password is not echoed and other
90
# cross-platform niceties
91
password = getpass.getpass('')
93
# echo doesn't make sense without a terminal
94
password = self.stdin.readline()
97
elif password[-1] == '\n':
98
password = password[:-1]
101
def get_password(self, prompt='', **kwargs):
102
"""Prompt the user for a password.
104
:param prompt: The prompt to present the user
105
:param kwargs: Arguments which will be expanded into the prompt.
106
This lets front ends display different things if
108
:return: The password string, return None if the user
109
canceled the request.
112
self.prompt(prompt, **kwargs)
113
# There's currently no way to say 'i decline to enter a password'
114
# as opposed to 'my password is empty' -- does it matter?
115
return self.get_non_echoed_password()
117
def get_username(self, prompt, **kwargs):
118
"""Prompt the user for a username.
120
:param prompt: The prompt to present the user
121
:param kwargs: Arguments which will be expanded into the prompt.
122
This lets front ends display different things if
124
:return: The username string, return None if the user
125
canceled the request.
128
self.prompt(prompt, **kwargs)
129
username = self.stdin.readline()
132
elif username[-1] == '\n':
133
username = username[:-1]
136
def make_progress_view(self):
137
"""Construct and return a new ProgressView subclass for this UI.
139
# if the user specifically requests either text or no progress bars,
140
# always do that. otherwise, guess based on $TERM and tty presence.
141
if os.environ.get('BZR_PROGRESS_BAR') == 'text':
142
return TextProgressView(self.stderr)
143
elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
144
return NullProgressView()
145
elif progress._supports_progress(self.stderr):
146
return TextProgressView(self.stderr)
148
return NullProgressView()
150
def _make_output_stream_explicit(self, encoding, encoding_type):
151
if encoding_type == 'exact':
152
# force sys.stdout to be binary stream on win32;
153
# NB: this leaves the file set in that mode; may cause problems if
154
# one process tries to do binary and then text output
155
if sys.platform == 'win32':
156
fileno = getattr(self.stdout, 'fileno', None)
159
msvcrt.setmode(fileno(), os.O_BINARY)
160
return TextUIOutputStream(self, self.stdout)
162
encoded_stdout = codecs.getwriter(encoding)(self.stdout,
163
errors=encoding_type)
164
# For whatever reason codecs.getwriter() does not advertise its encoding
165
# it just returns the encoding of the wrapped file, which is completely
166
# bogus. So set the attribute, so we can find the correct encoding later.
167
encoded_stdout.encoding = encoding
168
return TextUIOutputStream(self, encoded_stdout)
79
170
def note(self, msg):
80
171
"""Write an already-formatted message, clearing the progress bar if necessary."""
82
173
self.stdout.write(msg + '\n')
175
def prompt(self, prompt, **kwargs):
176
"""Emit prompt on the CLI.
178
:param kwargs: Dictionary of arguments to insert into the prompt,
179
to allow UIs to reformat the prompt.
182
# See <https://launchpad.net/bugs/365891>
183
prompt = prompt % kwargs
184
prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
186
self.stderr.write(prompt)
84
188
def report_transport_activity(self, transport, byte_count, direction):
85
189
"""Called by transports as they do IO.
270
388
self._bytes_since_update = 0
271
389
self._last_transport_msg = msg
393
class TextUIOutputStream(object):
394
"""Decorates an output stream so that the terminal is cleared before writing.
396
This is supposed to ensure that the progress bar does not conflict with bulk
399
# XXX: this does not handle the case of writing part of a line, then doing
400
# progress bar output: the progress bar will probably write over it.
401
# one option is just to buffer that text until we have a full line;
402
# another is to save and restore it
404
# XXX: might need to wrap more methods
406
def __init__(self, ui_factory, wrapped_stream):
407
self.ui_factory = ui_factory
408
self.wrapped_stream = wrapped_stream
409
# this does no transcoding, but it must expose the underlying encoding
410
# because some callers need to know what can be written - see for
411
# example unescape_for_display.
412
self.encoding = getattr(wrapped_stream, 'encoding', None)
415
self.ui_factory.clear_term()
416
self.wrapped_stream.flush()
418
def write(self, to_write):
419
self.ui_factory.clear_term()
420
self.wrapped_stream.write(to_write)
422
def writelines(self, lines):
423
self.ui_factory.clear_term()
424
self.wrapped_stream.writelines(lines)