robathome

ChestMon - remote

Oct 29th, 2025 (edited)
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. -- inv_client_loop.lua (Advanced Wireless Pocket Computer)
  2. local PROTO = "inv"
  3. local REFRESH = 2 -- seconds
  4. local side = peripheral.getName(peripheral.find("modem")) or error("no modem")
  5. rednet.open(side)
  6.  
  7. local items, top = {}, 1
  8.  
  9. local function fetch()
  10. rednet.broadcast("list", PROTO)
  11. local id, resp = rednet.receive(PROTO, 3)
  12. if id and type(resp) == "table" then items = resp end
  13. end
  14.  
  15. local function draw()
  16. term.clear()
  17. term.setCursorPos(1,1)
  18. print(("Items (%d) — %s"):format(#items, textutils.formatTime(os.clock(), true)))
  19. print(("-"):rep(28))
  20. local w,h = term.getSize()
  21. local visible = h - 3
  22. for i = 0, visible-1 do
  23. local row = items[top + i]
  24. if not row then break end
  25. term.setCursorPos(1, 3 + i)
  26. print(("%-24s x%d"):format(row.name or "?", row.count or 0))
  27. end
  28. term.setCursorPos(1, h)
  29. print("[Q] quit [↑/↓] scroll")
  30. end
  31.  
  32. local function clampTop()
  33. local w,h = term.getSize()
  34. local visible = h - 3
  35. if top < 1 then top = 1 end
  36. local maxTop = math.max(1, #items - visible + 1)
  37. if top > maxTop then top = maxTop end
  38. end
  39.  
  40. -- main loop
  41. while true do
  42. fetch()
  43. clampTop()
  44. draw()
  45.  
  46. -- wait up to REFRESH seconds for input; otherwise refresh again
  47. local t = os.startTimer(REFRESH)
  48. while true do
  49. local e, a = os.pullEvent()
  50. if e == "timer" and a == t then
  51. break
  52. elseif e == "key" then
  53. if a == keys.q then
  54. term.clear(); term.setCursorPos(1,1); return
  55. elseif a == keys.up then
  56. top = top - 1; clampTop(); draw()
  57. elseif a == keys.down then
  58. top = top + 1; clampTop(); draw()
  59. end
  60. end
  61. end
  62. end
  63.  
Advertisement
Add Comment
Please, Sign In to add comment