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") checkArg(1, history, "table", "nil")
history = history or {} history = history or {}
table.insert(history, "") 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 offset = term.getCursor() - 1
local scrollX, scrollY = 0, #history - 1 local scrollX, scrollY = 0, #history - 1
local cursorX = 1 local cursorX = 1
local hintCache = (type(hint)=="table" and #hint > 1)and hint
local selectedHint = 0
local function getCursor() local function getCursor()
return cursorX, 1 + scrollY return cursorX, 1 + scrollY
end end
@ -255,45 +251,48 @@ function term.read(history, dobreak, hint, prompt)
end end
local function tab() local function tab()
local after = hint(line()) if not hintCache then
if type(after) == "string" 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() local _, cby = getCursor()
history[cby] = after history[cby] = hintCache[selectedHint] or ""
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
end end
redraw() redraw()
ende() ende()
end end
local function cleanHint()
if type(hint) ~= "table" then
hintCache = nil
end
end
local function onKeyDown(char, code) local function onKeyDown(char, code)
term.setCursorBlink(false) term.setCursorBlink(false)
if code == keyboard.keys.back then if code == keyboard.keys.back then
if left() then delete() end if left() then delete() end cleanHint()
elseif code == keyboard.keys.delete then elseif code == keyboard.keys.delete then
delete() delete()cleanHint()
elseif code == keyboard.keys.left then elseif code == keyboard.keys.left then
left() left()
elseif code == keyboard.keys.right then elseif code == keyboard.keys.right then
right() right()
elseif code == keyboard.keys.home then elseif code == keyboard.keys.home then
home() home()cleanHint()
elseif code == keyboard.keys["end"] then elseif code == keyboard.keys["end"] then
ende() ende()cleanHint()
elseif code == keyboard.keys.up then elseif code == keyboard.keys.up then
up() up()
elseif code == keyboard.keys.down then 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 if cby ~= #history then -- bring entry to front
history[#history] = line() history[#history] = line()
table.remove(history, cby) table.remove(history, cby)
cleanHint()
end end
return true, history[#history] .. "\n" return true, history[#history] .. "\n"
elseif keyboard.isControlDown() and code == keyboard.keys.d then elseif keyboard.isControlDown() and code == keyboard.keys.d then
@ -317,6 +317,7 @@ function term.read(history, dobreak, hint, prompt)
return true, nil return true, nil
elseif not keyboard.isControl(char) then elseif not keyboard.isControl(char) then
insert(unicode.char(char)) insert(unicode.char(char))
cleanHint()
end end
term.setCursorBlink(true) term.setCursorBlink(true)
term.setCursorBlink(true) -- force toggle to caret term.setCursorBlink(true) -- force toggle to caret