48
class _ChooseUI(object):
50
""" Helper class for choose implementation.
53
def __init__(self, ui, msg, choices, default):
56
self._build_alternatives(msg, choices, default)
58
def _setup_mode(self):
59
"""Setup input mode (line-based, char-based) and echo-back.
61
Line-based input is used if the BZR_TEXTUI_INPUT environment
62
variable is set to 'line-based', or if there is no controlling
65
if os.environ.get('BZR_TEXTUI_INPUT') != 'line-based' and \
66
self.ui.stdin == sys.stdin and self.ui.stdin.isatty():
67
self.line_based = False
70
self.line_based = True
71
self.echo_back = not self.ui.stdin.isatty()
73
def _build_alternatives(self, msg, choices, default):
74
"""Parse choices string.
76
Setup final prompt and the lists of choices and associated
81
self.alternatives = {}
82
choices = choices.split('\n')
83
if default is not None and default not in range(0, len(choices)):
84
raise ValueError("invalid default index")
86
name = c.replace('&', '').lower()
87
choice = (name, index)
88
if name in self.alternatives:
89
raise ValueError("duplicated choice: %s" % name)
90
self.alternatives[name] = choice
91
shortcut = c.find('&')
92
if -1 != shortcut and (shortcut + 1) < len(c):
94
help += '[' + c[shortcut + 1] + ']'
95
help += c[(shortcut + 2):]
96
shortcut = c[shortcut + 1]
98
c = c.replace('&', '')
100
help = '[%s]%s' % (shortcut, c[1:])
101
shortcut = shortcut.lower()
102
if shortcut in self.alternatives:
103
raise ValueError("duplicated shortcut: %s" % shortcut)
104
self.alternatives[shortcut] = choice
105
# Add redirections for default.
107
self.alternatives[''] = choice
108
self.alternatives['\r'] = choice
109
help_list.append(help)
112
self.prompt = u'%s (%s): ' % (msg, ', '.join(help_list))
115
line = self.ui.stdin.readline()
121
char = osutils.getchar()
122
if char == chr(3): # INTR
123
raise KeyboardInterrupt
124
if char == chr(4): # EOF (^d, C-d)
129
"""Keep asking the user until a valid choice is made.
132
getchoice = self._getline
134
getchoice = self._getchar
138
if 1 == iter or self.line_based:
139
self.ui.prompt(self.prompt)
143
self.ui.stderr.write('\n')
145
except KeyboardInterrupt:
146
self.ui.stderr.write('\n')
147
raise KeyboardInterrupt
148
choice = choice.lower()
149
if choice not in self.alternatives:
150
# Not a valid choice, keep on asking.
152
name, index = self.alternatives[choice]
154
self.ui.stderr.write(name + '\n')
46
158
class TextUIFactory(UIFactory):
47
159
"""A UI factory for Text user interefaces."""
61
173
# paints progress, network activity, etc
62
174
self._progress_view = self.make_progress_view()
176
def choose(self, msg, choices, default=None):
177
"""Prompt the user for a list of alternatives.
179
Support both line-based and char-based editing.
181
In line-based mode, both the shortcut and full choice name are valid
182
answers, e.g. for choose('prompt', '&yes\n&no'): 'y', ' Y ', ' yes',
183
'YES ' are all valid input lines for choosing 'yes'.
185
An empty line, when in line-based mode, or pressing enter in char-based
186
mode will select the default choice (if any).
188
Choice is echoed back if:
189
- input is char-based; which means a controlling terminal is available,
190
and osutils.getchar is used
191
- input is line-based, and no controlling terminal is available
194
choose_ui = _ChooseUI(self, msg, choices, default)
195
return choose_ui.interact()
64
197
def be_quiet(self, state):
65
198
if state and not self._quiet:
78
211
# to clear it. We might need to separately check for the case of
79
212
self._progress_view.clear()
81
def get_boolean(self, prompt):
83
self.prompt(prompt + "? [y/n]: ")
84
line = self.stdin.readline().lower()
85
if line in ('y\n', 'yes\n'):
87
elif line in ('n\n', 'no\n'):
89
elif line in ('', None):
90
# end-of-file; possibly should raise an error here instead
93
214
def get_integer(self, prompt):
95
216
self.prompt(prompt)
198
319
:param kwargs: Dictionary of arguments to insert into the prompt,
199
320
to allow UIs to reformat the prompt.
322
if type(prompt) != unicode:
323
raise ValueError("prompt %r not a unicode string" % prompt)
202
325
# See <https://launchpad.net/bugs/365891>
203
326
prompt = prompt % kwargs
204
327
prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
205
328
self.clear_term()
206
330
self.stderr.write(prompt)
208
332
def report_transport_activity(self, transport, byte_count, direction):