Advertisement
NickM13

Todo list (ComputerCraft)

May 30th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. --[[    Written by NickM13
  2.     Todo list for ComputerCraft terminal glasses
  3.    
  4.     Command list:
  5.     $$add       [New TODO Item]
  6.     $$finish    [Item ID(1-9)]
  7.     $$clear     [Page(1-9) or nothing for current page]
  8.     $$title     [New Page Title]
  9.     $$delete    [Item ID(1-9)]
  10.     $$page      [Page ID(1-9)]
  11. --]]
  12.  
  13.  
  14.  
  15. local termbridge = nil
  16.  
  17. local ip = {}
  18.  
  19. local list = {}
  20. for i=1, 9 do
  21.     list[i] = {}
  22.     for j=1, 10 do
  23.         list[i][j] = ""
  24.     end
  25.     list[i][10] = "Todo List"
  26. end
  27.  
  28. local itemcount = {}
  29. for i=1, 9 do
  30.     itemcount[i] = 0
  31. end
  32.  
  33. local page = 1
  34.  
  35. todoitemfile = "list"
  36.  
  37. local function loadList()
  38.     local loadingpage = 0
  39.     if fs.exists(todoitemfile) then
  40.         file = fs.open(todoitemfile, "r")
  41.         for i=1, 9 do
  42.             itemcount[i] = 0
  43.             for j=1, 10 do
  44.                 list[i][j] = file.readLine()
  45.                
  46.                 if list[i][j] ~= "" and j ~= 10 then
  47.                     itemcount[i] = itemcount[i] + 1
  48.                 end
  49.             end
  50.         end
  51.         file.close()
  52.     end
  53. end
  54.  
  55. local function saveList()
  56.     file = fs.open(todoitemfile, "w")
  57.     for i=1, 9 do
  58.         for j=1, 10 do
  59.             file.writeLine(list[i][j])
  60.         end
  61.     end
  62.     file.close()
  63. end
  64.  
  65. local function input()
  66.     ip[1], ip[2], ip[3], ip[4], ip[5] = os.pullEvent()
  67.     if (ip[1] == "glasses_chat_command") then
  68.         return true
  69.     end
  70.     return false
  71. end
  72.  
  73. local function update()
  74.     if ip[5] ~= nil then
  75.         if string.sub(ip[5], 1, 3) == "add" then
  76.             if itemcount[page] < 9 and string.len(ip[5]) > 4 then
  77.                 itemcount[page] = itemcount[page] + 1
  78.                 list[page][itemcount[page]] = string.sub(ip[5], 5)
  79.                 print("Added item: "..string.sub(ip[5], 5))
  80.             end
  81.         elseif string.sub(ip[5], 1, 6) == "finish" then
  82.             if tonumber(string.sub(ip[5], 8, 8)) ~= nil then
  83.                 finitem = tonumber(string.sub(ip[5], 8, 8))
  84.                 if itemcount[page] > 0 then
  85.                     if finitem > 0 then
  86.                         list[page][finitem] = list[page][finitem] .. " - [Done]"
  87.                     end
  88.                 end
  89.             end
  90.             print("Item finished: "..string.sub(ip[5], 8, 8))
  91.         elseif string.sub(ip[5], 1, 5) == "clear" then
  92.             if string.len(ip[5]) < 7 then
  93.                 print("clearing page "..page)
  94.                 for i=1, itemcount[page] do
  95.                     list[page][i] = ""
  96.                 end
  97.                 itemcount[page] = 0
  98.                 print("Page cleared: "..page)
  99.             elseif tonumber(string.sub(ip[5], 7, 7)) ~= nil then
  100.                 pagetoclear = tonumber(string.sub(ip[5], 7, 7))
  101.                 if pagetoclear >= 1 and pagetoclear <= 5 then
  102.                     print("clearing page "..string.sub(ip[5], 7, 7))
  103.                     for i=1, itemcount[pagetoclear] do
  104.                         list[pagetoclear][i] = ""
  105.                     end
  106.                     itemcount[pagetoclear] = 0
  107.                     print("Page cleared: "..pagetoclear)
  108.                 end
  109.             end
  110.         elseif string.sub(ip[5], 1, 5) == "title" then
  111.             if string.len(ip[5]) > 6 then
  112.                 list[page][10] = string.sub(ip[5], 7)
  113.                 print("Page "..page.." title set to "..string.sub(ip[5], 7))
  114.             end
  115.         elseif string.sub(ip[5], 1, 6) == "delete" then
  116.             if string.len(ip[5]) >= 8 then
  117.                 alter = tonumber(string.sub(ip[5], 8, 8))
  118.                 if alter >= 1 and alter <= itemcount[page] then
  119.                     list[page][alter] = ""
  120.                     for i=alter, itemcount[page]-1 do
  121.                         list[page][i] = list[page][i+1]
  122.                     end
  123.                     list[page][itemcount[page]] = ""
  124.                     itemcount[page] = itemcount[page] - 1
  125.                     print("Todo item deleted: "..alter)
  126.                 end
  127.             end
  128.         elseif string.sub(ip[5], 1, 4) == "page" then
  129.             if string.len(ip[5]) >= 6 then
  130.                 newpage = tonumber(string.sub(ip[5], 6, 6))
  131.                 if newpage >= 1 and newpage <= 9 then
  132.                     page = newpage
  133.                     print("Paged turned to: "..page)
  134.                 end
  135.             end
  136.         elseif ip[5] == "Destroy bad todo list" then
  137.             rs.setOutput("bottom", true)
  138.         else
  139.             if string.len(ip[5]) > 0 and itemcount[page] < 9 then
  140.                 itemcount[page] = itemcount[page] + 1
  141.                 list[page][itemcount[page]] = ip[5]
  142.                 print("New item: "..ip[5])
  143.             end
  144.         end
  145.     end
  146. end
  147.  
  148. local function render()
  149.     termbridge.clear()
  150.    
  151.     termbridge.addBox(5, 5, 305, itemcount[page] * 8 + 11, 0x000000, 0.5)
  152.     termbridge.addText(8, 7, list[page][10].." ("..page..")", 0xFFFFFF)
  153.     if itemcount[page] ~= 0 then
  154.         for i=1, itemcount[page] do
  155.             termbridge.addText(8, i * 8 + 8, i..": "..list[page][i], 0xFFFFFF)
  156.         end
  157.     end
  158.    
  159.     termbridge.sync()
  160. end
  161.  
  162. local function mainLoop()
  163.     loadList()
  164.     render()
  165.    
  166.     while true do
  167.         if input() then
  168.             update()
  169.             render()
  170.             saveList()
  171.         end
  172.     end
  173. end
  174.  
  175. local function findBridge()
  176.     for i,v in pairs(peripheral.getNames()) do
  177.         if (peripheral.getType(v) == "openperipheral_bridge") then
  178.             termbridge = peripheral.wrap(v)
  179.             return true
  180.         end
  181.     end
  182.     return false
  183. end
  184.  
  185. if findBridge() then
  186.     mainLoop()
  187. else print("no connected terminal bridge") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement