每个实现对此的支持都不同。
您可能想使用辅助库,例如iolib.termios https://github.com/marsijanin/iolib.termios or cl-魅力 https://github.com/HiTECNOLOGYs/cl-charms(libcurses 的接口)如果您想要在不同实现之上有一个可移植层。
SBCL
我找到了一个讨论话题 http://article.gmane.org/gmane.lisp.steel-bank.general/1603有关 SBCL 的信息,以下是该实现的代码,来自 Richard M. Kreuter:
(require :sb-posix)
(defun echo-off ()
(let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
(setf (sb-posix:termios-lflag tm)
(logandc2 (sb-posix:termios-lflag tm) sb-posix:echo))
(sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))
(defun echo-on ()
(let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
(setf (sb-posix:termios-lflag tm)
(logior (sb-posix:termios-lflag tm) sb-posix:echo))
(sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))
所以,终于有机会谈论PROG2 http://clhs.lisp.se/Body/m_prog1c.htm:
(defun read-silently ()
(prog2
(echo-off)
(read-line sb-sys:*tty*)
(echo-on)))
但是,您可能希望确保在展开堆栈时始终重置回显,并在输入内容之前清除输入:
(defun read-silently ()
(echo-off)
(unwind-protect
(progn
(clear-input sb-sys:*tty*)
(read-line sb-sys:*tty*))
(echo-on)))
CL-魅力
这是使用 libcurse 的替代方案。以下足以进行简单的测试工作。
(defun read-silently ()
(let (input)
(charms:with-curses ()
(charms:disable-echoing)
(charms:enable-raw-input)
(clear-input *terminal-io*)
(setf input (read-line *terminal-io*))
(charms:disable-raw-input)
(charms:enable-echoing))
input))
此外,使用 libcurse 可能会帮助您实现一个漂亮的刽子手控制台游戏。