Advertisement
zamoth

Untitled

Jul 5th, 2025 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. -- bar_ui.lua  (run on your UI-side computer)
  2.  
  3. -- ========================================================
  4. -- Peripheral wrapping
  5. -- ========================================================
  6. rednet.open("left")  
  7. local monitorBar = assert(peripheral.wrap("back"), "Bar monitor not found")
  8.  
  9. -- ========================================================
  10. -- Formatting
  11. -- ========================================================
  12. local function formatK(val)
  13.   if not val then return "-" end
  14.   if val >= 1000 then return string.format("%dk", math.floor(val/1000)) end
  15.   return tostring(val)
  16. end
  17.  
  18. -- ========================================================
  19. -- Monitor Bar UI (with 3-color bar and min/max markers)
  20. -- ========================================================
  21. local function monitorBarUI(data)
  22.   monitorBar.setBackgroundColor(colors.black)
  23.   monitorBar.clear()
  24.   monitorBar.setTextScale(0.5)
  25.  
  26.   local width, _    = monitorBar.getSize()
  27.   local margin      = 2
  28.   local barWidth    = width - margin * 2
  29.   local curTotal    = data.totalEnergy
  30.   local maxTotal    = data.totalCapacity
  31.   local fillColumns = (data.accPct / 100) * barWidth
  32.  
  33.   -- compute marker positions
  34.   local minPct       = maxTotal > 0 and (data.minEnergy / maxTotal) or 0
  35.   local markerMinPos = math.floor(minPct * barWidth + 0.5)
  36.   local maxPct       = maxTotal > 0 and (data.maxEnergy / maxTotal) or 0
  37.   local markerMaxPos = math.floor(maxPct * barWidth + 0.5)
  38.  
  39.   -- decide bar color by current energy
  40.   local barColor
  41.   if curTotal < 36000000 then
  42.     barColor = colors.red
  43.   elseif curTotal < data.minEnergy then
  44.     barColor = colors.orange
  45.   else
  46.     barColor = colors.lime
  47.   end
  48.  
  49.   -- header: “XX.X%    123k/900k”
  50.   local pctStr    = string.format("%.1f%%", data.accPct)
  51.   local curMaxStr = formatK(curTotal) .. "/" .. formatK(maxTotal)
  52.   local header    = pctStr .. string.rep(" ", width - #pctStr - #curMaxStr) .. curMaxStr
  53.  
  54.   local fillChar = string.char(149)
  55.   local barLines = {}
  56.  
  57.   -- build each line of the bar
  58.   for _ = 1, 5 do
  59.     local line = ""
  60.     for j = 1, barWidth do
  61.       if j == markerMinPos or j == markerMaxPos then
  62.         line = line .. "|"
  63.       else
  64.         local floor = math.floor(fillColumns)
  65.         if j <= floor or (j == floor + 1 and fillColumns % 1 > 0) then
  66.           line = line .. fillChar
  67.         else
  68.           line = line .. " "
  69.         end
  70.       end
  71.     end
  72.     table.insert(barLines, string.rep(" ", margin) .. line .. string.rep(" ", margin))
  73.   end
  74.  
  75.   -- draw header
  76.   monitorBar.setCursorPos(1, 2)
  77.   monitorBar.setTextColor(colors.white)
  78.   monitorBar.write(header)
  79.  
  80.   -- draw the bar
  81.   for i, line in ipairs(barLines) do
  82.     monitorBar.setCursorPos(1, 2 + i)
  83.     for j = 1, #line do
  84.       local c     = line:sub(j, j)
  85.       local idx   = j - margin
  86.       local floor = math.floor(fillColumns)
  87.       if c == "|" then
  88.         -- draw marker
  89.         monitorBar.setTextColor(colors.black)
  90.         monitorBar.setBackgroundColor(idx <= fillColumns and barColor or colors.gray)
  91.         monitorBar.write("|")
  92.       elseif idx >= 1 and idx <= floor then
  93.         -- filled
  94.         monitorBar.setTextColor(barColor)
  95.         monitorBar.setBackgroundColor(barColor)
  96.         monitorBar.write(fillChar)
  97.       elseif idx == floor + 1 and fillColumns % 1 > 0 then
  98.         -- partial fill
  99.         monitorBar.setTextColor(barColor)
  100.         monitorBar.setBackgroundColor(colors.gray)
  101.         monitorBar.write(fillChar)
  102.       elseif idx > 0 and idx <= barWidth then
  103.         -- empty
  104.         monitorBar.setTextColor(colors.gray)
  105.         monitorBar.setBackgroundColor(colors.gray)
  106.         monitorBar.write(" ")
  107.       else
  108.         -- margin
  109.         monitorBar.setTextColor(colors.white)
  110.         monitorBar.setBackgroundColor(colors.black)
  111.         monitorBar.write(" ")
  112.       end
  113.     end
  114.   end
  115.  
  116.   -- bottom labels: min and max as percentages
  117.   local minLabel     = string.format("%.1f%%", (data.minEnergy / maxTotal) * 100)
  118.   local maxLabel     = string.format("%.1f%%", (data.maxEnergy / maxTotal) * 100)
  119.   local leftPadding  = margin + markerMinPos - math.floor(#minLabel / 2) - 1
  120.   local bottomLine   = string.rep(" ", math.max(0, leftPadding)) .. minLabel
  121.   local gap          = (margin + markerMaxPos - math.floor(#maxLabel / 2) - 1) - #bottomLine
  122.   if gap > 0 then
  123.     bottomLine = bottomLine .. string.rep(" ", gap)
  124.   end
  125.   bottomLine = bottomLine .. maxLabel
  126.  
  127.   monitorBar.setCursorPos(1, 2 + #barLines + 1)
  128.   monitorBar.setTextColor(colors.white)
  129.   monitorBar.setBackgroundColor(colors.black)
  130.   monitorBar.write(bottomLine)
  131. end
  132.  
  133. -- ========================================================
  134. -- Receive & render loop
  135. -- ========================================================
  136. while true do
  137.   local _, data = rednet.receive("generator_status")
  138.   monitorBarUI(data)
  139. end
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement