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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Dec. 1, 2013, 4:01 p.m.
Message ID <e67c39469f0b1f19b5b3.1385913713@juju>
Download mbox | patch
Permalink /patch/3193/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Dec. 1, 2013, 4:01 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1385913029 -32400
#      Mon Dec 02 00:50:29 2013 +0900
# Node ID e67c39469f0b1f19b5b328d625022e8a30d13c55
# Parent  1df77035c8141d4586ff5af84c34d54cb9912402
ui: add "extractchoices()" to share the logic to extract choices from prompt
Augie Fackler - Jan. 16, 2014, 2:50 p.m.
On Mon, Dec 02, 2013 at 01:01:53AM +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1385913029 -32400
> #      Mon Dec 02 00:50:29 2013 +0900
> # Node ID e67c39469f0b1f19b5b328d625022e8a30d13c55
> # 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,20 @@
>          except EOFError:
>              raise util.Abort(_('response expected'))
>
> +    @staticmethod
> +    def extractchoices(prompt):

If this method can be completely static, why not move it out of the ui
object entirely?

> +        """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 +665,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
Katsunori FUJIWARA - Jan. 17, 2014, 7:20 a.m.
At Thu, 16 Jan 2014 09:50:23 -0500,
Augie Fackler wrote:
> 
> On Mon, Dec 02, 2013 at 01:01:53AM +0900, FUJIWARA Katsunori wrote:
> > # HG changeset patch
> > # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> > # Date 1385913029 -32400
> > #      Mon Dec 02 00:50:29 2013 +0900
> > # Node ID e67c39469f0b1f19b5b328d625022e8a30d13c55
> > # 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,20 @@
> >          except EOFError:
> >              raise util.Abort(_('response expected'))
> >
> > +    @staticmethod
> > +    def extractchoices(prompt):
> 
> If this method can be completely static, why not move it out of the ui
> object entirely?

Defining "extractchoices()" in "ui" class allows callers to use it
without newly adding "import ui".

"record" and "transplant" extensions (callers of "extractchoices()" in
this series) don't import "mercurial.ui" before and after this series.


> > +        """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 +665,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,20 @@ 
         except EOFError:
             raise util.Abort(_('response expected'))
 
+    @staticmethod
+    def extractchoices(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 +665,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: