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

How to Center text without using tables?

Started by AnthonyD98™, 27 January 2013 - 06:02 PM
AnthonyD98™ #1
Posted 27 January 2013 - 07:02 PM
Hello, I have a question:

How do you center text without using tables?

What I usually do is

local w,h = term.getSize()
local titleY = 1
term.clear()
local function center(text, line)
  term.setCursorPos(math.floor(w/2)-string.len(text)/2, line)
  print(text)
end
local title ={ " [ SelectGUI Alpha ] " }
for i,v in ipairs (title) do
	center(v,titleY+i)
end

Pastebin link:
Spoilerhttp://pastebin.com/srKsUjd8

Also if anyone could help me make my GUI / OS mouse compatiable it would be greatly appreciated

My GUI / OS ( SelectGUI Alpha)
Spoilerhttp://pastebin.com/z19FzBbD


Thanks, AnthonyD98
Zoinky #2
Posted 27 January 2013 - 07:07 PM
Try something like this:


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

Just give it a string like the regular print function.
AnthonyD98™ #3
Posted 27 January 2013 - 07:14 PM
I'm confused on this though, do I still need?

for i,v in ipairs (title) do
center(v,titleY+i)
end
AnthonyD98™ #4
Posted 27 January 2013 - 07:16 PM
Ahh! I see now

cPrint("I am a string!")

And it is centered!
Zoinky #5
Posted 27 January 2013 - 07:18 PM
Ahh! I see now

cPrint("I am a string!")

And it is centered!

Yea :D/> I'll try making a simple menu for you. No promises though.
AnthonyD98™ #6
Posted 27 January 2013 - 07:21 PM
Ahh! I see now

cPrint("I am a string!")

And it is centered!

Yea :D/> I'll try making a simple menu for you. No promises though.

:D/> Thank you Zoinky for your help, +1 for you.

Also thanks for trying to make a simple menu. And I see your in the same country as me! :)/>

EDIT: Just found out I can't have a picture in my signature :o/>
Zoinky #7
Posted 27 January 2013 - 09:00 PM
Also thanks for trying to make a simple menu.

Sorry, couldn't get one of the functionalities to work without making the program really large, so I gave up on it. Forgot to mention that. :S
AnthonyD98™ #8
Posted 28 January 2013 - 10:43 AM
Also thanks for trying to make a simple menu.

Sorry, couldn't get one of the functionalities to work without making the program really large, so I gave up on it. Forgot to mention that. :S

Its alright, atleast you tried :)/>
AnthonyD98™ #9
Posted 28 January 2013 - 10:54 AM
Try something like this:


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

Just give it a string like the regular print function.

Also I can add this code to the API I'm making - ( Private API )
NeverCast #10
Posted 28 January 2013 - 12:03 PM
Just a note, you shouldn't use string as an argument name, as it means you can't access the string table used for string functions inside that function.
You might not want to use things like string.sub or string.len ( more so when you have #stringvar )

but you should typically avoid it always, it helps when debugging in the future.


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

^ That would serve you better as a reference.
Orwell #11
Posted 28 January 2013 - 12:08 PM
Just a note, you shouldn't use string as an argument name, as it means you can't access the string table used for string functions inside that function.
You might not want to use things like string.sub or string.len ( more so when you have #stringvar )
I said this once too when someone did the same thing and then someone else pointed out to me that it doesn't matter. I tested it then and indeed, if it can't index it in the local scope, it appears to search for a global variable with the same name that it can index (I was quite shocked by it). So it would work, but it's still very bad practice IMO.
NeverCast #12
Posted 28 January 2013 - 12:22 PM
it's very bad practice yes, but it does make sense. The local tables metatable indeed has _G in it's __index. So it makes sense that it would do that.
But you shouldn't ever override or hide global tables or functions, unless it's your explicit intention to do so.

You've certainly taught me something, although I understand it, I wouldn't have guessed the outcome.
Orwell #13
Posted 28 January 2013 - 12:51 PM
I didn't know that local variables had _G as __index in their metatables so I learn something as well. :)/> It's perfectly logical now.
NeverCast #14
Posted 28 January 2013 - 12:54 PM
not local variables, I meant the environment that the function is running in.. Give me a moment, I'll check it :D/>
NeverCast #15
Posted 28 January 2013 - 01:06 PM

local function coolStory(string)
	local string = string
	local env = getfenv()
	local mt = getmetatable(env)
	local callback = mt.__index
	print(callback)
	oldString = callback(env, "string")
	print(string)
	print(oldString)
	string = string.sub(string, 1,2)
	print(string)
end

coolStory("hello")

Showing how it works.
Seems that when it can't find string.bla, it grabs the local environments metatable and does a call on it, to get the string table

Edit: ONLY works because the argument is a string, if you make it a table, it does NOT work!

Edit2: After more thinking I think it's the way that Lua determines what variable to compile against.
When it determines that you are looking for a table ( string.bla) then it looks for the local table by that name, if it's not available, it looks for the next upvalue with that name. So it searches for variables by type.

How it works entirely I can't be certain, But I think it's more of Lua compiling and looking for a table instead of just any variable.
Orwell #16
Posted 28 January 2013 - 01:09 PM

local function coolStory(string)
	local string = string
	local env = getfenv()
	local mt = getmetatable(env)
	local callback = mt.__index
	print(callback)
	oldString = callback(env, "string")
	print(string)
	print(oldString)
	string = string.sub(string, 1,2)
	print(string)
end

coolStory("hello")

Showing how it works.
Seems that when it can't find string.bla, it grabs the local environments metatable and does a call on it, to get the string table

Edit: ONLY works because the argument is a string, if you make it a table, it does NOT work!
I kinda assumed it was the function environment but I paraphrased you wrongly. :P/> It's what I thought.
NeverCast #17
Posted 28 January 2013 - 01:13 PM
It's probably a combination of using the __index + looking for a table.
Not sure. One way to find out would be to change the index to a table with a different string table in it.
Zoinky #18
Posted 28 January 2013 - 08:04 PM
Guys, getting a little off-topic-ish*. Lol. :D/>

* Don't hurt me D:
Zoinky #19
Posted 28 January 2013 - 08:09 PM
-snip-

Also I can add this code to the API I'm making - ( Private API )

Erm, sure. Go ahead :D/>

Edit: Crap, double post. Is it possible to have a moderator merge them? :3
Mads #20
Posted 29 January 2013 - 05:55 AM
Just divide the screen width by 2, and do the same with the text length. Then you set the cursor position and write.