[PATCH] With corrected documentation
Ingo Proetel
proetel at aicas.de
Tue Aug 9 16:24:59 CDT 2011
# HG changeset patch
# User Ingo Proetel <proetel at aicas.de>
# Date 1312924615 -7200
# Node ID 3ab34f9c42699a8136bdb23a2f1ebb0aa445b4e6
# Parent f4522df38c658a1768ffe08c864575e119b327ce
ui: add function 'promptselection'
diff -r f4522df38c65 -r 3ab34f9c4269 mercurial/ui.py
--- a/mercurial/ui.py Tue Aug 02 15:21:10 2011 -0400
+++ b/mercurial/ui.py Tue Aug 09 23:16:55 2011 +0200
@@ -6,7 +6,7 @@
# GNU General Public License version 2 or any later version.
from i18n import _
-import errno, getpass, os, socket, sys, tempfile, traceback
+import errno, getpass, os, socket, sys, tempfile, traceback, re
import config, scmutil, util, error
class ui(object):
@@ -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:#request == 'reopen'
+ 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
+ '''
+ respsString = ''
+ for choice in choices:
+ command = choice[0]
+ helpText = choice[1]
+ commandChar = helpText[helpText.index('&')+1].lower()
+
+ if not default or default == command:
+ default = command
+ defaultCommandChar = commandChar
+ commandChar = commandChar.upper()
+ respsString += commandChar
+
+ while True:
+ r = self.prompt('%s [%s?]' %(msg, respsString), defaultCommandChar)
+ index = respsString.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 = re.sub('\&','',choice[1])
+ 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