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

How do you display centered text on a monitor and scale the text size?

Started by Phantomizer, 08 October 2013 - 05:33 PM
Phantomizer #1
Posted 08 October 2013 - 07:33 PM
I've done a little searching around and found plenty of answers, but I can't seem to work them out…I have a 4 (w) by 1 (h) set of monitors and I want to display a line of text on them that is centered and scale the text so it's easy to read, the computer is behind the monitors.

Thanks!
Lyqyd #2
Posted 08 October 2013 - 08:04 PM
Split into new topic.
Zacklogan #3
Posted 08 October 2013 - 09:47 PM

local function cPrint(string)
x, _ = term.getSize()
_, y = term.getCursorPos()
term.setCursorPos((x-#string)/2,y)
print(string)
end

Is this what you are looking for?
Phantomizer #4
Posted 08 October 2013 - 09:56 PM

local function cPrint(string)
x, _ = term.getSize()
_, y = term.getCursorPos()
term.setCursorPos((x-#string)/2,y)
print(string)
end

Is this what you are looking for?

Umm…that doesn't do anything?

I replaced string on the last line with the text I want to display on the monitors, still didn't display anything…not even an error.
theoriginalbit #5
Posted 08 October 2013 - 10:01 PM

local function cPrint(string)
x, _ = term.getSize()
_, y = term.getCursorPos()
term.setCursorPos((x-#string)/2,y)
print(string)
end

Is this what you are looking for?

Umm…that doesn't do anything?

I replaced string on the last line with the text I want to display on the monitors, still didn't display anything…not even an error.
it does work, you have to call it like so

cPrint("text to print on in middle")
however there are a few problems with the code that I'll address now

local function cPrint(str)
  local x = term.getSize()
  local _,y = term.getCursorPos()
  term.setCursorPos(math.ceil((x-#str)/2), y)
  print(str)
end
The problems with the code were:
  • naming the variable string, was overriding the string api, this is bad, avoid naming variables the same as apis and default variables/functions
  • no localisation of variables, everything should be localised when not needed elsewhere
  • no rounding of the x position, the rounding I performed makes it look good on the screen when it is an odd or even length string
The above function you still call in the same way

cPrint("Centred text")
Phantomizer #6
Posted 08 October 2013 - 10:13 PM
Ok…after some some poking around with it, it does work, but i'm trying to output text to a series of monitors, not the computer itself :P/>
theoriginalbit #7
Posted 08 October 2013 - 10:22 PM
take a look into term.redirect and term.restore.
Phantomizer #8
Posted 08 October 2013 - 10:30 PM

Works, but doesn't output the text centered or scaled.
theoriginalbit #9
Posted 08 October 2013 - 10:33 PM
Works, but doesn't output the text centered or scaled.
Are you putting any effort into working this out yourself? everything you need to know are either in the answers provided or on the link I provided

"Here in Ask a Pro, the effort people are willing to expend helping you is usually about equal to the effort you are putting in." — Lyqyd

As a side note for you, if you're trying to do multiple text scales, you cannot, there can only be one text scale on the monitor.

Post your code as well.
Phantomizer #10
Posted 08 October 2013 - 10:43 PM

local function cPrint(str)
  local x = term.getSize(3)
  local _,y = term.getCursorPos()
  term.setCursorPos(math.ceil((x-#str)/2), y)
  print(str)
end

term.redirect(peripheral.wrap("back"))
cPrint("BLACKSMITH")

It mostly works, but the problem is: The text is centered at the top, not the center of the monitor and I can't get the text to scale size.
theoriginalbit #11
Posted 08 October 2013 - 10:53 PM
The text is centered at the top, not the center of the monitor
Well of course, you didn't specify the absolute centre, so its printing the centre of the line

y = term.getCursorPos()
term.setCursorPos(math.ceil((x-#str)/2), y)

make it math.floor(y/2)

and I can't get the text to scale size.
Here
Phantomizer #12
Posted 09 October 2013 - 12:18 AM
Got it, thanks.