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

read() Only type a certain number of characters.

Started by os.reboot(), 22 July 2012 - 09:49 AM
os.reboot() #1
Posted 22 July 2012 - 11:49 AM
I need user input in a program but I want the user to only be able to type in 44 characters maximum.
Once I do this I'll make billions on this server for being the first to make a chat program and I may also give away the code once the program is done because it's actually quite good in my opinion.

Please help!
KevinW1998 #2
Posted 22 July 2012 - 12:05 PM
You can copy read() from the bios.lua and modify it!
Kolpa #3
Posted 22 July 2012 - 12:25 PM
I need user input in a program but I want the user to only be able to type in 44 characters maximum.
Once I do this I'll make billions on this server for being the first to make a chat program and I may also give away the code once the program is done because it's actually quite good in my opinion.

Please help!
u could create your own read function i did this once for a password lock it goes like this :



function maxread(x)
count = 0
txt =""
repeat
id,chr = os.pullEvent()
if id == "char" then
term.write(chr)
txt = txt..chr
count = count + 1
end

if id == "key" and chr == 28 then
return txt
end

until count == x

while true do
id,chr = os.pullEvent()

if id == "key" and chr == 28 then
return txt
end

end

end
note that u cant delete text here
Ponder #4
Posted 22 July 2012 - 12:46 PM
note that u cant delete text here

You can, it's just a bit more complicated. Instead of writing each character on its on the screen, put all the characters you have received so far in a string and with each iteration of the loop, set the cursor to the beginning of the line (or where ever you want it to be) and write the whole string. If you receive a backspace delete the last character of the string.
Kolpa #5
Posted 22 July 2012 - 01:08 PM
note that u cant delete text here

You can, it's just a bit more complicated. Instead of writing each character on its on the screen, put all the characters you have received so far in a string and with each iteration of the loop, set the cursor to the beginning of the line (or where ever you want it to be) and write the whole string. If you receive a backspace delete the last character of the string.
i said that my function doesn't allow that, not that its not possible
os.reboot() #6
Posted 22 July 2012 - 01:42 PM
I need user input in a program but I want the user to only be able to type in 44 characters maximum.
Once I do this I'll make billions on this server for being the first to make a chat program and I may also give away the code once the program is done because it's actually quite good in my opinion.

Please help!
u could create your own read function i did this once for a password lock it goes like this :



function maxread(x)
count = 0
txt =""
repeat
id,chr = os.pullEvent()
if id == "char" then
term.write(chr)
txt = txt..chr
count = count + 1
end

if id == "key" and chr == 28 then
return txt
end

until count == x

while true do
id,chr = os.pullEvent()

if id == "key" and chr == 28 then
return txt
end

end

end
note that u cant delete text here

Thanks for your help, that function you gave me did work but my chat system does not want to work with it, so I've decided to continue using the normal read() but instead it will deny sending it to other people if the string exceeds 44 characters.

Sooooooo… My next question is how to find out if a string exceeds 44 of characters.
KevinW1998 #7
Posted 22 July 2012 - 01:59 PM
Copy the read() function and modify it with an if

