HelmDeepYT

Todo 1

May 31st, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 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", completed = false, inProgress = false}
  32. else
  33. todo = textutils.unserialize(data)
  34. end
  35. else
  36. todo[1] = {text = "Make a todo list", completed = false, inProgress = false}
  37. end
  38. end
  39.  
  40. function saveTODO()
  41. local file = fs.open("todolist", "w")
  42. file.write(textutils.serialize(todo))
  43. file.close()
  44. end
  45.  
  46. function displayPage(p)
  47. textCol(colors.white)
  48. term.setCursorPos(1, 2)
  49. for i = (1 + (p - 1) * line), (p * line) do
  50. if todo[i] ~= nil then
  51. local status = "Not Completed"
  52. if todo[i].completed then
  53. status = "Completed"
  54. end
  55. if todo[i].inProgress then
  56. status = "In Progress"
  57. end
  58. textCol(colors.white)
  59. term.write("[" .. status .. "] ")
  60. textCol(colors.gray)
  61. print(" " .. todo[i].text)
  62. end
  63. end
  64. end
  65.  
  66. function printButtons()
  67. local buttonY = height - 1
  68.  
  69. -- Add Item Button
  70. term.setCursorPos(2, buttonY)
  71. textCol(colors.green)
  72. term.write("Add Item")
  73.  
  74. -- Remove Completed Button
  75. term.setCursorPos(15, buttonY)
  76. textCol(colors.red)
  77. term.write("Remove Completed")
  78.  
  79. -- Cycle Page Button
  80. term.setCursorPos(width - 11, buttonY)
  81. textCol(colors.yellow)
  82. term.write("Cycle Page")
  83. end
  84.  
  85. function addItem()
  86. term.clear()
  87. term.setCursorPos(2, 2)
  88. print("Please enter a new item:")
  89. local input = read()
  90. local newItem = {text = input, completed = false, inProgress = false}
  91.  
  92. table.insert(todo, 1, newItem)
  93. saveTODO()
  94.  
  95. term.clear()
  96. term.setCursorPos(2, 2)
  97. textCol(colors.white)
  98. print("Thanks! Item added.")
  99. os.sleep(2)
  100. end
  101.  
  102. function removeCompletedTasks()
  103. local newTodo = {} -- Create a new table for non-completed tasks
  104. for i = 1, #todo do
  105. if not todo[i].completed then
  106. table.insert(newTodo, todo[i])
  107. end
  108. end
  109. todo = newTodo -- Update the 'todo' table with the filtered list
  110. saveTODO()
  111. end
  112.  
  113. openTODO()
  114.  
  115. -- Initial printing
  116. term.clear()
  117. printButtons()
  118. displayPage(1)
  119.  
  120. while true do
  121. local event, but, x, y
  122.  
  123. if mon == term.native() then
  124. event, but, x, y = os.pullEvent("mouse_click")
  125. else
  126. event, _, x, y = os.pullEvent("monitor_touch")
  127. end
  128.  
  129. if y == height - 1 then
  130. if x <= 13 then
  131. addItem()
  132. elseif x >= 15 and x <= 30 then
  133. removeCompletedTasks()
  134. elseif x >= width - 11 then
  135. if currentPage >= #todo / line then
  136. currentPage = 1
  137. else
  138. currentPage = currentPage + 1
  139. end
  140. end
  141. elseif y >= 2 and y <= height - 3 then
  142. local itm = (currentPage - 1) * line + y - 1
  143. if todo[itm] ~= nil then
  144. todo[itm].completed = not todo[itm].completed
  145. todo[itm].inProgress = false
  146. saveTODO()
  147. end
  148.  
  149. term.clear()
  150. printButtons()
  151. term.setCursorPos(1, 1)
  152. displayPage(currentPage)
  153. end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment