From 21f5ab533519171fd7666680e07d69f367361f97 Mon Sep 17 00:00:00 2001 From: Cormac Shannon Date: Thu, 9 Oct 2025 16:50:43 +0100 Subject: [PATCH] Enhance REPL user experience by adding Ctrl+L to clear the screen and improving Ctrl+C interruption handling --- luash | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/luash b/luash index 8afb447..e3fa995 100755 --- a/luash +++ b/luash @@ -213,7 +213,7 @@ function start_repl() -- Main REPL loop print("🚀 Luash Interactive Shell") print("Type Lua expressions, use $VAR for env vars, `commands` for shell") - print("Special commands: .help .exit .clear .history") + print("Special commands: .help .exit .clear .history (Ctrl+L to clear, Ctrl+D to exit)") print("") while true do @@ -221,13 +221,30 @@ function start_repl() io.write(prompt) io.flush() - local line = io.read("*line") + -- Safely read a line; handle Ctrl+C gracefully + local ok, line = pcall(io.read, "*line") + if not ok then + print("\nInterrupted (Ctrl+C)") + break + end if not line then -- EOF (Ctrl+D) print("\nGoodbye!") break end + -- Handle Ctrl+L (form feed) to clear screen + if line:find(string.char(12), 1, true) then + repl.buffer = "" + io.write("\27[2J\27[H") -- Clear screen and move cursor to top + io.flush() + print("🚀 Luash Interactive Shell") + print("Type Lua expressions, use $VAR for env vars, `commands` for shell") + print("Special commands: .help .exit .clear .history (Ctrl+L to clear, Ctrl+D to exit)") + print("") + goto continue + end + line = trim(line) -- Handle special dot commands @@ -323,6 +340,7 @@ Multi-line input supported for functions, loops, etc.]]) end end end + ::continue:: end end @@ -336,6 +354,7 @@ if filename == "-h" or filename == "--help" then return elseif not filename or filename == "-i" or filename == "--interactive" then start_repl() + return end local file = io.open(filename, "r")