Advertisement
GNOOR1S

TODO List New and Improved | Advance Monitors Computercraft

Jun 7th, 2023 (edited)
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | Gaming | 0 0
  1. -- Define the size and position of the monitor window
  2. local monitor = peripheral.wrap("top")
  3. local width, height = monitor.getSize()
  4. local x, y = 1, 1
  5.  
  6. -- Define the todo list data structure
  7. local todoList = {}
  8.  
  9. -- Define the maximum number of items to display on one page
  10. local maxItemsPerPage = height - 2  -- accounting for borders and prompt
  11.  
  12. -- Define the background and text colors
  13. local bgColor = colors.lightGray
  14. local textColor = colors.white
  15.  
  16. -- Define the colors for the "+" button
  17. local addButtonBgColor = colors.green
  18. local addButtonTextColor = bgColor
  19.  
  20. -- Define a function to draw the todo list on the monitor
  21. local function drawTodoList(page)
  22.   -- Clear the monitor
  23.   monitor.setBackgroundColor(bgColor)
  24.   monitor.clear()
  25.   monitor.setCursorPos(1,1)
  26.  
  27.   -- Draw the todo list items for the current page
  28.   local startIndex = (page - 1) * maxItemsPerPage + 1
  29.   local endIndex = math.min(page * maxItemsPerPage, #todoList)
  30.   for i = startIndex, endIndex do
  31.     local listItem = todoList[i]
  32.     monitor.write("- " .. listItem)
  33.     monitor.setCursorPos(1, monitor.getCursorY() + 1)
  34.   end
  35.  
  36.   -- Draw the "+" button at the bottom of the screen
  37.   monitor.setCursorPos(width - 1, height)
  38.   monitor.setBackgroundColor(addButtonBgColor)
  39.   monitor.setTextColor(addButtonTextColor)
  40.   monitor.write("+")
  41. end
  42.  
  43. -- Define a function to handle user input (clicks on the monitor)
  44. local function handleInput()
  45.   -- Define the current page at the beginning of the function
  46.   local page = 1
  47.  
  48.   while true do
  49.     -- Wait for a user input event
  50.     local event, player, x, y = os.pullEvent("monitor_touch")
  51.  
  52.     -- Check if the "+" button was clicked
  53.     if x == width and y == height then
  54.       -- Prompt the user to enter a task, and add it to the todo list if entered
  55.       term.clear()
  56.       term.setCursorPos(1, 1)
  57.       term.setTextColor(textColor)
  58.       term.write("Enter a new task: ")
  59.       local task = io.read()
  60.       if task ~= "" then
  61.         table.insert(todoList, task)
  62.       end
  63.  
  64.       -- Redraw the todo list on the monitor
  65.       drawTodoList(1)
  66.     else
  67.       -- Check if a todo list item was clicked, and remove it if so
  68.       local itemIndex = y + maxItemsPerPage * (page - 1)
  69.       if itemIndex <= #todoList then
  70.         table.remove(todoList, itemIndex)
  71.         drawTodoList(page)
  72.       end
  73.     end
  74.   end
  75. end
  76.  
  77. -- Start the program by drawing the initial todo list and handling user input
  78. drawTodoList(1)
  79. handleInput()
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement