This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Crowdy199's profile picture

read

Started by Crowdy199, 26 February 2013 - 09:06 AM
Crowdy199 #1
Posted 26 February 2013 - 10:06 AM
is there a way i can limit how many characters u can type with read() and then it will automaticly go to the next line of code
SuicidalSTDz #2
Posted 26 February 2013 - 10:15 AM
remiX or Orwell could help you with this. This is kingdaro's code:


local function read()
  local input = ''
  local x,y = term.getCursorPos()
  term.setCursorBlink(true)
  repeat
	term.setCursorPos(x,y)
	term.write(input)
	local ev, p1 = os.pullEvent()
	if ev == 'char' then
	  if #input < 10 then
		input = input .. p1
	  end
	elseif ev == 'key' then
	  if p1 == keys.backspace then
		input = input:sub(1, #input - 1)
	  end
	end
  until ev == 'key' and p1 == keys.enter
  term.setCursorBlink(false)
  return input
end

YOU will have to find a way to set it to the next line since that is how we all work here on the CC forums ;)/>
LBPHacker #3
Posted 26 February 2013 - 10:15 AM
Yes, there is. But it's hardcore in my opinion.

Stolen from bios.lua. Third parameter is the character limit. If you don't know what the first two paramters are, just call readLimited(nil, nil, charNum)
Spoiler


function read( _sReplaceChar, _tHistory , _iCharLimit)
	term.setCursorBlink( true )

	local sLine = ""
	local nHistoryPos = nil
	local nPos = 0
	if _sReplaceChar then
		_sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
	end

	local w, h = term.getSize()
	local sx, sy = term.getCursorPos()	

	local function redraw( _sCustomReplaceChar )
		local nScroll = 0
		if sx + nPos >= w then
			nScroll = (sx + nPos) - w
		end

		term.setCursorPos( sx, sy )
		local sReplace = _sCustomReplaceChar or _sReplaceChar
		if sReplace then
			term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
		else
			term.write( string.sub( sLine, nScroll + 1 ) )
		end
		term.setCursorPos( sx + nPos - nScroll, sy )
	end

	while true do
		local sEvent, param = os.pullEvent()
		if sEvent == "char" then
			sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
			nPos = nPos + 1
			redraw()
            if _iCharLimit and _iCharLimit == #sLine then break end

		elseif sEvent == "key" then
			if param == keys.enter then
				-- Enter
				break

			elseif param == keys.left then
				-- Left
				if nPos > 0 then
					nPos = nPos - 1
					redraw()
				end

			elseif param == keys.right then
				-- Right				
				if nPos < string.len(sLine) then
					nPos = nPos + 1
					redraw()
				end

			elseif param == keys.up or param == keys.down then
				-- Up or down
				if _tHistory then
					redraw(" ");
					if param == keys.up then
						-- Up
						if nHistoryPos == nil then
							if #_tHistory > 0 then
								nHistoryPos = #_tHistory
							end
						elseif nHistoryPos > 1 then
							nHistoryPos = nHistoryPos - 1
						end
					else
						-- Down
						if nHistoryPos == #_tHistory then
							nHistoryPos = nil
						elseif nHistoryPos ~= nil then
							nHistoryPos = nHistoryPos + 1
						end						
					end

					if nHistoryPos then
						sLine = _tHistory[nHistoryPos]
						nPos = string.len( sLine )
					else
						sLine = ""
						nPos = 0
					end
					redraw()
				end
			elseif param == keys.backspace then
				-- Backspace
				if nPos > 0 then
					redraw(" ");
					sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
					nPos = nPos - 1					
					redraw()
				end
			elseif param == keys.home then
				-- Home
				nPos = 0
				redraw()		
			elseif param == keys.delete then
				if nPos < string.len(sLine) then
					redraw(" ");
					sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )				
					redraw()
				end
			elseif param == keys["end"] then
				-- End
				nPos = string.len(sLine)
				redraw()
			end
		end
	end

	term.setCursorBlink( false )
	term.setCursorPos( w + 1, sy )
	print()

	return sLine
end

EDIT: YAY ninja'd :D/>

EDIT2: BIG DERP - THIS IS FROM MY OS - GIVE ME 5 MINS…

EDIT3: Ok, solved… That really works…
SuicidalSTDz #4
Posted 26 February 2013 - 10:17 AM
Yes, there is. But it's hardcore in my opinion.

Stolen from bios.lua. Third parameter is the character limit. If you don't know what the first two paramters are, just call readLimited(nil, nil, charNum)
Spoiler


local readLimited = function( _sReplaceChar, _tHistory, _iCharLimit )
	local videoSession = video.getSessionByCoroutineID(coroutine.id())
	term.setCursorBlink( true )

	local sLine = ""
	local nHistoryPos = nil
	local nPos = 0
	if _sReplaceChar then
		_sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
	end

	local w, h = term.getSize()
	local sx, sy = term.getCursorPos()
	local size = {video.sessions.getSize(videoSession)}

	local function redraw( _sCustomReplaceChar )
		local nScroll = 0
		if sx + nPos >= w then
			nScroll = (sx + nPos) - w
		end

		term.setCursorPos( sx, sy )
		local sReplace = _sCustomReplaceChar or _sReplaceChar
		if sReplace then
			term.write( string.rep(sReplace, string.len(sLine) - nScroll) , true)
		else
			term.write( string.sub( sLine, nScroll + 1 ) , true)
		end
		if bNaturalCall then if video.sessions.getStatusBit(videoSession, 1) then video.update(videoSession, size[1], sy + size[2] - 1, size[3], sy + size[2] - 1) end end
		term.setCursorPos( sx + nPos - nScroll, sy )
	end

	while true do
		local sEvent, param = os.pullEvent()
		if sEvent == "char" then
			sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
			nPos = nPos + 1
			redraw()

		elseif sEvent == "key" then
			if param == keys.enter or (_iCharLimit and _iCharLimit <= #sLine) then
				-- Enter
				break

			elseif param == keys.left then
				-- Left
				if nPos > 0 then
					nPos = nPos - 1
					redraw()
				end

			elseif param == keys.right then
				-- Right				
				if nPos < string.len(sLine) then
					nPos = nPos + 1
					redraw()
				end

			elseif param == keys.up or param == keys.down then
				-- Up or down
				if _tHistory then
					redraw(" ");
					if param == keys.up then
						-- Up
						if nHistoryPos == nil then
							if #_tHistory > 0 then
								nHistoryPos = #_tHistory
							end
						elseif nHistoryPos > 1 then
							nHistoryPos = nHistoryPos - 1
						end
					else
						-- Down
						if nHistoryPos == #_tHistory then
							nHistoryPos = nil
						elseif nHistoryPos ~= nil then
							nHistoryPos = nHistoryPos + 1
						end						
					end

					if nHistoryPos then
						sLine = _tHistory[nHistoryPos]
						nPos = string.len( sLine )
					else
						sLine = ""
						nPos = 0
					end
					redraw()
				end
			elseif param == keys.backspace then
				-- Backspace
				if nPos > 0 then
					redraw(" ");
					sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
					nPos = nPos - 1					
					redraw()
				end
			elseif param == keys.home then
				-- Home
				nPos = 0
				redraw()		
			elseif param == keys.delete then
				if nPos < string.len(sLine) then
					redraw(" ");
					sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )				
					redraw()
				end
			elseif param == keys["end"] then
				-- End
				nPos = string.len(sLine)
				redraw()
			end
		end
	end

	term.setCursorBlink( false )
	term.setCursorPos( w + 1, sy )
	print()

	return sLine
end

EDIT: YAY ninja'd :D/>
Lol, that works as well :D/>

EDIT: Hmm, spelled Kingdaro's name wrong again?!

Looking at bios.lua hurts my eyes…