Mercurial > public > mercurial-scm > hg
comparison mercurial/ui.py @ 8259:98acfd1d2b08
ui: replace regexp pattern with sequence of choices
Use ampersands (&) to delineate the response char in each choice.
ui.prompt() responses are now explicitly case insensitive. GUIs
that subclass ui can generate dialogs from the full choice names.
author | Steve Borho <steve@borho.org> |
---|---|
date | Thu, 30 Apr 2009 10:15:32 -0500 |
parents | 46293a0c7e9f |
children | b87a50b7125c |
comparison
equal
deleted
inserted
replaced
8258:2263c49af028 | 8259:98acfd1d2b08 |
---|---|
266 # raw_input() to emit an extra trailing carriage return | 266 # raw_input() to emit an extra trailing carriage return |
267 if os.linesep == '\r\n' and line and line[-1] == '\r': | 267 if os.linesep == '\r\n' and line and line[-1] == '\r': |
268 line = line[:-1] | 268 line = line[:-1] |
269 return line | 269 return line |
270 | 270 |
271 def prompt(self, msg, pat=None, default="y"): | 271 def prompt(self, msg, choices=None, default="y"): |
272 """Prompt user with msg, read response, and ensure it matches pat | 272 """Prompt user with msg, read response, and ensure it matches |
273 | 273 one of the provided choices. choices is a sequence of acceptable |
274 If not interactive -- the default is returned | 274 responses with the format: ('&None', 'E&xec', 'Sym&link') |
275 No sequence implies no response checking. Responses are case | |
276 insensitive. If ui is not interactive, the default is returned. | |
275 """ | 277 """ |
276 if not self.interactive(): | 278 if not self.interactive(): |
277 self.note(msg, ' ', default, "\n") | 279 self.note(msg, ' ', default, "\n") |
278 return default | 280 return default |
279 while True: | 281 while True: |
280 try: | 282 try: |
281 r = self._readline(msg + ' ') | 283 r = self._readline(msg + ' ') |
282 if not r: | 284 if not r: |
283 return default | 285 return default |
284 if not pat or re.match(pat, r): | 286 if not choices: |
285 return r | 287 return r |
288 resps = [s[s.index('&')+1].lower() for s in choices] | |
289 if r.lower() in resps: | |
290 return r.lower() | |
286 else: | 291 else: |
287 self.write(_("unrecognized response\n")) | 292 self.write(_("unrecognized response\n")) |
288 except EOFError: | 293 except EOFError: |
289 raise util.Abort(_('response expected')) | 294 raise util.Abort(_('response expected')) |
290 | 295 |