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

Kirill Smelkov kirr at mns.spb.ru
Thu Dec 27 07:48:38 CST 2007


# HG changeset patch
# User Kirill Smelkov <kirr at mns.spb.ru>
# Date 1198762486 -10800
# Node ID 5ce7ac984fd1c257ebf52063079bc049dd05f35f
# Parent  17f113422432bda71e9fbf7c743736ee7c7ed8c2
ui: prompt_key -- like prompt, but inputs only one keystroke

rationale: we'll need this for 'record'

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -22,6 +22,9 @@ def updateconfig(source, dest, sections=
             dest.add_section(section)
         for name, value in source.items(section, raw=True):
             dest.set(section, name, value)
+
+# ui related flags
+INPUT_KEYSTROKE =   0x0001
 
 class ui(object):
     _isatty = None
@@ -410,7 +413,7 @@ class ui(object):
             line = line[:-1]
         return line
 
-    def prompt(self, msg, pat=None, default="y"):
+    def prompt(self, msg, pat=None, default="y", inputflags=0):
         """Prompt user with msg, read his answer, and ensure it matches pat
 
         If not interactive -- the default is returned
@@ -418,13 +421,22 @@ class ui(object):
         if not self.interactive: return default
         while True:
             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 + ' ')
                 if not pat or re.match(pat, r):
                     return r
                 else:
                     self.write(_("unrecognized response\n"))
             except EOFError:
                 raise util.Abort(_('response expected'))
+
+    def prompt_key(self, msg, pat=None, default='y'):
+        return self.prompt(msg, pat, default, inputflags=INPUT_KEYSTROKE)
 
     def getpass(self, prompt=None, default=None):
         if not self.interactive: return default


More information about the Mercurial-devel mailing list