Zulip Chat Archive
Stream: new members
Topic: How to access raw terminal mode in Lean?
Srayan Jana (Sep 10 2025 at 04:30):
I was working on an ASCII rogue like, but I got frustrated because I wanted to use raw terminal mode so I could read inputs without waiting for a newline. However, I couldn't figure out how to set it up. Any ideas?
Srayan Jana (Sep 10 2025 at 04:31):
Example of what I'm talking about (kinda) https://www.erlang.org/doc/apps/stdlib/terminal_interface.html
Anthony Wang (Sep 10 2025 at 05:26):
This works for me, on Linux:
def main : IO Unit := do
-- Read input immediately without waiting for newline
_ ← IO.Process.run {
cmd := "stty"
args := #["-F", "/dev/tty", "cbreak", "min", "1"],
}
-- Hide user input
_ ← IO.Process.run {
cmd := "stty"
args := #["-F", "/dev/tty", "-echo"],
}
let stdin ← IO.getStdin
while true do
let a ← stdin.read 1
match String.fromUTF8? a with
| some str => IO.println s!"Hello {str}"
| none => return
Anthony Wang (Sep 10 2025 at 05:27):
It's also possible to do this using ANSI escape sequences as suggested in #new members > How to clear terminal in Lean? but I think the method above is slightly more readable.
Srayan Jana (Sep 10 2025 at 15:46):
Would be nice to figure out how to do this on Windows, but this is a good start!
Anthony Wang (Sep 10 2025 at 15:50):
You could try running it in WSL 2, which should work without any changes to the code above.
Last updated: Dec 20 2025 at 21:32 UTC