Jump to content




[Lua][Help]Text wrapping when typing.

utility lua help

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

#1 ChaddJackson12

  • Members
  • 264 posts

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?

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#3 ChaddJackson12

  • Members
  • 264 posts

Posted 05 December 2012 - 05:22 PM

View PostKingdaro, on 05 December 2012 - 05:20 PM, said:

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!

#4 ChaddJackson12

  • Members
  • 264 posts

Posted 05 December 2012 - 05:40 PM

View PostChaddJackson12, on 05 December 2012 - 05:22 PM, said:

View PostKingdaro, on 05 December 2012 - 05:20 PM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users