Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define the size and position of the monitor window
- local monitor = peripheral.wrap("top")
- local width, height = monitor.getSize()
- local x, y = 1, 1
- -- Define the todo list data structure
- local todoList = {}
- -- Define the maximum number of items to display on one page
- local maxItemsPerPage = height - 2 -- accounting for borders and prompt
- -- Define the background and text colors
- local bgColor = colors.lightGray
- local textColor = colors.white
- -- Define the colors for the "+" button
- local addButtonBgColor = colors.green
- local addButtonTextColor = bgColor
- -- Define a function to draw the todo list on the monitor
- local function drawTodoList(page)
- -- Clear the monitor
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- monitor.setCursorPos(1,1)
- -- Draw the todo list items for the current page
- local startIndex = (page - 1) * maxItemsPerPage + 1
- local endIndex = math.min(page * maxItemsPerPage, #todoList)
- for i = startIndex, endIndex do
- local listItem = todoList[i]
- monitor.write("- " .. listItem)
- monitor.setCursorPos(1, monitor.getCursorY() + 1)
- end
- -- Draw the "+" button at the bottom of the screen
- monitor.setCursorPos(width - 1, height)
- monitor.setBackgroundColor(addButtonBgColor)
- monitor.setTextColor(addButtonTextColor)
- monitor.write("+")
- end
- -- Define a function to handle user input (clicks on the monitor)
- local function handleInput()
- -- Define the current page at the beginning of the function
- local page = 1
- while true do
- -- Wait for a user input event
- local event, player, x, y = os.pullEvent("monitor_touch")
- -- Check if the "+" button was clicked
- if x == width and y == height then
- -- Prompt the user to enter a task, and add it to the todo list if entered
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(textColor)
- term.write("Enter a new task: ")
- local task = io.read()
- if task ~= "" then
- table.insert(todoList, task)
- end
- -- Redraw the todo list on the monitor
- drawTodoList(1)
- else
- -- Check if a todo list item was clicked, and remove it if so
- local itemIndex = y + maxItemsPerPage * (page - 1)
- if itemIndex <= #todoList then
- table.remove(todoList, itemIndex)
- drawTodoList(page)
- end
- end
- end
- end
- -- Start the program by drawing the initial todo list and handling user input
- drawTodoList(1)
- handleInput()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement