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

Help with a menu using a config file

Started by Himself12794, 05 February 2013 - 05:14 AM
Himself12794 #1
Posted 05 February 2013 - 06:14 AM
Title: Help with a menu using a config file

I'm trying to make a menu asking for the user to select a side, which it should then store in a config file. If the file does not exists or contain a valid side, it will run the menu to ask. The menu works fine, just the side selected is not being written to the file, and I get the menu every time I run the code. Here it is:

function printCentered(height, value)
  local xpos = w/2 - string.len(value)/2
  term.setCursorPos(xpos, height) 
  term.write(value)
end
function clearA()
  term.clear()
  term.setCursorPos(1,1)
end
w, h=term.getSize()
function selSide()
  sel=1
  while true do
    clearA()
term.setCursorPos(1,3)
print("No output side has been set for the cables.")
term.setCursorPos(1,4)
print("Please select a side: ")
    if sel==1 then
	  printCentered(7, "   [ Top ]    ")
   else printCentered(7, "	 Top	  ")
end
if sel==2 then
	  printCentered(8, "   [ Bottom ]  ")
   else printCentered(8, "	 Bottom    ")
end
    if sel==3 then
	  printCentered(9, "   [ Left ]   ")
   else printCentered(9, "	 Left	 ")
end
if sel==4 then
  printCentered(10, "   [ Right ]   ")
  else printCentered(10, "	 Right	 ")
end
    if sel==5 then
	  printCentered(11, "   [ Back ]   ")
   else printCentered(11, "	 Back	 ")
end
if sel==6 then
   printCentered(12, "   [ Front ]   ")
	  else printCentered(12, "	 Front	 ")
    end

    a, b=os.pullEvent("key")
    if b==200 and sel>1 then
	  sel=sel-1
    elseif b==200 and sel==1 then
   sel=6
end
if b==208 and sel<6 then
   sel=sel+1
elseif b==208 and sel==6 then
   sel=1
end
if b==28 then
   break
end
  end
  return sel
end

function configCheck()
  if not fs.exists("config") then
    config=fs.open("config", "w")
    slc=selSide()
    if slc==1 then
   config.writeLine("top")
   side="top"
   config.close()
elseif slc==2 then
   config.writeLine("bottom")
   side="bottom"
   config.close()
elseif slc==3 then
   config.writeLine("left")
   side="left"
   config.close()
elseif slc==4 then
   config.writeLine("right")
   side="right"
   config.close()
elseif slc==5 then
   config.writeLine("top")
   side="top"
   config.close()
   else config.writeLine("bottom") side="bottom" config.close()
end
  else
    config=fs.open("config", "r")
   if config.readLine() ~= "top" or config.readLine ~= "bottom" or config.readLine() ~= "left" or config.readLine() ~= "right" or config.readLine() ~= "front" or config.readLine() ~= "back" then
	 config.close()
  config=fs.open("config", "w")
  slc=selSide()
  if slc==1 then
	   config.writeLine("top")
    side="top"
    config.close()
	 elseif slc==2 then
	   config.writeLine("bottom")
    side="bottom"
    config.close()
	 elseif slc==3 then
	   config.writeLine("left")
    side="left"
    config.close()
	 elseif slc==4 then
	   config.writeLine("right")
    side="right"
    config.close()
	 elseif slc==5 then
	   config.writeLine("top")
    side="top"
    config.close()
	   else config.writeLine("bottom")
    side="bottom"
    config.close()
  end
	  else side=config.readLine() config.close()
end
  end
end
configCheck()
clearA()
print(side)
remiX #2
Posted 05 February 2013 - 07:22 AM
When you check if config.readLine() ~= "top" or etc, each or needs to be AND.

Also, do this:

    config=fs.open("config", "r")
	tmpSide = config.readLine()
   if tmpSide ~= "top" and tmpSide ~= "bottom" and tmpSide ~= "left" and tmpSide ~= "right" and tmpSide ~= "front" and tmpSide ~= "back" then
because each time it checks the config.readLine() it reads the next line and will fail all the time.

And change
else side=config.readLine() config.close() -- because it will read the next line
to
else side=tmpSide config.close()

Basically, everytime you do config.readLine() it reads the NEXT line.
zekesonxx #3
Posted 05 February 2013 - 07:32 AM

print("Please select a side: ")
    if sel==1 then
		  printCentered(7, "   [ Top ]    ")
   else printCentered(7, "	   Top	  ")
end
if sel==2 then
		  printCentered(8, "   [ Bottom ]  ")
   else printCentered(8, "	   Bottom    ")
end
    if sel==3 then
		  printCentered(9, "   [ Left ]   ")
   else printCentered(9, "	   Left    ")
end
if sel==4 then
  printCentered(10, "   [ Right ]   ")
  else printCentered(10, "	   Right   ")
end
    if sel==5 then
		  printCentered(11, "   [ Back ]   ")
   else printCentered(11, "	  Back    ")
end
if sel==6 then
   printCentered(12, "   [ Front ]   ")
		  else printCentered(12, "	   Front   ")
    end

Instead, do:

for i = 1,#rs.getSides() do
    if sel==i then
        printCentered(i+6, "   [ "..rs.getSides()[i].." ]   ")
    else
        printCentered(i+6, "     "..rs.getSides()[i].."     ")
    end
end
Himself12794 #4
Posted 05 February 2013 - 10:24 AM
Thank you for your help! I wondered myself if I was just reading a new line every time, but you confirmed it for me. Thanks alot! I also found a way to break my code up into several smaller files. It may be inefficient, but I don't like to scroll through lines of code to find a spot. Also, they need to be used by other programs as well.

Using the for loop does make my selection smaller, but it does not allow them to be centered properly. I will look over what you gave me and see if I can fix that. Thanks alot!