The thing I want to prevent is this:
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Stop read() from clearing end of line
Started by Cranium, 02 November 2012 - 02:48 PMPosted 02 November 2012 - 03:48 PM
I have some clever little screens on my upcoming program, but I don't know how I can limit read() do just write the input in a certain defined area. I though about using os.pullEvent to create a custom function, but wanted to know if this is already implemented in lua.
The thing I want to prevent is this:
The thing I want to prevent is this:
Posted 02 November 2012 - 03:53 PM
Copy read() and modify it.
Posted 02 November 2012 - 03:55 PM
How do I find read()? Where is it stored in the API's?
Posted 02 November 2012 - 04:05 PM
It's not the most elegant, or efficient for that matter, of a solution, but it works for what you need. I wrote this a long while ago so I can't really provide much support for it.
Sorry for the spacing, but when I post from pastebin to the forums it usually messes the indentation up horribly.
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
Sorry for the spacing, but when I post from pastebin to the forums it usually messes the indentation up horribly.
Posted 02 November 2012 - 04:11 PM
I think you actually sent me this a long time ago, but I lost it :D/>/>It's not the most elegant, or efficient for that matter, of a solution, but it works for what you need. I wrote this a long while ago so I can't really provide much support for it.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
Sorry for the spacing, but when I post from pastebin to the forums it usually messes the indentation up horribly.
nLength is the width i want it to stop at, right?
Posted 02 November 2012 - 04:19 PM
Yes. cReplaceChar will be what you want to replace the text typed with. For passwords, for example, you can call
limitRead(14, '*')
for a 14 character password that looks like this when you type it in:
**************
Posted 02 November 2012 - 04:24 PM
So it will just stop at that amount of characters? Or can I call limitRead(10, "*"), enter a 20 character long string, and it will return the entire 20 characters?
Posted 02 November 2012 - 04:50 PM
That call would only return a 10 character string. You can have an unlimited number of characters by using:
limitRead(-1, '*')
Posted 02 November 2012 - 04:50 PM
read() is in bios.lua