Enhance REPL user experience by adding Ctrl+L to clear the screen and improving Ctrl+C interruption handling

This commit is contained in:
2025-10-09 16:50:43 +01:00
parent da979811dd
commit 21f5ab5335

23
luash
View File

@@ -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")