Jump to content




Limit read()


  • You cannot reply to this topic
5 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 26 March 2016 - 12:36 PM

How can I limit the read line?
I want to create a text box.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 March 2016 - 01:17 PM

Create a window, redirect term to that, then call read.

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 26 March 2016 - 04:52 PM

It works thanks.

#4 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 26 March 2016 - 10:11 PM

You could use something that I found a little while ago on the forums from someone I can't remember his name and this works how you like but just on the one line and limits the text, code:
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

Edited by DannySMc, 26 March 2016 - 10:12 PM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 26 March 2016 - 10:21 PM

I think that was Cranium's

#6 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 27 March 2016 - 01:29 AM

View PostKingofGamesYami, on 26 March 2016 - 10:21 PM, said:

I think that was Cranium's

Ahh yes it was! Sorry could never remember the name, I just know it was the most useful and lost code I needed xD





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users