SHARE
    TWEET
    AnttiV

    Untitled

    Apr 21st, 2025
    468
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Lua 2.07 KB | Gaming | 0 0
    1. local monitorSide = "top"
    2. local filePath = "/disk/items"
    3. local refreshTime = 30
    4. local monitor = peripheral.wrap(monitorSide)
    5. if not monitor then
    6. print("Error: No monitor on side '" .. monitorSide .. "'")
    7. return
    8. end
    9. monitor.setTextScale(1)
    10. local function readItems()
    11. if not fs.exists(filePath) then return {} end
    12. local file = fs.open(filePath, "r")
    13. local items = {}
    14. local line = file.readLine()
    15. while line do
    16. table.insert(items, line)
    17. line = file.readLine()
    18. end
    19. file.close()
    20. return items
    21. end
    22. local function writeItems(items)
    23. local file = fs.open(filePath, "w")
    24. for _, item in ipairs(items) do
    25. file.writeLine(item)
    26. end
    27. file.close()
    28. end
    29. local function displayItems(items)
    30. monitor.setBackgroundColor(colors.black)
    31. monitor.setTextColor(colors.white)
    32. monitor.clear()
    33. for i, item in ipairs(items) do
    34. monitor.setCursorPos(1, i) -- item text
    35. monitor.write(i .. ". " .. item)
    36. monitor.setCursorPos(30, i) -- 'button'
    37. monitor.setTextColor(colors.red)
    38. monitor.write("[X]")
    39. monitor.setTextColor(colors.white)
    40. end
    41. end
    42. local function detectClick(items, x, y)
    43. -- Assuming 'button' starts at x=30, ends at 32 or 33
    44. if y >=1 and y <= #items then
    45. if x >= 30 and x <= 32 then
    46. -- Remove the clicked item
    47. table.remove(items, y)
    48. writeItems(items)
    49. displayItems(items)
    50. print("Removed item at line " .. y)
    51. end
    52. end
    53. end
    54. -- Main Loop
    55. while true do
    56. local items = readItems()
    57. displayItems(items)
    58. local timer = os.startTimer(refreshTime)
    59. while true do
    60. local event, p1, p2, p3 = os.pullEvent()
    61. if event == "monitor_touch" then
    62. local side, x, y = p1, p2, p3
    63. if side == monitorSide then
    64. detectClick(items, x, y)
    65. end
    66. elseif event == "timer" and p1 == timer then
    67. break -- Time to refresh the list
    68. end
    69. end
    70. end
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /