Hint function can now return tables and iterators

Returning table or iterator will display set of available completion options. When user will return table, he/she should also provide prompt function for (re)drawing prompt if one is used is his/her program
This commit is contained in:
Łukasz Magiera 2014-08-05 14:50:25 +02:00
parent 47c95a62d8
commit 62a507ed87

View File

@ -101,7 +101,7 @@ function term.isAvailable()
return component.isAvailable("gpu") and component.isAvailable("screen") return component.isAvailable("gpu") and component.isAvailable("screen")
end end
function term.read(history, dobreak, hint) 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, "")
@ -109,6 +109,10 @@ function term.read(history, dobreak, hint)
local scrollX, scrollY = 0, #history - 1 local scrollX, scrollY = 0, #history - 1
local cursorX = 1 local cursorX = 1
if type(prompt) == "function" then
pcall(prompt)
end
local function getCursor() local function getCursor()
return cursorX, 1 + scrollY return cursorX, 1 + scrollY
end end
@ -252,8 +256,15 @@ function term.read(history, dobreak, hint)
if type(after) == "string" then if type(after) == "string" then
local _, cby = getCursor() local _, cby = getCursor()
history[cby] = after history[cby] = after
elseif type(after) == "table" or type(after) == "function" then
term.write("\n")
for _, v in type(after) == "table" and pairs(after) or (function()return _,after end) do
term.write(name .. " ", true)
end
term.write("\n")
if type(prompt) == "function" then pcall(prompt)end
end end
redraw() --hint might have printed sth redraw()
ende() ende()
end end