As Lyqyd stated the text is going off the screen because by default the `hardware` level writes (i.e. term.write and monitor.write) do not handle line wrapping. The job of line wrapping was written (ever so nicely) into ComputerCraft by the devs with the
write function. The
print function just uses
write, and adds a new line. So if you redirect the terminal (term api) to the monitor object you can then use
write and
print, just as you would for the computer.
local monObj = peripheral.wrap('left')
term.redirect(monObj)
print('this is on the monitor')
term.restore()
print('this is on the computer')
As for the centring function, here is the way I like to use
local function writeCentre(msg, y)
local sw, sh = term.getSize()
term.setCursorPos( math.floor( (sw + #msg ) / 2 ) + (#msg % 2 == 0 and 1 or 0), y or (sh / 2))
write(msg)
end
now the same code, expanded and explained
local function writeCenter( msg, y )
--# get the screen size
local screenWidth, screenHeight = term.getSize()
--# calculate the centre of the screen, # gets the length of (in this case) the message string, math.floor removes the decimal part of the number
local xPos = math.floor((screenWidth + #msg) / 2)
--# now even length strings look a little better when they sit to the right of centre, so we check if it is an even length string with modulus (%) and if it is, add 1, or else add 0
xPos = xPos + (#msg % 2 == 0 and 1 or 0)
--# calculate the y position to be the value given to the function, or half the screen height
local yPos = y or (screeHeight / 2)
--# move the cursor
term.setCursorPos(xPos, yPos)
--# write the text
write(msg)
end
As for scrolling text, well do you mean up and down, or side-to-side?