Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Coded by HPWebcamAble for YUGATUG
- Forum Post:
- http://www.computercraft.info/forums2/index.php?/topic/22918-program-request/
- ==== Description ====
- Helps you keep track of the enderchest colors you've used
- (From the mod Ender Storage)
- Run with the argument 'help' for usages
- ==== Variables you can change ====
- ]]
- local listHeading = "Enderchest List:"
- local showInfoInList = true --Wasn't sure if you wanted this by default or not
- local fileName = "enderchest"
- --==== Other Variables ====--
- local args = {...}
- local pName = shell.getRunningProgram()
- local f = fs.open(fileName,"r")
- local items
- if f then
- items = textutils.unserialize(f.readAll())
- else
- f = fs.open(fileName,"w")
- items = {}
- end
- f.close()
- --==== Functions ====--
- local function search(toFind)
- for i = 1, #items do
- if items[i][1] == toFind then
- return i
- end
- end
- return false
- end
- local function tc(color)
- if term.isColor() then term.setTextColor(color) end
- end
- local function list() --From my getMethods program... Like I said to do :)
- local w,h = term.getSize()
- term.setCursorPos(1,1)
- term.clear()
- tc(colors.yellow)
- local heading = listHeading
- local hLines = print(heading)+1
- tc(colors.white)
- local pFunctions = items
- local pages = math.ceil(#pFunctions/(h-hLines))
- local fPerPage = math.floor(#pFunctions/pages)
- local cPage = 1
- if pages > 1 then
- while true do
- for i = 1, h-hLines do
- local curF = i+fPerPage*(cPage-1)
- if curF > #pFunctions then break end
- term.setCursorPos(1,hLines+i-1)
- if showInfoInList then
- term.write(pFunctions[curF][1].. (pFunctions[curF][2]~=nil and (": "..pFunctions[curF][2]) or "") )
- else
- term.write(pFunctions[curF][1])
- end
- end
- term.setCursorPos(1,h)
- if cPage == 1 then
- tc(colors.yellow)
- term.write("Use arrow keys to navigate (Right) >")
- tc(colors.white)
- elseif cPage < pages then
- tc(colors.yellow)
- term.write("< (Left) Back / Forward (Right) >")
- tc(colors.white)
- else
- tc(colors.yellow)
- term.write("< (Left) Back / Exit (Right) >")
- tc(colors.white)
- end
- local continue = true
- while continue do
- local event,key = os.pullEvent("key")
- if key == keys.right then
- if cPage < pages then
- cPage = cPage+1
- break
- else
- term.setCursorPos(1,h)
- tc(colors.yellow)
- term.write("Press (Right) again to exit ")
- tc(colors.white)
- while true do
- local event1,key1 = os.pullEvent("key")
- if key1 == keys.right then
- term.setCursorPos(1,h)
- term.clearLine()
- term.setCursorPos(1,math.ceil(#pFunctions/pages)+hLines)
- error()
- elseif key1 == keys.left then
- continue = false
- break
- end
- end
- end
- elseif key == keys.left then
- if cPage > 1 then
- cPage = cPage-1
- break
- end
- end
- end
- term.setCursorPos(1,1)
- term.clear()
- tc(colors.yellow)
- print(heading)
- tc(colors.white)
- end
- else
- for a,b in pairs(pFunctions) do
- if showInfoInList then
- print(b[1].. (b[2]~=nil and (": "..b[2]) or "") )
- else
- print(b[1])
- end
- end
- end
- end
- local function saveList()
- local f = fs.open(fileName,"w")
- f.write(textutils.serialize(items))
- f.close()
- end
- --==== Program ====--
- function main()
- if args[1] == "add" then
- if not args[2] then
- print("Usage:")
- print(pName.." add <string>")
- print(pName.." add <string> <info>")
- return
- elseif search(args[2]) then
- print(args[2].." is already on the list")
- return
- end
- local name = args[2]
- table.remove(args,1)
- table.remove(args,1)
- table.insert(items,{name,table.concat(args," ")})
- print("Added "..name.." to the list")
- saveList()
- elseif args[1] == "list" then
- list()
- elseif args[1] == "search" then
- if not args[2] then
- print("Usage:")
- print(pName.." search <string>")
- return
- elseif not search(args[2]) then
- print(args[2].." isn't on the list")
- return
- end
- if items[search(args[2])][2] then
- print(args[2]..": "..items[search(args[2])][2])
- else
- print(args[2].." exists, but doesn't have info")
- end
- elseif args[1] == "delete" then
- if not args[2] then
- print("Usage:")
- print(pName.." delete <string>")
- return
- elseif not search(args[2]) then
- print(args[2].." isn't on the list")
- return
- end
- table.remove(items,search(args[2]))
- saveList()
- else
- print("Usages:")
- print(pName.." add <string>")
- print(pName.." add <string> <info>")
- print(pName.." list")
- print(pName.." search <string>")
- print(pName.." delete <string>")
- return
- end
- end
- local state,err = pcall(main)
- if not state and err then
- print()
- print("Error!")
- printError(err)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement