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

How to center text on a monitor?

Started by owufail, 04 November 2012 - 02:26 PM
owufail #1
Posted 04 November 2012 - 03:26 PM
Alright currently, I have no clue what i was doing. I need to center my text per line,

I have a 2x3 monitors saying
Welcome to
Servername

but the Welcome to i can center by setting cursor position, but when i do it will the server name it isn't exactly centered.
Pharap #2
Posted 04 November 2012 - 03:49 PM
that's probably because when you call print, it sets the cursor position to one more than the current y, but sets x to 1

try doing this:

--this function will print the text in the centre of the current line
local printcentred =
function(text)
local maxw, maxh = term.getSize() --screen dimensions
local curx, cury = term.getCursorPos() --cursor pos
term.setCursorPos((maxw-#text)/2,cury) --set it to the point where write will make the text centred
term.write(text) --write the text
term.setCursorPos(curx,cury+1) --return the cursor to the same position on the next line
end
local maxw, maxh = term.getSize() --screen dimensions
term.setCursorPos(1,math.floor(maxh/2))--set cursor to the start of the upper middle line
printcentred("Welcome To")--print centred on that line
printcentred("Server Name")--print centred on the next line

jag #3
Posted 04 November 2012 - 05:25 PM
that's probably because when you call print, it sets the cursor position to one more than the current y, but sets x to 1

try doing this:

--this function will print the text in the centre of the current line
local printcentred =
function(text)
local maxw, maxh = term.getSize() --screen dimensions
local curx, cury = term.getCursorPos() --cursor pos
term.setCursorPos((maxw-#text)/2,cury) --set it to the point where write will make the text centred
term.write(text) --write the text
term.setCursorPos(curx,cury+1) --return the cursor to the same position on the next line
end
local maxw, maxh = term.getSize() --screen dimensions
term.setCursorPos(1,math.floor(maxh/2))--set cursor to the start of the upper middle line
printcentred("Welcome To")--print centred on that line
printcentred("Server Name")--print centred on the next line

Let me just make a quick note here; you are showing him a function to print in the middle on the GUI, not on a monitor.
1lann #4
Posted 04 November 2012 - 11:07 PM
Use term.redirect Problem solved?
Pharap #5
Posted 05 November 2012 - 11:44 AM
Let me just make a quick note here; you are showing him a function to print in the middle on the GUI, not on a monitor.

It will if you run it on the monitor, but if you really insist on a monitor specific version:


--this function will print the text in the centre of the current line of the monitor connected on the specified side
local monitorprintcentred = function(text, side)
local monitor = peripheral.wrap(side)
local maxw, maxh = monitor.getSize() --screen dimensions
local curx, cury = monitor.getCursorPos() --cursor pos

monitor.setCursorPos((maxw-#text)/2,cury) --set it to the point where write will make the text centred
monitor.write(text) --write the text
monitor.setCursorPos(curx,cury+1) --return the cursor to the same position on the next line
end

local monitor = peripheral.wrap("left") -- makes 'monitor' refer to the monitor on the left side
local maxw, maxh =monitor.getSize() --screen dimensions
monitor.setCursorPos(1,math.floor(maxh/2))--set cursor to the start of the upper middle line

monitorprintcentred("Welcome To","left")--print centred on that line
monitorprintcentred("Server Name","left")--print centred on the next line

That was only knocked together in about 5 minutes, and I was assuming most people knew about the monitor program since it's mentioned on the CC wiki(http://computercraft...p?title=Monitor), as well as mentioned in the built in help program.