Please help me with this. I am trying to make a "read()" thing that word wraps. I have figured that part out thanks to some other help on the forums. The problem is:

When the words reach the end of the screen, not only does it move a word that is too long to the next line, but the remaining part of the word still remains up at the line it entered down from. To kind of give you an idea:

Line 1: "Blah Blah Blah Blah Blah Blah Bl" <– The word is too long…
Line 2: "Blah" <– The word returned from above.

I was wondering how I could possibly clear the above word when a word gets longer than the width of the screen and needs to return to the next line. I was thinking with something that detects spaces, and counts the characters from the last space, but I have no idea how to do this.

If you can, please help. Thanks in advance.

The code for the word wrap "read()":


Message = ""
while true do
  term.clear()
  term.setCursorPos(1, 1)
  term.setCursorBlink(true)
  write(Message)

  local event, key = os.pullEvent()
  if event == "char" then
    Message = Message .. key
  elseif event = "key" then
    if key == 14 then
      Message = Message:sub(1, #Message - 1)
      x, y = term.getCursorPos()
      term.setCursorPos(w - 1, h)
      write(" ")
      term.setCursorPos(w, h)
    elseif key == 28
      term.setCursorBlink(false)
      break
    end
  end
end