[PATCH 1 of 3] getch() -- to read one keystroke from console
Kirill Smelkov
kirr at mns.spb.ru
Tue Dec 25 09:52:25 CST 2007
# HG changeset patch
# User Kirill Smelkov <kirr at mns.spb.ru>
# Date 1198597912 -10800
# Node ID b74f1d14d56ea89736a1505e6a0dc1e9f246b81a
# Parent 6ce2bf084c586678decb828ef1fc9b925d35af5d
getch() -- to read one keystroke from console
taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1766,3 +1766,46 @@ def hidepassword(url):
url_parts[1])
return urlparse.urlunparse(url_parts)
+
+
+# getch, taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
+class _Getch:
+ """Gets a single character from standard input. Does not echo to the
+screen."""
+ def __init__(self):
+ try:
+ self.impl = _GetchWindows()
+ except ImportError:
+ self.impl = _GetchUnix()
+
+ def __call__(self): return self.impl()
+
+
+class _GetchUnix:
+ def __init__(self):
+ import tty, sys
+
+ def __call__(self):
+ import sys, tty, termios
+ fd = sys.stdin.fileno()
+ old_settings = termios.tcgetattr(fd)
+ try:
+ tty.setraw(sys.stdin.fileno())
+ ch = sys.stdin.read(1)
+ finally:
+ termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
+ return ch
+
+
+class _GetchWindows:
+ def __init__(self):
+ import msvcrt
+
+ def __call__(self):
+ import msvcrt
+ return msvcrt.getch()
+
+
+getch = _Getch()
+
+
More information about the Mercurial-devel
mailing list