Patchwork [1,of,5] ui: add "extractchoices()" to share the logic to extract choices from prompt

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Nov. 18, 2013, 10:43 a.m.
Message ID <68b649593e1d7821012e.1384771435@juju>
Download mbox | patch
Permalink /patch/3056/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - Nov. 18, 2013, 10:43 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1384770812 -32400
#      Mon Nov 18 19:33:32 2013 +0900
# Node ID 68b649593e1d7821012e5b51a8aba386cdb18960
# Parent  08fffc33af47f3f47647fd5df6d8d76f71d7f38d
ui: add "extractchoices()" to share the logic to extract choices from prompt

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -640,6 +640,19 @@ 
         except EOFError:
             raise util.Abort(_('response expected'))
 
+    def extractchoices(self, prompt):
+        """Extract prompt message and list of choices from specified prompt.
+
+        This returns tuple "(message, choices)", and "choices" is the
+        list of tuple "(response character, text without &)".
+        """
+        parts = prompt.split('$$')
+        msg = parts[0].rstrip(' ')
+        choices = [p.strip(' ') for p in parts[1:]]
+        return (msg,
+                [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
+                 for s in choices])
+
     def promptchoice(self, prompt, default=0):
         """Prompt user with a message, read response, and ensure it matches
         one of the provided choices. The prompt is formatted as follows:
@@ -651,10 +664,8 @@ 
         returned.
         """
 
-        parts = prompt.split('$$')
-        msg = parts[0].rstrip(' ')
-        choices = [p.strip(' ') for p in parts[1:]]
-        resps = [s[s.index('&') + 1].lower() for s in choices]
+        msg, choices = self.extractchoices(prompt)
+        resps = [r for r, t in choices]
         while True:
             r = self.prompt(msg, resps[default])
             if r.lower() in resps: