Posted 26 March 2016 - 12:36 PM
How can I limit the read line?
I want to create a text box.
I want to create a text box.
function limitRead(limX, rChar)
term.setCursorBlink(true)
local origX, origY = term.getCursorPos()
local returnString = ""
while true do
local xPos, yPos = term.getCursorPos()
local event, p1, p2 = os.pullEvent()
if event == "char" then
returnString = returnString..p1
if not rChar then
if not limX then
write(p1)
else
if string.len(returnString) >= limX then
term.setCursorPos(origX, origY)
write(string.sub(returnString, (string.len(returnString)-limX)+1))
elseif string.len(returnString) < limX then
write(p1)
end
end
else
if not limX then
write(rChar)
else
if string.len(returnString) >= limX then
term.setCursorPos(origX, origY)
write(string.rep(rChar, limX))
elseif string.len(returnString) < limX then
write(rChar)
end
end
end
elseif event == "key" and p1 == 14 then --backspace
returnString = string.sub(returnString, 1, (string.len(returnString))-1)
term.setCursorPos(xPos-1,yPos)
write(" ")
term.setCursorPos(origX, origY)
if string.len(returnString) >= limX then
if not rChar then
write(string.sub(returnString, (string.len(returnString)-limX)+1))
else
write(string.rep(rChar,limX))
end
else
if not rChar then
write(returnString)
else
write(string.rep(rChar, string.len(returnString)))
end
end
elseif event == "key" and p1 == 28 then --enter
break
end
end
term.setCursorBlink(false)
return returnString
end
I think that was Cranium's