Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Written by NickM13
- Todo list for ComputerCraft terminal glasses
- Command list:
- $$add [New TODO Item]
- $$finish [Item ID(1-9)]
- $$clear [Page(1-9) or nothing for current page]
- $$title [New Page Title]
- $$delete [Item ID(1-9)]
- $$page [Page ID(1-9)]
- --]]
- local termbridge = nil
- local ip = {}
- local list = {}
- for i=1, 9 do
- list[i] = {}
- for j=1, 10 do
- list[i][j] = ""
- end
- list[i][10] = "Todo List"
- end
- local itemcount = {}
- for i=1, 9 do
- itemcount[i] = 0
- end
- local page = 1
- todoitemfile = "list"
- local function loadList()
- local loadingpage = 0
- if fs.exists(todoitemfile) then
- file = fs.open(todoitemfile, "r")
- for i=1, 9 do
- itemcount[i] = 0
- for j=1, 10 do
- list[i][j] = file.readLine()
- if list[i][j] ~= "" and j ~= 10 then
- itemcount[i] = itemcount[i] + 1
- end
- end
- end
- file.close()
- end
- end
- local function saveList()
- file = fs.open(todoitemfile, "w")
- for i=1, 9 do
- for j=1, 10 do
- file.writeLine(list[i][j])
- end
- end
- file.close()
- end
- local function input()
- ip[1], ip[2], ip[3], ip[4], ip[5] = os.pullEvent()
- if (ip[1] == "glasses_chat_command") then
- return true
- end
- return false
- end
- local function update()
- if ip[5] ~= nil then
- if string.sub(ip[5], 1, 3) == "add" then
- if itemcount[page] < 9 and string.len(ip[5]) > 4 then
- itemcount[page] = itemcount[page] + 1
- list[page][itemcount[page]] = string.sub(ip[5], 5)
- print("Added item: "..string.sub(ip[5], 5))
- end
- elseif string.sub(ip[5], 1, 6) == "finish" then
- if tonumber(string.sub(ip[5], 8, 8)) ~= nil then
- finitem = tonumber(string.sub(ip[5], 8, 8))
- if itemcount[page] > 0 then
- if finitem > 0 then
- list[page][finitem] = list[page][finitem] .. " - [Done]"
- end
- end
- end
- print("Item finished: "..string.sub(ip[5], 8, 8))
- elseif string.sub(ip[5], 1, 5) == "clear" then
- if string.len(ip[5]) < 7 then
- print("clearing page "..page)
- for i=1, itemcount[page] do
- list[page][i] = ""
- end
- itemcount[page] = 0
- print("Page cleared: "..page)
- elseif tonumber(string.sub(ip[5], 7, 7)) ~= nil then
- pagetoclear = tonumber(string.sub(ip[5], 7, 7))
- if pagetoclear >= 1 and pagetoclear <= 5 then
- print("clearing page "..string.sub(ip[5], 7, 7))
- for i=1, itemcount[pagetoclear] do
- list[pagetoclear][i] = ""
- end
- itemcount[pagetoclear] = 0
- print("Page cleared: "..pagetoclear)
- end
- end
- elseif string.sub(ip[5], 1, 5) == "title" then
- if string.len(ip[5]) > 6 then
- list[page][10] = string.sub(ip[5], 7)
- print("Page "..page.." title set to "..string.sub(ip[5], 7))
- end
- elseif string.sub(ip[5], 1, 6) == "delete" then
- if string.len(ip[5]) >= 8 then
- alter = tonumber(string.sub(ip[5], 8, 8))
- if alter >= 1 and alter <= itemcount[page] then
- list[page][alter] = ""
- for i=alter, itemcount[page]-1 do
- list[page][i] = list[page][i+1]
- end
- list[page][itemcount[page]] = ""
- itemcount[page] = itemcount[page] - 1
- print("Todo item deleted: "..alter)
- end
- end
- elseif string.sub(ip[5], 1, 4) == "page" then
- if string.len(ip[5]) >= 6 then
- newpage = tonumber(string.sub(ip[5], 6, 6))
- if newpage >= 1 and newpage <= 9 then
- page = newpage
- print("Paged turned to: "..page)
- end
- end
- elseif ip[5] == "Destroy bad todo list" then
- rs.setOutput("bottom", true)
- else
- if string.len(ip[5]) > 0 and itemcount[page] < 9 then
- itemcount[page] = itemcount[page] + 1
- list[page][itemcount[page]] = ip[5]
- print("New item: "..ip[5])
- end
- end
- end
- end
- local function render()
- termbridge.clear()
- termbridge.addBox(5, 5, 305, itemcount[page] * 8 + 11, 0x000000, 0.5)
- termbridge.addText(8, 7, list[page][10].." ("..page..")", 0xFFFFFF)
- if itemcount[page] ~= 0 then
- for i=1, itemcount[page] do
- termbridge.addText(8, i * 8 + 8, i..": "..list[page][i], 0xFFFFFF)
- end
- end
- termbridge.sync()
- end
- local function mainLoop()
- loadList()
- render()
- while true do
- if input() then
- update()
- render()
- saveList()
- end
- end
- end
- local function findBridge()
- for i,v in pairs(peripheral.getNames()) do
- if (peripheral.getType(v) == "openperipheral_bridge") then
- termbridge = peripheral.wrap(v)
- return true
- end
- end
- return false
- end
- if findBridge() then
- mainLoop()
- else print("no connected terminal bridge") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement