Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function limitRead( nlim, cOverlay)
- cOverlay=cOverlay or nil
- local px,py = term.getCursorPos()
- local txt = ""
- local boxOffset = 0
- local strPos = 1
- term.setCursorBlink(true)
- while true do
- e,p1 = os.pullEvent()
- if e=="key" then
- local key = p1
- if key==keys.left then
- if strPos>1 then
- strPos=strPos-1
- if strPos-boxOffset==0 then
- boxOffset=boxOffset-1
- end
- end
- elseif key==keys.right then
- if strPos<=#txt then
- strPos=strPos+1
- if strPos-boxOffset>nlim then
- boxOffset=boxOffset+1
- end
- end
- elseif key==keys.enter then
- break
- elseif key==keys.backspace then
- if strPos~=1 then
- txt=txt:sub(1,strPos-2)..txt:sub(strPos,#txt)
- strPos=strPos-1
- if strPos-boxOffset==0 then
- boxOffset=boxOffset-1
- end
- end
- elseif key==keys.delete then
- if strPos-1~=#txt then
- txt=txt:sub(1,strPos-1)..txt:sub(strPos+1,#txt)
- end
- end
- elseif e=="char" then
- txt = txt:sub(1,strPos-1)..p1..txt:sub(strPos,#txt)
- strPos=strPos+1
- if strPos-1-boxOffset>=nlim then
- boxOffset=boxOffset+1
- end
- end
- term.setCursorPos(px,py)
- write(string.rep(" ",nlim))
- term.setCursorPos(px,py)
- --print(cOverlay==nil and "Yep" or "Nope")
- --read()
- if cOverlay==nil then
- -- term.setTextColor(colors.white)
- write(txt:sub(boxOffset+1,boxOffset+nlim))
- -- write(txt)
- else
- write(string.rep(cOverlay,#txt):sub(boxOffset+1,nlim+boxOffset))
- end
- term.setCursorPos(px+strPos-1-boxOffset,py)
- end
- term.setCursorBlink(false)
- return txt
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement