TyanColte

AE2Monitor

Feb 1st, 2025 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. -- Find peripherals
  2. local meBridge = peripheral.find("meBridge")
  3. local monitor = peripheral.find("monitor")
  4.  
  5. if not meBridge then
  6.     error("ME Bridge not found!")
  7. end
  8.  
  9. if not monitor then
  10.     error("Monitor not found!")
  11. end
  12.  
  13. -- Set up monitor
  14. monitor.setTextScale(0.75) -- Medium text size
  15. monitor.setCursorBlink(false)
  16. monitor.clear()
  17.  
  18. -- Function to draw a two-color progress bar
  19. local function drawProgressBar(x, y, width, height, progress)
  20.     local filledWidth = math.floor(width * math.max(0, math.min(1, progress))) -- Clamp between 0 and 1
  21.  
  22.     -- Draw background (gray)
  23.     monitor.setBackgroundColor(colors.gray)
  24.     monitor.setTextColor(colors.white)
  25.     for i = 0, height - 1 do
  26.         monitor.setCursorPos(x, y + i)
  27.         monitor.write(string.rep(" ", width))
  28.     end
  29.  
  30.     -- Draw filled progress (green)
  31.     monitor.setBackgroundColor(colors.green)
  32.     for i = 0, height - 1 do
  33.         monitor.setCursorPos(x, y + i)
  34.         monitor.write(string.rep(" ", filledWidth))
  35.     end
  36.  
  37.     -- Reset background color
  38.     monitor.setBackgroundColor(colors.black)
  39. end
  40.  
  41. -- Store previous values to minimize unnecessary updates
  42. local prevUsed, prevTotal
  43.  
  44. -- Main loop
  45. while true do
  46.     -- Get storage data
  47.     local totalStorage = meBridge.getTotalItemStorage()
  48.     local usedStorage = meBridge.getUsedItemStorage()
  49.  
  50.     -- Prevent division by zero
  51.     local progress = usedStorage / (totalStorage > 0 and totalStorage or 1)
  52.  
  53.     -- Format numbers for alignment
  54.     local usedStr = string.format("%10d", usedStorage)  -- Right-align to 10 characters
  55.     local totalStr = string.format("%10d", totalStorage) -- Right-align to 10 characters
  56.  
  57.     -- Update only if values have changed
  58.     if usedStorage ~= prevUsed or totalStorage ~= prevTotal then
  59.         monitor.clear()
  60.        
  61.         -- Display storage info
  62.         monitor.setCursorPos(2, 1)
  63.         monitor.write("ME Storage Monitor")
  64.  
  65.         monitor.setCursorPos(2, 3)
  66.         monitor.write("Used:  " .. usedStr .. " items")
  67.  
  68.         monitor.setCursorPos(2, 4)
  69.         monitor.write("Total: " .. totalStr .. " items")
  70.  
  71.         -- Draw progress bar
  72.         drawProgressBar(2, 6, 30, 2, progress)
  73.  
  74.         -- Store previous values
  75.         prevUsed = usedStorage
  76.         prevTotal = totalStorage
  77.     end
  78.  
  79.     -- Wait before updating
  80.     sleep(1)
  81. end
  82.  
Advertisement
Add Comment
Please, Sign In to add comment