Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local run = true
- local sItem = 1
- local tW,tH = term.getSize()
- local tMen = {}
- local run = true
- local cMen = 1
- -- Menu functions
- function sMenu(menu)
- local h = fs.open("menus","a")
- h.writeLine(textutils.serialize(menu))
- h.close()
- end
- function ex()
- for i=1, #tMen do
- for j=1,#(tMen[i].choice) do
- if tMen[i].choice[j].name == tMen[cMen].name then
- cMen = i
- sItem = 1
- return
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- run = false
- end
- function wizard()
- local counter = 1
- local name = nil
- local choice = {}
- term.clear()
- term.setCursorPos(1,1)
- print("This wizards guides you through the Menu Creation!")
- print("Please insert a Name for your Menu")
- name = read()
- print("Please insert the Choices of your Menu;Exit by typing 'exit'")
- while true do
- print("Choice "..tostring(counter))
- local tmp = read()
- if tmp == "exit" then
- break
- elseif counter > 7 then
- print("To much entries; Please create SubMenus")
- break
- end
- choice[counter] = {}
- print("please insert the Function it should start;for a submenu type 'submenu'")
- local tmp2 = read()
- if tmp2 == "submenu" then
- print("What is the name of the connected Menu?")
- local tmp3 = read()
- choice[counter].name = tmp3
- else
- choice[counter].name = nil
- end
- choice[counter].text = tmp
- choice[counter].handler = tmp2
- counter = counter + 1
- end
- choice[counter] = {}
- choice[counter].text = "Exit"
- choice[counter].handler = "exx"
- local menu = {}
- menu.name = name
- menu.choice = choice
- sMenu(menu)
- end
- function gMenuCount()
- if not fs.exists("menus") then
- return 0
- else
- local counter = 0
- local h = fs.open("menus","r")
- while h.readLine() ~= nil do
- counter = counter + 1
- end
- h.close()
- return counter
- end
- end
- function gMenu(nr)
- if not fs.exists("menus") then
- return "no file"
- elseif nr > gMenuCount() then
- return "does not exist"
- else
- local h = fs.open("menus","r")
- for i = 1,(nr-1) do
- h.readLine()
- end
- local menu = textutils.unserialize(h.readLine())
- if menu == nil then
- return "menu was nil"
- else
- return menu
- end
- end
- end
- function gtMen()
- if not fs.exists("menus") then
- return false
- end
- print("done gtMen test")
- local i = 1
- local h = fs.open("menus","r")
- local tmp = h.readLine()
- while not (tmp == nil) do
- print("gtMen inLoop")
- tmp = textutils.unserialize(tmp)
- print("start gtMen test 2 ")
- if not (tmp == nil) then
- tMen[i] = {}
- tMen[i] = tmp
- end
- print("done gtMen test 2")
- i = i +1
- tmp = h.readLine()
- end
- h.close()
- print("done gtMen")
- end
- -- Print
- function cbstr(str,char)
- if char == nil then
- char = " "
- end
- local v = math.floor((tW-string.len(str))/2)
- for i=1,(v-1) do
- str = string.format(char..str)
- end
- for i = string.len(str),tW-1 do
- str = string.format(str..char)
- end
- return str
- end
- function chprint(strtbl)
- if strtbl == nil then
- strtbl = {"Test1","Test2","Test3"}
- end
- local space = 3
- local k = 0
- local v = math.floor((tH-#strtbl)/2)
- if v > space then
- while k < (space*2 -tH + #strtbl)/(#strtbl-1) do
- k = k+1
- end
- else
- space = 0
- end
- for i=1,#strtbl do
- for j=1,space do
- print(" ")
- end
- print(strtbl[i])
- for j=1,k do
- print(" ")
- end
- end
- end
- function printMenu()
- print(cbstr(tMen[cMen].name,"-"))
- local strtbl = {}
- for i=1,#(tMen[cMen].choice) do
- if i == sItem then
- strtbl[i] =cbstr("[ "..tostring(tMen[cMen].choice[i].text).." ]")
- else
- strtbl[i] =cbstr(" "..tostring(tMen[cMen].choice[i].text).." ")
- end
- end
- chprint(strtbl)
- end
- -- Handler
- function onKeyPressed(key)
- if key == keys.enter then
- onItemS()
- elseif key == keys.up then
- if sItem > 1 then
- sItem = sItem - 1
- end
- elseif key == keys.down then
- if sItem < #(tMen[cMen].choice) then
- sItem = sItem + 1
- end
- end
- end
- function onItemS()
- if tMen[cMen].choice[sItem].handler == "submenu" then
- for i = 1,#tMen do
- if tMen[i].name == tMen[cMen].choice[sItem].name then
- cMen = i
- return
- end
- end
- elseif tMen[cMen].choice[sItem].handler == "exx" then
- ex()
- else
- local h = loadstring(tMen[cMen].choice[sItem].handler)
- h()
- end
- end
- -- Main
- function main()
- run = true
- gtMen()
- while run do
- term.clear()
- term.setCursorPos(1,1)
- printMenu()
- local event,key = os.pullEvent("key")
- onKeyPressed(key)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment