when readln in console like guess.lua, it does not support UpDown Arrow history, quite inconvenient
readln history
even Home or End , Left or Right, neither supported?
maybe use console.readchar to detect Home or other special key
I see that readchar recognize special key as:
Home = "G"
End = "O"
Up = "H"
Down = "P"
Left = "K"
Right = "M"
Hi Ruby,
You are right, readln support only basic input from the console.
It's on my todo list to provide richer keyboard input, while preserving UTF8 correctness.
For the up/down history, or even HOME / END and other special keys it's possible to implement it using Lua and the [Login to see the link] function.
Thank you for your feedback !
my try. ugly but work
error = function(s)
console.writecolor("red", s.."\n")
end
run = function(s)
local f = load(s)
local f2 = load('print('..s..')')
local result, reason = pcall(f)
if result == false then
local result2, reason2 = pcall(f2)
if result2 == false then
error(reason)
end
end
end
Home = "G"
End = "O"
Up = "H"
Down = "P"
Left = "K"
Right = "M"
Delete = "S"
Back = "\b"
Esc = "\027"
Return = "\r"
Enter = Return
function get_line()
text = ""
repeat
local key, special = console.readchar()
if special == true then
if key == Home then
console.write("\r"..string.rep (" ", 120).."\r")
text = ""
elseif key == Delete then
console.write('\b \b')
text = text:sub(1, -2)
end
else
if key == Esc then
console.write("\r"..string.rep (" ", 110).."\r")
text = ""
elseif key == Back then
console.write('\b \b')
text = text:sub(1, -2)
else
console.write(key)
text = text..key
end
end
until key == Enter
print()
return text
end
repeat
text = get_line()
run(text)
until text == Enter
I have reworked the console module to support history/Home/End/Arrows keys with console.readln() while preserving UTF8 compatibility with cmd.exe (a true nightmare as conhost UTF8 support is bugged on Windows !).
It will be available in next release, but will need feedback for Windows Vista / 7 / 8 compatibility