Jump to content




Stop read() from clearing end of line


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

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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:
Posted Image

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 02 November 2012 - 03:53 PM

Copy read() and modify it.

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 02 November 2012 - 03:55 PM

How do I find read()? Where is it stored in the API's?

#4 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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.

#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 02 November 2012 - 04:11 PM

 Grim Reaper, on 02 November 2012 - 04:05 PM, said:

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?

#6 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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:
**************


#7 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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?

#8 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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, '*')


#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 02 November 2012 - 04:50 PM

read() is in bios.lua





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users