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

CenterPrint - Print centered messages

Started by manu_03, 25 October 2014 - 06:54 PM
manu_03 #1
Posted 25 October 2014 - 08:54 PM
Hello there! I'm manumanucraft and I'll show you my NEW program, CenterPrint. It's very simple. You only need to use:

print <text you want to print>
I'll show you how it works.

text = tArgs[1] -- text is the 1st argument
w,h = term.getSize() -- W is the term's width
l = #text/2 -- l is the half part of the text's width
for i=1,l do
  write(" ") --Write (l value) spaces
end
write(text)
for i=1,l do
  write("  ") --(if I only put 1 space, in some cases the command   input is in the same line that the printed text)
You can use it or modify it. If you download it and/or use it for any program, PM me.

pastebin get xh84rU5E print

EDIT: I saw the reply, you have reason, but that change isn't needed. Using this you can change the background color to color all the line.
Edited on 26 October 2014 - 06:40 AM
Dragon53535 #2
Posted 25 October 2014 - 09:02 PM
instead of relying on write's automatic word wrap, why don't you just use print(text) that way it automatically goes to the next line?

Also

write(string.rep(" ",l))
writes the amount of spaces need in one line without that for loop
Edited on 25 October 2014 - 07:06 PM
wieselkatze #3
Posted 26 October 2014 - 01:35 PM
In your pastebin code you're assuming that the cursor position is at x=1. If not, it wouldn't center the text correctly.
The following code -and that's 3 lines- would do just the same, but with correct centering.


local tArgs, _, yPos = { ... }, term.getCursorPos()
term.setCursorPos( math.ceil( ( tArgs[1] and term.getSize()-#tArgs[1]+1 or error( "No text given" ) )/2 ), yPos  )
term.write( tArgs[1] )
Edited on 26 October 2014 - 02:57 PM
KingofGamesYami #4
Posted 26 October 2014 - 02:16 PM
In your pastebin code you're assuming that the cursor position is at x=1. If not, it wouldn't center the text correctly.
The following code -and that's 3 lines- would do just the same, but with correct centering.


local tArgs, _, yPos = { ... }, term.getCursorPos()
term.setCursorPos( math.ceil( ( tArgs[1] and term.getSize()-#tArgs[1]+1 or error("No text given") )/2 ), yPos  )
term.write( tArgs[1] )
Actually, that's going to print the first word, rather than the entire line! Better to use this:

local tArgs, _, yPos = table.concat( { ... }, " " ), term.getCursorPos()
term.setCursorPos( math.ceil( (tArgs and term.getSize()-#tArgs+1 or error( "no text given") )/2 ), yPos )
term.write( tArgs )
wieselkatze #5
Posted 26 October 2014 - 03:56 PM
In your pastebin code you're assuming that the cursor position is at x=1. If not, it wouldn't center the text correctly.
The following code -and that's 3 lines- would do just the same, but with correct centering.


local tArgs, _, yPos = { ... }, term.getCursorPos()
term.setCursorPos( math.ceil( ( tArgs[1] and term.getSize()-#tArgs[1]+1 or error( "No text given" ) )/2 ), yPos  )
term.write( tArgs[1] )
Actually, that's going to print the first word, rather than the entire line! Better to use this:

local tArgs, _, yPos = table.concat( { ... }, " " ), term.getCursorPos()
term.setCursorPos( math.ceil( (tArgs and term.getSize()-#tArgs+1 or error( "no text given") )/2 ), yPos )
term.write( tArgs )

That's not true in recent versions of Computercraft. You would have a point in older versions, but now you can pass text with spaces as one argument - like so:


<program name> "This is all one argument, but with spaces included" "And this is the second one"
Edited on 26 October 2014 - 02:58 PM
fishermedders #6
Posted 27 October 2014 - 12:04 AM
Wow! I have been looking for one of these for a long time! :)/> This will help A lot with many different things. Thanks ;)/>
TheOddByte #7
Posted 31 October 2014 - 06:58 PM

write(string.rep(" ",l))
I'd also like to add that this reduces screen flickering