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

Scrolling textbox function

Started by InputUsername, 05 February 2013 - 04:37 AM
InputUsername #1
Posted 05 February 2013 - 05:37 AM
Hello folks, here's a simple function I made. It's a scrolling textbox with configurable width.

Warning: has not been fully tested (yet)! Tested, should work as expected.

Code:

Spoiler


local function maxRead(nWidth)
  nWidth = nWidth and nWidth-1 or 10

  term.setCursorBlink(true)
  local X_,Y_ = term.getCursorPos() --get original cursor X,Y

  local String = ""

  while true do --main loop
	local event,p1 = os.pullEvent() --pull event

	if event == "char" then --character was pressed
	  String = String..p1
	elseif event == "key" then --key was pressed
	  if p1 == 28 then --return/enter
		break
	  elseif p1 == 14 then --backspace
		String = String:sub(1,#String-1)
	  end
	end

	term.setCursorPos(X_,Y_)
	term.write(string.rep(" ",nWidth+1))
	term.setCursorPos(X_,Y_)
	term.write(String:sub(((#String - nWidth) >= 1 and #String - nWidth) or 1,#String))
  end

  term.setCursorBlink(false)
  term.setCursorPos(X_,Y_+1) --return cursor to original X, original Y + 1

  return String --return the string
end

or

eJGVpdFv
Edited on 02 January 2014 - 03:12 AM
theoriginalbit #2
Posted 05 February 2013 - 02:55 PM
having a default char would be nice, so you can have max length and password.
anonimo182 #3
Posted 05 February 2013 - 03:27 PM
Having support for up/down/left/right keys is also good.

Sincerely, you can accomplish this by seeing the code of read() and changing the line local sx, sy = term.getSize() into local sx, sy = maxX, 1
GopherAtl #4
Posted 06 February 2013 - 03:13 AM
anonimo, that would limit the area it displays the entered text in before scrolling it, not the length of the text you're allowed to enter.
anonimo182 #5
Posted 07 February 2013 - 09:45 AM
Oh, sorry for that…
InputUsername #6
Posted 10 February 2013 - 03:10 AM
anonimo, that would limit the area it displays the entered text in before scrolling it, not the length of the text you're allowed to enter.

That's actually what my function is supposed to do; it's a 'scrolling' text box ;)/> it does not limit the length of text to be entered, although I could add that option.
GopherAtl #7
Posted 10 February 2013 - 06:23 AM
oh! sorry, haha. Never mind, anonimo, carry on :)/>