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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Nov. 28, 2013, 3:11 p.m.
Message ID <2b4399a017a613c97a8c.1385651488@juju>
Download mbox | patch
Permalink /patch/3185/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - Nov. 28, 2013, 3:11 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1385651260 -32400
#      Fri Nov 29 00:07:40 2013 +0900
# Node ID 2b4399a017a613c97a8c0489b267209b1684d208
# Parent  1df77035c8141d4586ff5af84c34d54cb9912402
ui: add "extractchoices()" to share the logic to extract choices from prompt
Katsunori FUJIWARA - Nov. 30, 2013, 2:30 p.m.
At Fri, 29 Nov 2013 00:11:28 +0900,
FUJIWARA Katsunori wrote:
> 
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1385651260 -32400
> #      Fri Nov 29 00:07:40 2013 +0900
> # Node ID 2b4399a017a613c97a8c0489b267209b1684d208
> # Parent  1df77035c8141d4586ff5af84c34d54cb9912402
> ui: add "extractchoices()" to share the logic to extract choices from prompt
> 
> 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])
> +

I noticed that "ui.extractchoices()" should be defined as
"@staticmethod", because it doesn't need "self", and "@staticmethod"
is easy to reuse from other than "ui" class.

I'll re-post revised version (V3) of this series.


>      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:
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
> 

----------------------------------------------------------------------
[FUJIWARA Katsunori]                             foozy@lares.dti.ne.jp

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: