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

centerText API

Started by Carrots084, 26 July 2014 - 02:26 AM
Carrots084 #1
Posted 26 July 2014 - 04:26 AM
First off, this may be used as an API or imported into your code. You do not need to credit me, as I'm sure I'm not the only one who though of code like this. It only centers on the X Axis, may implement a Y Axis.
Usage:
Spoiler

os.loadAPI(<yourfilename>)
<filename>.centerText("Your text here!")
It's that simple!
Installation:
Spoiler

pastebin get yHaPXesQ <filename>
Screenshots (taken from my development of an adaptation of A Dark Room):
SpoilerClick to enlarge:
[attachment=1829:centerText(1).JPG]
[attachment=1830:centerText(2).JPG]
Have fun!
Edited on 27 July 2014 - 04:39 PM
Zudo #2
Posted 27 July 2014 - 12:33 PM
Personally, I would do it like this:


function center(txt, ln)
 w = term.getSize()
 term.setCursorPos(w/2-#txt/2, ln)
 write(txt)
end
theoriginalbit #3
Posted 27 July 2014 - 01:10 PM
-snip-
polluting the global space much. why not just


local function center(text, y)
  local w = term.getSize()
  local _, cy = term.getCursorPos()
  term.setCursorPos(math.ceil((w-#text)/2), y or cy)
  write(text)
end

math.ceil is essential to make sure it always looks like it is centered, since the width is 51.
wieselkatze #4
Posted 27 July 2014 - 03:57 PM
Please test your code before you post in on the forums. At a first glance I already saw a typo (math.ciel on line 4).
I also don't think that 6 lines of code are really worth calling this an API.
Carrots084 #5
Posted 27 July 2014 - 06:38 PM
Please test your code before you post in on the forums. At a first glance I already saw a typo (math.ciel on line 4).
I also don't think that 6 lines of code are really worth calling this an API.

did i really put ciel? I'll fix it asap
EDIT: fixed. And I'm aware it's a bit short, but it helps newbies with the trouble of trying to figure out how to center their text, and they can look at it to figure out how to develop their own centerText form
Edited on 27 July 2014 - 04:40 PM
theoriginalbit #6
Posted 27 July 2014 - 11:03 PM
but it helps newbies with the trouble of trying to figure out how to center their text
That is why we have tools like Google.
Carrots084 #7
Posted 27 July 2014 - 11:04 PM
but it helps newbies with the trouble of trying to figure out how to center their text
That is why we have tools like Google.
:P/>