function write(text) -- Get the terminal size and the cursor position local w, h = term.getSize() local x, y = term.getCursorPos() -- Define the new line function -- It creates a new line (moves the cursor to the next line, and scrolls the terminal) local function newLine() -- If the next line the cursor will be on is less than the height of the terminal if y + 1 <= h then term.setCursorPos(1, y + 1) else -- Scroll the terminal screen term.setCursorPos(1, h) term.scroll(1) end x, y = term.getCursorPos() end -- Print the line with word wrapping while string.len(text) > 0 do -- Get the amount of whitespace (spaces) are at the current position in the text local whitespace = string.match(text, "^[ \t]+") if whitespace then -- Print whitespace term.write(whitespace) x, y = term.getCursorPos() text = string.sub(text, string.len(whitespace) + 1) end -- Print any newlines that are needed local newline = string.match(text, "^\n") if newline then newLine() text = string.sub(text, 2) end local text = string.match(text, "^[^ \t\n]+") if text then text = string.sub(text, string.len(text) + 1) if string.len(text) > w then -- Print a multiline word (a word that has more letters than the size of the screen) while string.len(text) > 0 do if x > w then newLine() end term.write(text) text = string.sub(text, (w-x) + 2) x, y = term.getCursorPos() end else -- Print a word normally (one that can fit onto the screen) if x + string.len(text) - 1 > w then newLine() end term.write(text) x, y = term.getCursorPos() end end end end