HelmDeepYT

Todo 2

May 31st, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. shell.run("label set ToDo")
  2. local periList = peripheral.getNames()
  3. local mon = term.native()
  4. for i = 1, #periList do
  5. if peripheral.getType(periList[i]) == "monitor" then
  6. mon = peripheral.wrap(periList[i])
  7. print("Monitor wrapped as... " .. periList[i])
  8. end
  9. end
  10.  
  11. term.redirect(mon)
  12.  
  13. local currentPage = 1
  14. local width, height = mon.getSize()
  15. local line = height - 3
  16.  
  17. function textCol(color)
  18. if mon.isColor() then
  19. term.setTextColor(color)
  20. end
  21. end
  22.  
  23. local todo = {} -- Initialize the 'todo' table as an empty table
  24.  
  25. function openTODO()
  26. local file = fs.open("todolist", "r")
  27. if file ~= nil then
  28. local data = file.readAll()
  29. file.close()
  30. if textutils.unserialize(data) == nil then
  31. todo[1] = {text = "Make a todo list", status = "Not Completed"}
  32. else
  33. todo = textutils.unserialize(data)
  34. -- Ensure "status" is set for older tasks
  35. for _, task in pairs(todo) do
  36. task.status = task.status or "Not Completed"
  37. end
  38. end
  39. else
  40. todo[1] = {text = "Make a todo list", status = "Not Completed"}
  41. end
  42. end
  43.  
  44. function saveTODO()
  45. local file = fs.open("todolist", "w")
  46. file.write(textutils.serialize(todo))
  47. file.close()
  48. end
  49.  
  50. function displayPage(p)
  51. textCol(colors.white)
  52. term.setCursorPos(1, 2)
  53. for i = (1 + (p - 1) * line), (p * line) do
  54. if todo[i] ~= nil then
  55. local status = todo[i].status
  56. local textColor = colors.white
  57. if status == "Completed" then
  58. textColor = colors.green
  59. elseif status == "In Progress" then
  60. textColor = colors.orange
  61. end
  62. textCol(textColor)
  63. term.write("[" .. status .. "] ")
  64. textCol(colors.gray)
  65. print(" " .. todo[i].text)
  66. end
  67. end
  68. end
  69.  
  70. function printButtons()
  71. local buttonY = height - 1
  72.  
  73. -- Add Item Button
  74. term.setCursorPos(2, buttonY)
  75. textCol(colors.green)
  76. term.write("Add Item")
  77.  
  78. -- Remove Completed Button
  79. term.setCursorPos(15, buttonY)
  80. textCol(colors.red)
  81. term.write("Remove Completed")
  82.  
  83. -- Cycle Page Button
  84. term.setCursorPos(width - 11, buttonY)
  85. textCol(colors.yellow)
  86. term.write("Cycle Page")
  87. end
  88.  
  89. function addItem()
  90. term.clear()
  91. term.setCursorPos(2, 2)
  92. print("Please enter a new item:")
  93. local input = read()
  94. local newItem = {text = input, status = "Not Completed"}
  95.  
  96. table.insert(todo, 1, newItem)
  97. saveTODO()
  98.  
  99. term.clear()
  100. term.setCursorPos(2, 2)
  101. textCol(colors.white)
  102. print("Thanks! Item added.")
  103. os.sleep(2)
  104. end
  105.  
  106. function removeCompletedTasks()
  107. local newTodo = {} -- Create a new table for non-completed tasks
  108. for i = 1, #todo do
  109. if todo[i].status ~= "Completed" then
  110. table.insert(newTodo, todo[i])
  111. end
  112. end
  113. todo = newTodo -- Update the 'todo' table with the filtered list
  114. saveTODO()
  115. end
  116.  
  117. openTODO()
  118.  
  119. -- Initial printing
  120. term.clear()
  121. printButtons()
  122. displayPage(1)
  123.  
  124. while true do
  125. local event, but, x, y
  126.  
  127. if mon == term.native() then
  128. event, but, x, y = os.pullEvent("mouse_click")
  129. else
  130. event, _, x, y = os.pullEvent("monitor_touch")
  131. end
  132.  
  133. if y == height - 1 then
  134. if x <= 13 then
  135. addItem()
  136. elseif x >= 15 and x <= 30 then
  137. removeCompletedTasks()
  138. elseif x >= width - 11 then
  139. if currentPage >= #todo / line then
  140. currentPage = 1
  141. else
  142. currentPage = currentPage + 1
  143. end
  144. end
  145. elseif y >= 2 and y <= height - 3 then
  146. local itm = (currentPage - 1) * line + y - 1
  147. if todo[itm] ~= nil then
  148. if todo[itm].status == "Not Completed" then
  149. todo[itm].status = "In Progress"
  150. elseif todo[itm].status == "In Progress" then
  151. todo[itm].status = "Completed"
  152. else
  153. todo[itm].status = "Not Completed"
  154. end
  155. saveTODO()
  156. end
  157.  
  158. term.clear()
  159. printButtons()
  160. term.setCursorPos(1, 1)
  161. displayPage(currentPage)
  162. end
  163. end
Add Comment
Please, Sign In to add comment