function read( _sReplaceChar, _tHistory )    
    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()
        local nScroll = 0
        if sx + nPos >= w then
            nScroll = (sx + nPos) - w
        end
            
        term.setCursorPos( sx, sy )
        term.write( string.rep(" ", w - sx + 1) )
        term.setCursorPos( sx, sy )
        if _sReplaceChar then
            term.write( string.rep(_sReplaceChar, 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 sLine <= 44 then
	    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 == 28 then
                -- Enter
                break
                
            elseif param == 203 then
                -- Left
                if nPos > 0 then
                    nPos = nPos - 1
                    redraw()
                end
                
            elseif param == 205 then
                -- Right                
                if nPos < string.len(sLine) then
                    nPos = nPos + 1
                    redraw()
                end
            
            elseif param == 200 or param == 208 then
			    -- Up or down
                if _tHistory then
                    if param == 200 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 == 14 then
                -- Backspace
                if nPos > 0 then
                    sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
                    nPos = nPos - 1                    
                    redraw()
                end
            end
	    end
        end
    end
    
    term.setCursorBlink( false )
    term.setCursorPos( w + 1, sy )
    print()
    
    return sLine
end

os.reboot() #8
Posted 22 July 2012 - 02:37 PM
My chat system didn't work with this read modification either. Is there any other ways?
Without read() modifications preferrably.
Kolpa #9
Posted 22 July 2012 - 06:09 PM
My chat system didn't work with this read modification either. Is there any other ways?
Without read() modifications preferrably.

text = read()
if text:len() <= 44 then
do something
end
Grim Reaper #10
Posted 22 July 2012 - 09:59 PM
My chat system didn't work with this read modification either. Is there any other ways?
Without read() modifications preferrably.

text = read()
if text:len() <= 44 then
do something
end

The above code would allow you to input any number of characters because read is constantly receiving input until 'enter' is pressed, jumping to the next line in your program to be executed. This would mean that you would have to keep getting input over and over until the user entered something within the limit.

I wrote up a quick solution that works and is hopefully what you're looking for:


function limitRead( nLength, cReplaceChar )
  term.setCursorBlink( true )

  nLength = nLength or -1 -- -1 is unlimited
  sReturnString = ""

  xPos, yPos = term.getCursorPos()

  while true do
	   event, char = os.pullEvent()

	   if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length	  check

		if event == "char" then sReturnString = sReturnString .. char
		elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
		elseif event == "key" and char == 14 then -- Backspace
			term.setCursorPos( xPos, yPos )
			term.write( string.rep( " ", string.len( sReturnString ) ) )
			sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
			term.setCursorPos( xPos, yPos )

			if not cReplaceChar then term.write( sReturnString )
			else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
		end

		term.setCursorPos( xPos, yPos )
		term.write( string.rep( " ", string.len( sReturnString ) ) )
		term.setCursorPos( xPos, yPos )
		if not cReplaceChar then term.write( sReturnString )
		else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
	end
end
http://pastebin.com/gAw5X9bU

nLength is the maximum length and cReplaceChar is the character that will act as a mask for the input.

Unfortunately, there is no scrolling if you decide to enter input off of the page.

Hope I helped! :)/>/>
Kolpa #11
Posted 23 July 2012 - 12:33 PM
My chat system didn't work with this read modification either. Is there any other ways? Without read() modifications preferrably.
 text = read() if text:len() <= 44 then do something end 
The above code would allow you to input any number of characters because read is constantly receiving input until 'enter' is pressed, jumping to the next line in your program to be executed. This would mean that you would have to keep getting input over and over until the user entered something within the limit. I wrote up a quick solution that works and is hopefully what you're looking for:
 function limitRead( nLength, cReplaceChar ) term.setCursorBlink( true ) nLength = nLength or -1 -- -1 is unlimited sReturnString = "" xPos, yPos = term.getCursorPos() while true do event, char = os.pullEvent() if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check if event == "char" then sReturnString = sReturnString .. char elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter elseif event == "key" and char == 14 then -- Backspace term.setCursorPos( xPos, yPos ) term.write( string.rep( " ", string.len( sReturnString ) ) ) sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 ) term.setCursorPos( xPos, yPos ) if not cReplaceChar then term.write( sReturnString ) else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end end term.setCursorPos( xPos, yPos ) term.write( string.rep( " ", string.len( sReturnString ) ) ) term.setCursorPos( xPos, yPos ) if not cReplaceChar then term.write( sReturnString ) else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end end end 
http://pastebin.com/gAw5X9bU nLength is the maximum length and cReplaceChar is the character that will act as a mask for the input. Unfortunately, there is no scrolling if you decide to enter input off of the page. Hope I helped! :)/>/>
Zzz u do read the whole thread before writing comments don't u ….