aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormryouse2022-06-12 01:18:41 +0000
committermryouse2022-06-12 01:18:41 +0000
commit0459ef120e1a7f23c259c35d3249b71120906fcc (patch)
tree20f02241c8bd2ab1ef49a3e210683fd05284e87f
parent6fcf1dd36c30368b2833cfb517208f67aaac98e5 (diff)
implement read-char
-rw-r--r--interpreter.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/interpreter.py b/interpreter.py
index a2d7b08..e2cf006 100644
--- a/interpreter.py
+++ b/interpreter.py
@@ -712,6 +712,20 @@ def interpretReadLine(symbol, args, env):
GLOBALS.register("read-line", Builtin(interpretReadLine, [Arg("prompt", T.String, False, False)]))
+def interpretReadChar(symbol, args, env):
+ import termios, tty
+ fd = sys.stdin.fileno()
+ old = termios.tcgetattr(fd)
+ try:
+ tty.setraw(fd)
+ ch = sys.stdin.buffer.read1(4) # some keys are >1 bytes
+ except Exception:
+ raise
+ finally:
+ termios.tcsetattr(fd, termios.TCSADRAIN, old)
+ return String(ch.decode("utf-8"))
+
+GLOBALS.register("read-char", Builtin(interpretReadChar, []))
def interpretAppend(symbol, args, env):
lst = args[0]