[PATCH 1 of 3] ui: prompt() takes set of responses in lieu of regexp

Steve Borho steve at borho.org
Wed Apr 29 00:09:34 CDT 2009


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

Rather than taking a regexp pattern of acceptable responses, ui.prompt()
now takes a set 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 1264366a42f8 mercurial/ui.py
--- a/mercurial/ui.py	Sun Apr 26 16:50:44 2009 -0500
+++ b/mercurial/ui.py	Tue Apr 28 23:20:31 2009 -0500
@@ -267,20 +267,22 @@
             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=(), default="y"):
+        """Prompt user with msg, read response, and ensure it matches
+        one of the provided choices.  choices is a set of resp, name
+        tuples describing acceptable responses.  An empty set implies
+        no response checking. If not interactive, the default is returned.
         """
         if not self.interactive():
             self.note(msg, ' ', default, "\n")
             return default
+        resps = [resp for (resp, name) in choices]
         while True:
             try:
                 r = self._readline(msg + ' ')
                 if not r:
                     return default
-                if not pat or re.match(pat, r):
+                if not resps or r in resps:
                     return r
                 else:
                     self.write(_("unrecognized response\n"))


More information about the Mercurial-devel mailing list