Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local me = peripheral.wrap("left")
- function resetTerm()
- term.clear()
- term.setCursorPos(1,1)
- end
- function printHelp()
- resetTerm()
- print("[1] Add or update item")
- print("[2] Remove item")
- print("[3] List all items")
- end
- function loadConfig()
- local file = fs.open("./stock.config", "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data) or {}
- end
- function saveConfig(table)
- local file = fs.open("./stock.config", "w")
- file.write(textutils.serialize(table))
- file.close()
- resetTerm()
- print("Config saved successfully.")
- sleep(2)
- end
- function q(text)
- resetTerm()
- print(text)
- local t = read()
- return t
- end
- function tableLength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function listTable(table)
- local toShow = 8
- local count = 1
- for key, val in pairs(table) do
- print("Key: ", key, " - Value: ", val)
- if count % toShow then
- print("Press any key to continue...")
- os.pullEvent("key")
- end
- end
- end
- while true do
- printHelp()
- local action = read()
- if action == "1" then
- resetTerm()
- local config = loadConfig()
- local item = q("ID of item to add or update:")
- local ok, meItem = pcall(me.findItem, item)
- if not ok then
- resetTerm()
- print("Item not found. Canceling...")
- sleep(2)
- else
- local name = meItem.getMetadata().name
- if config[name] then
- local yn = q("That item already exists. Would you like to update quantity? (y/n)\nCurrent quantity: " .. config[name].qty)
- if yn == "y" then
- local qty = tonumber(q("Quantity to keep in stock:"))
- config[name].qty = qty
- saveConfig(config)
- else
- resetTerm()
- print("Canceling...")
- sleep(2)
- end
- else
- local qty = tonumber(q("Quantity to keep in stock:"))
- local t = {}
- t["qty"] = qty
- t["name"] = name
- config[name] = t
- saveConfig(config)
- end
- end
- elseif action == "2" then
- local config = loadConfig()
- if tableLength(config) > 0 then
- local item = q("Enter the item you would like to remove.")
- local ok, meItem = pcall(me.findItem, item)
- if not ok then
- resetTerm()
- print("Item not found. Canceling...")
- sleep(2)
- else
- config[meItem.getMetadata().name] = nil
- saveConfig(config)
- end
- else
- resetTerm()
- print("No items to delete.")
- sleep(2)
- end
- elseif action == "3" then
- local config = loadConfig()
- resetTerm()
- print("Listing all saved items:")
- if tableLength(config) < 1 then
- print("No saved items... Continuing in 2 seconds.")
- sleep(2)
- else
- listTable(config)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement