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

Stop read() from clearing end of line

Started by Cranium, 02 November 2012 - 02:48 PM
Cranium #1
Posted 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:
Lyqyd #2
Posted 02 November 2012 - 03:53 PM
Copy read() and modify it.
Cranium #3
Posted 02 November 2012 - 03:55 PM
How do I find read()? Where is it stored in the API's?
Grim Reaper #4
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.


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.
Cranium #5
Posted 02 November 2012 - 04:11 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.


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.
I think you actually sent me this a long time ago, but I lost it :D/>/>
nLength is the width i want it to stop at, right?
Grim Reaper #6
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:

**************
Cranium #7
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?
Grim Reaper #8
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, '*')
Lyqyd #9
Posted 02 November 2012 - 04:50 PM
read() is in bios.lua