[PATCH 2 of 3] ui: prompt_key -- like prompt, but inputs only one keystroke

Kirill Smelkov kirr at mns.spb.ru
Thu Dec 27 03:05:15 CST 2007


В сообщении от 27 декабря 2007 04:05 Matt Mackall написал(a):
> > В сообщении от 26 декабря 2007 08:07 Bryan O'Sullivan написал(a):
> > > Kirill Smelkov wrote:
> > > > -    def prompt(self, msg, pat=None, default="y", matchflags=0):
> > > > +    def prompt(self, msg, pat=None, default="y", matchflags=0,
> > > > inputflags=0): if not self.interactive: return default
> > > >          try:
> > > > -            r = self._readline(msg + ' ')
> > > > +            if inputflags & INPUT_KEYSTROKE:
> > > > +                self.write(msg + ' ')
> > > > +                self.flush()
> > > > +                r = util.getch()
> > > > +                self.write(r + '\n')
> > > > +            else:
> > > > +                r = self._readline(msg + ' ')
> > > > +
> > >
> > > It will be less work to simply write a new function instead of crufting
> > > this one up.
> >
> > I originally wanted to do what you write, but realized that we'll need to
> > duplicate:
> >
> > - matching code
> > - response recognized/ not recognized
> > - EOFError handling
>
> First, flags are generally best avoided. Thanks for drawing my attention
> to this code, where the silly matchflags parameter had already snuck in.
> Python already lets you embed these flags directly in the regex, which
> is a much nicer way to do things:
[...]
> So we should undo that.

ok, here is prompt cleanup:

# HG changeset patch
# User Kirill Smelkov <kirr at mns.spb.ru>
# Date 1198745355 -10800
# Node ID 6b918b1d5c3c705a2afb4069528040eb4a0acfb7
# Parent  e9f1a1ccd51b42f3756fda476968e80e7cd1fe75
prompt: kill matchflags

rationale: Python already lets one to embed RE flags directly in a regex, which
rationale: is a much nicer way to do things:

(?iLmsux)
        (One or more letters from the set "i", "L", "m", "s", "u", "x".)
        ...

matchflags was introduced in 67afecb8d6cc, and the record extension is the only
user. I've killed matchflag, and adjusted record code appropriately.

spot-by: Matt Mackall <mpm at selenic.com>

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -247,8 +247,8 @@ def filterpatch(ui, chunks):
         if resp_file[0] is not None:
             return resp_file[0]
         while True:
-            r = (ui.prompt(query + _(' [Ynsfdaq?] '), '[Ynsfdaq?]?$',
-                           matchflags=re.I) or 'y').lower()
+            r = (ui.prompt(query + _(' [Ynsfdaq?] '), '(?i)[Ynsfdaq?]?$')
+                 or 'y').lower()
             if r == '?':
                 c = record.__doc__.find('y - record this change')
                 for l in record.__doc__[c:].splitlines():
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -410,11 +410,15 @@ class ui(object):
             line = line[:-1]
         return line
 
-    def prompt(self, msg, pat=None, default="y", matchflags=0):
+    def prompt(self, msg, pat=None, default="y"):
+        """Prompt user with msg, read his answer, and ensure it matches pat
+        
+        If not interactive -- the default is returned
+        """
         if not self.interactive: return default
         try:
             r = self._readline(msg + ' ')
-            if not pat or re.match(pat, r, matchflags):
+            if not pat or re.match(pat, r):
                 return r
             else:
                 self.write(_("unrecognized response\n"))




> Second, most of our prompts are currently single-character, and we
> probably want some consistency here. And no, I'm not particularly keen
> on having a command-line option to control the behavior. That just means
> when I sit down at someone else's machine and hit "y<enter>", I may be
> surprised to have just done two things rather than one.

I agree, that normal prompts need <enter> always. Record is special however --
there the user is doing a *lot* of answers, so I think it is convenient to ease
the task.

As said, in another mail in the thread by Aurelien, fsck has this behaviour.
Also I can say that my experience with darcs was very productive, and I like
the way record works there.

Unfortunately, darcs has its own problems, so for me, record was a killer
feature, so right after I saw there is record extension for HG I've switched
-- thanks Bryan!

Now I'm polishing it, and also mq version of record is in the works in my patch
queue.



More information about the Mercurial-devel mailing list