[PATCH] ui: add function 'promptselection'
Ingo Proetel
proetel at aicas.de
Thu Aug 11 02:36:57 CDT 2011
# HG changeset patch
# User Ingo Proetel <proetel at aicas.de>
# Date 1313046309 -7200
# Node ID 3ab40477ce60e1757ceeaa49e2c9d2790fbb47e0
# Parent f4522df38c658a1768ffe08c864575e119b327ce
ui: add function 'promptselection'
diff -r f4522df38c65 -r 3ab40477ce60 mercurial/ui.py
--- a/mercurial/ui.py Tue Aug 02 15:21:10 2011 -0400
+++ b/mercurial/ui.py Thu Aug 11 09:05:09 2011 +0200
@@ -584,6 +584,60 @@
return resps.index(r.lower())
self.write(_("unrecognized response\n"))
+ def promptselection(self, msg, choices, default=None):
+ '''Prompt user with msg, read response, and ensure it matches
+ one of the provided choices. choices is a sequence of tuples
+ with the format: (('command', _('help for &command')),...). command
+ is a non-localized string that will be the return value. Help texts are
+ localized and mark the expected user selection char with a '&'. A help
+ command for will be provided for input '?'. Responses are case
+ insensitive. If ui is not interactive, the default is returned.
+ If no default is given the first entry in choices is used.
+ Example:
+ choices = (('abort', _('&Abort command')),
+ ('new', _('Create &new request')),
+ ('reopen', _('&Reopen request')) )
+ request = ui.promptselection('now what?', choices, 'new')
+ if request == 'abort':
+ raise util.Abort('User aborted')
+ elif request == 'new':
+ ui.status('creating new')
+ else:
+ ui.status('reopen existing')
+ will ask:
+ $now what? [aNr?] ?
+ a Abort command
+ n Create new request
+ r Reopen request
+ $now what? [aNr?] n
+ creating new
+ '''
+ responses = ''
+ for choice in choices:
+ command = choice[0]
+ helptext = choice[1]
+ commandchar = helptext[helptext.index('&') + 1].lower()
+
+ if not default or default == command:
+ default = command
+ defaultchar = commandchar
+ commandchar = commandchar.upper()
+ responses += commandchar
+
+ while True:
+ r = self.prompt('%s [%s?]' % (msg, responses), defaultchar)
+ index = responses.lower().find(r.lower())
+ if index > -1:
+ return choices[index][0]
+ elif r == '?':
+ for choice in choices:
+ commandchar = choice[1][choice[1].index('&') + 1].lower()
+ helptext = choice[1].replace('\&', '')
+ self.write(' %s %s\n' % (commandchar, helptext))
+ self.write('\n')
+ else :
+ self.write(_("unrecognized response\n"))
+
def getpass(self, prompt=None, default=None):
if not self.interactive():
return default
More information about the Mercurial-devel
mailing list