Enhance REPL user experience by adding Ctrl+L to clear the screen and improving Ctrl+C interruption handling
This commit is contained in:
23
luash
23
luash
@@ -213,7 +213,7 @@ function start_repl()
|
|||||||
-- Main REPL loop
|
-- Main REPL loop
|
||||||
print("🚀 Luash Interactive Shell")
|
print("🚀 Luash Interactive Shell")
|
||||||
print("Type Lua expressions, use $VAR for env vars, `commands` for 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("")
|
print("")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
@@ -221,13 +221,30 @@ function start_repl()
|
|||||||
io.write(prompt)
|
io.write(prompt)
|
||||||
io.flush()
|
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)
|
if not line then -- EOF (Ctrl+D)
|
||||||
print("\nGoodbye!")
|
print("\nGoodbye!")
|
||||||
break
|
break
|
||||||
end
|
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)
|
line = trim(line)
|
||||||
|
|
||||||
-- Handle special dot commands
|
-- Handle special dot commands
|
||||||
@@ -323,6 +340,7 @@ Multi-line input supported for functions, loops, etc.]])
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
::continue::
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -336,6 +354,7 @@ if filename == "-h" or filename == "--help" then
|
|||||||
return
|
return
|
||||||
elseif not filename or filename == "-i" or filename == "--interactive" then
|
elseif not filename or filename == "-i" or filename == "--interactive" then
|
||||||
start_repl()
|
start_repl()
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local file = io.open(filename, "r")
|
local file = io.open(filename, "r")
|
||||||
|
|||||||
Reference in New Issue
Block a user