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:
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)