Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitorSide = "top"
- local filePath = "/disk/items"
- local refreshTime = 30
- local monitor = peripheral.wrap(monitorSide)
- if not monitor then
- print("Error: No monitor on side '" .. monitorSide .. "'")
- return
- end
- monitor.setTextScale(1)
- local function readItems()
- if not fs.exists(filePath) then return {} end
- local file = fs.open(filePath, "r")
- local items = {}
- local line = file.readLine()
- while line do
- table.insert(items, line)
- line = file.readLine()
- end
- file.close()
- return items
- end
- local function writeItems(items)
- local file = fs.open(filePath, "w")
- for _, item in ipairs(items) do
- file.writeLine(item)
- end
- file.close()
- end
- local function displayItems(items)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- for i, item in ipairs(items) do
- monitor.setCursorPos(1, i) -- item text
- monitor.write(i .. ". " .. item)
- monitor.setCursorPos(30, i) -- 'button'
- monitor.setTextColor(colors.red)
- monitor.write("[X]")
- monitor.setTextColor(colors.white)
- end
- end
- local function detectClick(items, x, y)
- -- Assuming 'button' starts at x=30, ends at 32 or 33
- if y >=1 and y <= #items then
- if x >= 30 and x <= 32 then
- -- Remove the clicked item
- table.remove(items, y)
- writeItems(items)
- displayItems(items)
- print("Removed item at line " .. y)
- end
- end
- end
- -- Main Loop
- while true do
- local items = readItems()
- displayItems(items)
- local timer = os.startTimer(refreshTime)
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "monitor_touch" then
- local side, x, y = p1, p2, p3
- if side == monitorSide then
- detectClick(items, x, y)
- end
- elseif event == "timer" and p1 == timer then
- break -- Time to refresh the list
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment