Changed term.read hint behaviour

When hint function returns table, It no longer writes it to screen. The result is cached, and user may view hintd by pressing tab key repetitively.
This commit is contained in:
Łukasz Magiera 2014-08-07 17:13:33 +02:00
parent 61c1f02fe0
commit c791bcc00c

View File

@ -105,17 +105,13 @@ function term.read(history, dobreak, hint, prompt)
checkArg(1, history, "table", "nil")
history = history or {}
table.insert(history, "")
if type(prompt) == "function" then
pcall(prompt)
elseif type(prompt) == "string" then
term.write(prompt)
end
local offset = term.getCursor() - 1
local scrollX, scrollY = 0, #history - 1
local cursorX = 1
local hintCache = (type(hint)=="table" and #hint > 1)and hint
local selectedHint = 0
local function getCursor()
return cursorX, 1 + scrollY
end
@ -255,45 +251,48 @@ function term.read(history, dobreak, hint, prompt)
end
local function tab()
local after = hint(line())
if type(after) == "string" then
if not hintCache then
if type(hint) == "function" then
local h = hint(line())
if type(h) == "string" then
local _, cby = getCursor()
history[cby] = after
elseif type(h) == "table" and #h > 0 then
hintCache = h
selectedHint = 1
local _, cby = getCursor()
history[cby] = hintCache[selectedHint] or ""
end
end
else
selectedHint = (selectedHint+1)<=#hintCache and (selectedHint+1) or 1
local _, cby = getCursor()
history[cby] = after
elseif type(after) == "table" then
term.write("\n")
for _, name in pairs(after) do
term.write(name .. " ", true)
end
term.write("\n")
if type(prompt) == "function" then pcall(prompt)
elseif type(prompt) == "string" then term.write(prompt)end
elseif type(after) == "function" then
term.write("\n")
for name in after do --This is possibly not the best solution
term.write(name .. " ", true)
end
term.write("\n")
if type(prompt) == "function" then pcall(prompt)
elseif type(prompt) == "string" then term.write(prompt)end
history[cby] = hintCache[selectedHint] or ""
end
redraw()
ende()
end
local function cleanHint()
if type(hint) ~= "table" then
hintCache = nil
end
end
local function onKeyDown(char, code)
term.setCursorBlink(false)
if code == keyboard.keys.back then
if left() then delete() end
if left() then delete() end cleanHint()
elseif code == keyboard.keys.delete then
delete()
delete()cleanHint()
elseif code == keyboard.keys.left then
left()
elseif code == keyboard.keys.right then
right()
elseif code == keyboard.keys.home then
home()
home()cleanHint()
elseif code == keyboard.keys["end"] then
ende()
ende()cleanHint()
elseif code == keyboard.keys.up then
up()
elseif code == keyboard.keys.down then
@ -305,6 +304,7 @@ function term.read(history, dobreak, hint, prompt)
if cby ~= #history then -- bring entry to front
history[#history] = line()
table.remove(history, cby)
cleanHint()
end
return true, history[#history] .. "\n"
elseif keyboard.isControlDown() and code == keyboard.keys.d then
@ -317,6 +317,7 @@ function term.read(history, dobreak, hint, prompt)
return true, nil
elseif not keyboard.isControl(char) then
insert(unicode.char(char))
cleanHint()
end
term.setCursorBlink(true)
term.setCursorBlink(true) -- force toggle to caret