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

Center?

Started by Theronguard, 15 August 2012 - 12:49 PM
Theronguard #1
Posted 15 August 2012 - 02:49 PM
Hello. Im new here and also im noob in lua :3. I have question, how did i center my menu options

Code :

    opcje = {}
    wybor = 1
    opcje[1] = "Wylacz komputer"
    opcje[2] = "Zrestartuj komputer"
    opcje[3] = "Zagraj w weza"
    opcje[4] = "Kontroluj zolwia"
    opcje[5] = "Przejdz do trybu komend"
	
    function uruchom(numer)
	 if numer == 1 then
	  shell.run("shutdown")
	 elseif numer == 2 then
	  shell.run("reboot")
	 elseif numer == 3 then
	  shell.run("worm")
	 elseif numer == 4 then
	  shell.run("wcmove")
	 elseif numer == 5 then
	 else
	  wybor = 1
	  return menu()
	 end
    end
	
function przycisk()
sleep(0.05)
event, argument = os.pullEvent()
if event == "key" then
  if argument == 28 then
   return uruchom(wybor)
  elseif argument == 200 then
   if wybor == 1 then
   else
    wybor = wybor - 1
   end
  elseif argument == 208 then
   if wybor == table.getn(opcje) then
   else
    wybor = wybor + 1
   end
  else
   return przycisk()
  end
else
  return przycisk()
end
return menu()
end

function menu()
shell.run("clear")
for i=1, table.getn(opcje) do
  if wybor == i then
   print("[ "..opcje[i].." ]")
  else
   print(opcje[i])
  end
end
return przycisk()
end

shell.run("clear")
return menu()
Sorry, it's polish menu :<, if it's problem, i will translate it…
So, i just want to make my options

    opcje = {}
    wybor = 1
    opcje[1] = "Wylacz komputer"
    opcje[2] = "Zrestartuj komputer"
    opcje[3] = "Zagraj w weza"
    opcje[4] = "Kontroluj zolwia"
    opcje[5] = "Przejdz do trybu komend"
be on center of my screen… Can i get some help? <Thanks>
Kazimir #2
Posted 15 August 2012 - 03:06 PM
use API getSize


local width, height = term.getSize()
width=(width/2)
term.setCursorPos(width,height)
Cranium #3
Posted 15 August 2012 - 03:10 PM
I have used this as my menu for a while. It centers the options on the screen, and puts a "><" on each side to select which one. just point to it as something like local input == opt(NAME OF YOUR TABLE HERE)

function opt(m)
n=1
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
b = string.len(">"..m[i].."<")/2
x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
And for future reference, you can also use your table like this:

local opcje = {
"Wylacz komputer",
"Zrestartuj komputer",
"Zagraj w weza",
"Kontroluj zolwia",
"Przejdz do trybu komend"
}
And it would work just as well as defining each table line like you do now.