Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function read(mask,history,normal,selected)
- local continue = true
- local stopKey = keys.enter
- local backKey = 14
- local W,H = term.getSize()
- local mask = mask or nil
- local history = history or {}
- local normal = normal or colors.black
- local selected = selected or colors.gray
- local cursor = 1
- local str = {}
- local lastLength = 0
- local sX,sY = term.getCursorPos()
- function redraw()
- term.setCursorPos(sX,sY)
- term.write(string.rep(" ",lastLength))
- term.setCursorPos(sX,sY)
- if(#str==0) then
- term.setTextColor(selected)
- term.write("_")
- else
- for k,v in pairs(str) do
- term.setTextColor(normal)
- if(k == cursor) then
- term.setTextColor(selected)
- end
- term.write(v)
- end
- end
- lastLength = W
- end
- redraw()
- while continue do
- local evt = {os.pullEvent()}
- if(evt[1] == "char") then
- str[#str+1] = evt[2]
- cursor = #str
- redraw()
- elseif(evt[1] == "key") then
- if(evt[2] == backKey) then
- str[cursor] = nil
- cursor = cursor-1
- if(cursor>#str) then cursor = #str end
- if(cursor<1) then cursor = 1 end
- redraw()
- elseif(evt[2] == keys.left) then
- if(cursor > 1) then cursor = cursor-1 end
- redraw()
- elseif(evt[2] == keys.right) then
- if(cursor < #str) then cursor = cursor+1 end
- redraw()
- elseif(evt[2] == keys.enter) then
- continue = false
- end
- end
- end
- return table.concat(str,"")
- end
Advertisement
Add Comment
Please, Sign In to add comment