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

[Lua][Help]Text wrapping when typing.

Started by ChaddJackson12, 05 December 2012 - 04:02 PM
ChaddJackson12 #1
Posted 05 December 2012 - 05:02 PM
I would like to be able to wrap input from a user, instead of having it on one line and going on forever.

Like when you print something and it word wraps, I would like to be able to have them type data that would be word wrapped, and depending on the size of the terminal… Is there a way to do this using read() or something?
Kingdaro #2
Posted 05 December 2012 - 05:20 PM
I don't know of a way to do this with read(), but as far as I know, print() word wraps the string given. Theoretically, you could use os.pullEvent() to create your own read().


local function wrapRead()
  local input = ''

  while true do
    term.clear()
    term.setCursorPos(1,1)
    print(input)

    local ev, p1 = os.pullEvent()
    if ev == 'char' then
      input = input..p1
    elseif ev == 'key' then
      if p1 == keys.backspace then
        input = input:sub(1, #input-1)
      elseif p1 == keys.enter then
	break
      end
    end
  end

  return input
end

Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
ChaddJackson12 #3
Posted 05 December 2012 - 05:22 PM
I don't know of a way to do this with read(), but as far as I know, print() word wraps the string given. Theoretically, you could use os.pullEvent() to create your own read().


local function wrapRead()
  local input = ''

  while true do
	term.clear()
	term.setCursorPos(1,1)
	print(input)

	local ev, p1 = os.pullEvent()
	if ev == 'char' then
	  input = input..p1
	elseif ev == 'key' then
	  if p1 == keys.backspace then
		input = input:sub(1, #input-1)
	  elseif p1 == keys.enter then
	break
	  end
	end
  end

  return input
end

Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
Alright, thanks, I will try it out!
ChaddJackson12 #4
Posted 05 December 2012 - 05:40 PM
I don't know of a way to do this with read(), but as far as I know, print() word wraps the string given. Theoretically, you could use os.pullEvent() to create your own read().


local function wrapRead()
  local input = ''

  while true do
	term.clear()
	term.setCursorPos(1,1)
	print(input)

	local ev, p1 = os.pullEvent()
	if ev == 'char' then
	  input = input..p1
	elseif ev == 'key' then
	  if p1 == keys.backspace then
		input = input:sub(1, #input-1)
	  elseif p1 == keys.enter then
	break
	  end
	end
  end

  return input
end

Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
Alright, thanks, I will try it out!
Seems like it is just what I was looking for, thanks!