46
class _ChooseUI(object):
48
""" Helper class for choose implementation.
51
def __init__(self, ui, msg, choices, default):
54
self._build_alternatives(msg, choices, default)
56
def _setup_mode(self):
57
"""Setup input mode (line-based, char-based) and echo-back.
59
Line-based input is used if the BZR_TEXTUI_INPUT environment
60
variable is set to 'line-based', or if there is no controlling
63
if os.environ.get('BZR_TEXTUI_INPUT') != 'line-based' and \
64
self.ui.stdin == sys.stdin and self.ui.stdin.isatty():
65
self.line_based = False
68
self.line_based = True
69
self.echo_back = not self.ui.stdin.isatty()
71
def _build_alternatives(self, msg, choices, default):
72
"""Parse choices string.
74
Setup final prompt and the lists of choices and associated
79
self.alternatives = {}
80
choices = choices.split('\n')
81
if default is not None and default not in range(0, len(choices)):
82
raise ValueError("invalid default index")
84
name = c.replace('&', '').lower()
85
choice = (name, index)
86
if name in self.alternatives:
87
raise ValueError("duplicated choice: %s" % name)
88
self.alternatives[name] = choice
89
shortcut = c.find('&')
90
if -1 != shortcut and (shortcut + 1) < len(c):
92
help += '[' + c[shortcut + 1] + ']'
93
help += c[(shortcut + 2):]
94
shortcut = c[shortcut + 1]
96
c = c.replace('&', '')
98
help = '[%s]%s' % (shortcut, c[1:])
99
shortcut = shortcut.lower()
100
if shortcut in self.alternatives:
101
raise ValueError("duplicated shortcut: %s" % shortcut)
102
self.alternatives[shortcut] = choice
103
# Add redirections for default.
105
self.alternatives[''] = choice
106
self.alternatives['\r'] = choice
107
help_list.append(help)
110
self.prompt = u'%s (%s): ' % (msg, ', '.join(help_list))
113
line = self.ui.stdin.readline()
119
char = osutils.getchar()
120
if char == chr(3): # INTR
121
raise KeyboardInterrupt
122
if char == chr(4): # EOF (^d, C-d)
127
"""Keep asking the user until a valid choice is made.
130
getchoice = self._getline
132
getchoice = self._getchar
136
if 1 == iter or self.line_based:
137
self.ui.prompt(self.prompt)
141
self.ui.stderr.write('\n')
143
except KeyboardInterrupt:
144
self.ui.stderr.write('\n')
145
raise KeyboardInterrupt
146
choice = choice.lower()
147
if choice not in self.alternatives:
148
# Not a valid choice, keep on asking.
150
name, index = self.alternatives[choice]
152
self.ui.stderr.write(name + '\n')
156
46
class TextUIFactory(UIFactory):
157
47
"""A UI factory for Text user interefaces."""
171
61
# paints progress, network activity, etc
172
62
self._progress_view = self.make_progress_view()
174
def choose(self, msg, choices, default=None):
175
"""Prompt the user for a list of alternatives.
177
Support both line-based and char-based editing.
179
In line-based mode, both the shortcut and full choice name are valid
180
answers, e.g. for choose('prompt', '&yes\n&no'): 'y', ' Y ', ' yes',
181
'YES ' are all valid input lines for choosing 'yes'.
183
An empty line, when in line-based mode, or pressing enter in char-based
184
mode will select the default choice (if any).
186
Choice is echoed back if:
187
- input is char-based; which means a controlling terminal is available,
188
and osutils.getchar is used
189
- input is line-based, and no controlling terminal is available
192
choose_ui = _ChooseUI(self, msg, choices, default)
193
return choose_ui.interact()
195
64
def be_quiet(self, state):
196
65
if state and not self._quiet:
209
78
# to clear it. We might need to separately check for the case of
210
79
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
212
93
def get_integer(self, prompt):
214
95
self.prompt(prompt)