[PATCH] ui: prompt() takes sequence of responses in lieu of regexp

Steve Borho steve at borho.org
Wed Apr 29 22:38:16 CDT 2009


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1241062368 18000
# Node ID 2f9f678f9501165542c5ffd717ed613e4b82e3ad
# Parent  344751cd8cb88bac208d630f4825068a3170c92f
ui: prompt() takes sequence of responses in lieu of regexp

Rather than taking a regexp pattern of acceptable responses, ui.prompt()
now takes a sequence of (resp, name) tuples.  ui.prompt() uses just the
resp half of the tuples to validate user responses.  GUI applications
that subclass ui can use the names to build meaningful dialogs.

diff -r 344751cd8cb8 -r 2f9f678f9501 mercurial/ui.py
--- a/mercurial/ui.py	Sun Apr 26 16:50:44 2009 -0500
+++ b/mercurial/ui.py	Wed Apr 29 22:32:48 2009 -0500
@@ -267,10 +267,11 @@
             line = line[:-1]
         return line
 
-    def prompt(self, msg, pat=None, default="y"):
-        """Prompt user with msg, read response, and ensure it matches pat
-
-        If not interactive -- the default is returned
+    def prompt(self, msg, choices=None, default="y"):
+        """Prompt user with msg, read response, and ensure it matches
+        one of the provided choices.  choices is a sequence of (resp, name)
+        tuples describing acceptable responses.  No sequence implies
+        no response checking. If not interactive, the default is returned.
         """
         if not self.interactive():
             self.note(msg, ' ', default, "\n")
@@ -280,7 +281,7 @@
                 r = self._readline(msg + ' ')
                 if not r:
                     return default
-                if not pat or re.match(pat, r):
+                if choices is None or r in [resp for (resp, name) in choices]:
                     return r
                 else:
                     self.write(_("unrecognized response\n"))


More information about the Mercurial-devel mailing list