[PATCH 1 of 1] ui: add function 'promptselection'

Ingo Proetel proetel at aicas.de
Mon Aug 8 09:50:19 CDT 2011


# HG changeset patch
# User Ingo Proetel <proetel at aicas.de>
# Date 1312814807 -7200
# Node ID 1aa7449cea39342171bd9c2f3430de7931761a26
# Parent  f4522df38c658a1768ffe08c864575e119b327ce
ui: add function 'promptselection'

diff -r f4522df38c65 -r 1aa7449cea39 mercurial/ui.py
--- a/mercurial/ui.py	Tue Aug 02 15:21:10 2011 -0400
+++ b/mercurial/ui.py	Mon Aug 08 16:46:47 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. The selected command string.
+        choices is a sequence of acceptable responses with the format:
+        ('&None', 'E&xec', 'Sym&link'). The possible return values are
+        'none', 'exec', or 'symlink'. An automatic help 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