60 posts
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):
Spoiler
Click to enlarge:
[attachment=1829:centerText(1).JPG]
[attachment=1830:centerText(2).JPG]Have fun!
Edited on 27 July 2014 - 04:39 PM
1114 posts
Location
UK
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
7508 posts
Location
Australia
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.
227 posts
Location
Germany
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.
60 posts
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
7508 posts
Location
Australia
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.
60 posts
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/>