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

Help with monitor text going off the screen.

Started by firelink70, 04 June 2013 - 09:45 PM
firelink70 #1
Posted 04 June 2013 - 11:45 PM
Hello, recently I've been doing some work on an announcement board in my town on a server. However, a problem is occurring:

Text goes off the screen, rather than down to the next line.

I'd like to know if theres a simple line or two of code to make this not happen. Also, 2 other requests.

1) Center text?
2) Scrolling text?

Thank you. :)/>

- Firelink70
Lyqyd #2
Posted 05 June 2013 - 12:49 PM
Split into new topic.

Redirect to the monitor and use print instead of using .write directly from your wrapped monitor.

There are many, many centered text functions around.
theoriginalbit #3
Posted 05 June 2013 - 01:00 PM
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?
firelink70 #4
Posted 05 June 2013 - 02:43 PM
Side-to-side scrolling.

Also, can I replace write in:

--# write the text
  write(msg)
end

with print so text won't go off the screen?

And can I use setCursorPos() inside of:


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

for multiple lines of centred text or do I need to use the entire math function over again?
theoriginalbit #5
Posted 05 June 2013 - 10:29 PM
Side-to-side scrolling.
Ok that is a little harder, on my phone so too hard to type, I'll give a hand when I'm home if someone else hasn't already.

with print so text won't go off the screen?
As I stated before write is the one that deals with line wrapping, print just uses write with a \n character on the end for a new line. So write is fine here. Plus it's a centre printing function, if your text is needing to be wrapped then why are you trying to centre it.

And can I use setCursorPos() inside of:

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
for multiple lines of centred text or do I need to use the entire math function over again?
You may as well just do multiple calls to the function, it would be less code than attempting to create a Multiline centre printing function.
bluefoxy #6
Posted 07 October 2013 - 05:22 PM
i also have the same issue with the program here http://www.computercraft.info/forums2/index.php?/topic/15442-advancedtodo-list-v10-improved/
how can i fix this?
it happens after u right click a task