Advertisement
lewis_FEUP

Untitled

Mar 23rd, 2025 (edited)
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. ---
  2. --- Made for the Advanced Peripherals documentation - Can be used in production
  3. --- Created by Srendi - https://github.com/SirEndii
  4. --- DateTime: 24.12.2023 (No)
  5. --- Link: tbd
  6. ---
  7.  
  8. mon = peripheral.find("monitor")
  9. me = peripheral.find("meBridge")
  10.  
  11. data = {
  12.     drives = 0,
  13.     totalBytes = 0,
  14.     usedBytes = 0,
  15.     totalCells = 0,
  16. }
  17.  
  18. local label = "ME Drives"
  19.  
  20. local monX, monY
  21.  
  22. os.loadAPI("medrives/api/bars.lua")
  23.  
  24. function prepare()
  25.     mon.clear()
  26.     monX, monY = mon.getSize()
  27.     if monX < 38 or monY < 25 then
  28.         error("Monitor is too small, we need a size of 39x and 26y minimum.")
  29.     end
  30.     mon.setPaletteColor(colors.red, 0xba2525)
  31.     mon.setBackgroundColor(colors.black)
  32.     mon.setCursorPos((monX/2)-(#label/2),1)
  33.     mon.setTextScale(1)
  34.     mon.write(label)
  35.     mon.setCursorPos(1,1)
  36.     drawBox(2, monX - 1, 3, monY - 10, "Drives", colors.gray, colors.lightGray)
  37.     drawBox(2, monX - 1, monY - 8, monY - 1, "Stats", colors.gray, colors.lightGray)
  38.     addBars()
  39. end
  40.  
  41. function addBars()
  42.     drives = me.listCells()
  43.     data.drives = #drives
  44.     for i=1, #drives do
  45.         x = 3*i
  46.         full = drives[i].totalBytes
  47.         print(full)
  48.         print(drives[i].usedBytes)
  49.         bars.add(""..i,"ver", full, drives[i].usedBytes, 1+x, 5, 1, monY - 16, colors.red, colors.green)
  50.         mon.setCursorPos(x+1, monY - 11)
  51.         --mon.write(string.format(i))
  52.         data.totalBytes = data.totalBytes + drives[i].totalBytes
  53.         data.usedBytes = data.usedBytes + drives[i].usedBytes
  54.         data.totalCells = data.totalCells + #drives[i].cells
  55.     end
  56.     bars.construct(mon)
  57.     bars.screen()
  58. end
  59.  
  60.  
  61. function drawBox(xMin, xMax, yMin, yMax, title, bcolor, tcolor)
  62.     mon.setBackgroundColor(bcolor)
  63.     for xPos = xMin, xMax, 1 do
  64.         mon.setCursorPos(xPos, yMin)
  65.         mon.write(" ")
  66.     end
  67.     for yPos = yMin, yMax, 1 do
  68.         mon.setCursorPos(xMin, yPos)
  69.         mon.write(" ")
  70.         mon.setCursorPos(xMax, yPos)
  71.         mon.write(" ")
  72.  
  73.     end
  74.     for xPos = xMin, xMax, 1 do
  75.         mon.setCursorPos(xPos, yMax)
  76.         mon.write(" ")
  77.     end
  78.     mon.setCursorPos(xMin+2, yMin)
  79.     mon.setBackgroundColor(colors.black)
  80.     mon.setTextColor(tcolor)
  81.     mon.write(" ")
  82.     mon.write(title)
  83.     mon.write(" ")
  84.     mon.setTextColor(colors.white)
  85. end
  86.  
  87. function clear(xMin,xMax, yMin, yMax)
  88.     mon.setBackgroundColor(colors.black)
  89.     for xPos = xMin, xMax, 1 do
  90.         for yPos = yMin, yMax, 1 do
  91.             mon.setCursorPos(xPos, yPos)
  92.             mon.write(" ")
  93.         end
  94.     end
  95. end
  96.  
  97. function getUsage()
  98.     return (data.usedBytes * 100) / data.totalBytes
  99. end
  100.  
  101. function comma_value(n) -- credit http://richard.warburton.it
  102.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  103.     return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
  104. end
  105.  
  106. function roundToDecimal(num, decimalPlaces)
  107.     local mult = 10^(decimalPlaces or 0)
  108.     return math.floor(num * mult + 0.5) / mult
  109. end
  110.  
  111. function updateStats()
  112.     newDrives = me.listDrives()
  113.     data.totalBytes = 0;
  114.     data.usedBytes = 0;
  115.     data.totalCells = 0;
  116.  
  117.     if newDrives == nil then
  118.         data.drives = 0
  119.         print("The given table is nil, but why?")
  120.         return
  121.     end
  122.  
  123.     if newDrives == nil or #newDrives == 0 then
  124.         clear(3,monX - 3,4,monY - 12)
  125.         mon.setCursorPos(4, 5);
  126.         mon.write("Zero drives are better than -1 drives I guess")
  127.     else
  128.         for i=1, #newDrives do
  129.             data.totalBytes = data.totalBytes + newDrives[i].totalBytes
  130.             data.usedBytes = data.usedBytes + newDrives[i].usedBytes
  131.             data.totalCells = data.totalCells + #newDrives[i].cells
  132.  
  133.             bars.set(""..i,"cur", newDrives[i].usedBytes)
  134.             bars.set(""..i,"max", newDrives[i].totalBytes)
  135.         end
  136.     end
  137.     bars.screen()
  138.  
  139.     clear(3,monX - 3,monY - 5,monY - 2)
  140.     print("Drives: ".. data.drives)
  141.     mon.setCursorPos(4,monY-6)
  142.     mon.write("Drives: ".. data.drives)
  143.     mon.setCursorPos(4,monY-5)
  144.     mon.write("Full: ".. roundToDecimal(getUsage(), 2) .."%")
  145.     mon.setCursorPos(4,monY-4)
  146.     mon.write("Cells: ".. data.totalCells)
  147.     mon.setCursorPos(4,monY-3)
  148.     mon.write("Bytes(Total|Used):")
  149.     mon.setCursorPos(23,monY-3)
  150.     mon.write(comma_value(data.totalBytes) .." | ".. comma_value(data.usedBytes))
  151.  
  152.     if data.drives ~= #newDrives then
  153.         clear(3,monX - 3,4,monY - 12)
  154.         mon.setCursorPos(4, 5);
  155.         mon.write("Found new Drive... Rebooting")
  156.         shell.run("reboot")
  157.     end
  158. end
  159.  
  160. prepare()
  161.  
  162. while true do
  163.     updateStats()
  164.     sleep(0.5)
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement