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

Ingo Proetel proetel at aicas.de
Thu Aug 4 07:26:02 CDT 2011


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

diff -r f4522df38c65 -r cdf128a0bf08 mercurial/ui.py
--- a/mercurial/ui.py	Tue Aug 02 15:21:10 2011 -0400
+++ b/mercurial/ui.py	Thu Aug 04 14:25:23 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,58 @@
                 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 command'),
+                   _('Create &new request'),
+                   _('&Reopen request') )
+        request = ui.promptselection('now what?', choices,'new')
+        if    request == 'abort':
+          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 = ''
+        commands = []
+        for choice in choices:
+          commandChar = choice[choice.index('&')+1].lower()
+          command = '%s%s' % re.search('(\w*)\&(\w*)',choice.lower()).group(1, 2)
+          commands += [command]
+          if not default or default.lower() == command:
+            default = command
+            commandChar = commandChar.upper()
+          respsString += commandChar
+
+        while True:
+            r = self.prompt("%s [%s?]" %(msg, respsString),
+                            respsString[commands.index(default)]).lower()
+            if r in respsString.lower():
+                return commands[respsString.lower().index(r)]
+            elif r == '?':
+                for choice in choices:
+                  self.write(_(' %s %s\n' % (choice[choice.index('&')+1].lower(),
+                                             re.sub('\&','',choice))))
+                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