Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- bar_ui.lua (run on your UI-side computer)
- -- ========================================================
- -- Peripheral wrapping
- -- ========================================================
- rednet.open("left")
- local monitorBar = assert(peripheral.wrap("back"), "Bar monitor not found")
- -- ========================================================
- -- Formatting
- -- ========================================================
- local function formatK(val)
- if not val then return "-" end
- if val >= 1000 then return string.format("%dk", math.floor(val/1000)) end
- return tostring(val)
- end
- -- ========================================================
- -- Monitor Bar UI (with 3-color bar and min/max markers)
- -- ========================================================
- local function monitorBarUI(data)
- monitorBar.setBackgroundColor(colors.black)
- monitorBar.clear()
- monitorBar.setTextScale(0.5)
- local width, _ = monitorBar.getSize()
- local margin = 2
- local barWidth = width - margin * 2
- local curTotal = data.totalEnergy
- local maxTotal = data.totalCapacity
- local fillColumns = (data.accPct / 100) * barWidth
- -- compute marker positions
- local minPct = maxTotal > 0 and (data.minEnergy / maxTotal) or 0
- local markerMinPos = math.floor(minPct * barWidth + 0.5)
- local maxPct = maxTotal > 0 and (data.maxEnergy / maxTotal) or 0
- local markerMaxPos = math.floor(maxPct * barWidth + 0.5)
- -- decide bar color by current energy
- local barColor
- if curTotal < 36000000 then
- barColor = colors.red
- elseif curTotal < data.minEnergy then
- barColor = colors.orange
- else
- barColor = colors.lime
- end
- -- header: “XX.X% 123k/900k”
- local pctStr = string.format("%.1f%%", data.accPct)
- local curMaxStr = formatK(curTotal) .. "/" .. formatK(maxTotal)
- local header = pctStr .. string.rep(" ", width - #pctStr - #curMaxStr) .. curMaxStr
- local fillChar = string.char(149)
- local barLines = {}
- -- build each line of the bar
- for _ = 1, 5 do
- local line = ""
- for j = 1, barWidth do
- if j == markerMinPos or j == markerMaxPos then
- line = line .. "|"
- else
- local floor = math.floor(fillColumns)
- if j <= floor or (j == floor + 1 and fillColumns % 1 > 0) then
- line = line .. fillChar
- else
- line = line .. " "
- end
- end
- end
- table.insert(barLines, string.rep(" ", margin) .. line .. string.rep(" ", margin))
- end
- -- draw header
- monitorBar.setCursorPos(1, 2)
- monitorBar.setTextColor(colors.white)
- monitorBar.write(header)
- -- draw the bar
- for i, line in ipairs(barLines) do
- monitorBar.setCursorPos(1, 2 + i)
- for j = 1, #line do
- local c = line:sub(j, j)
- local idx = j - margin
- local floor = math.floor(fillColumns)
- if c == "|" then
- -- draw marker
- monitorBar.setTextColor(colors.black)
- monitorBar.setBackgroundColor(idx <= fillColumns and barColor or colors.gray)
- monitorBar.write("|")
- elseif idx >= 1 and idx <= floor then
- -- filled
- monitorBar.setTextColor(barColor)
- monitorBar.setBackgroundColor(barColor)
- monitorBar.write(fillChar)
- elseif idx == floor + 1 and fillColumns % 1 > 0 then
- -- partial fill
- monitorBar.setTextColor(barColor)
- monitorBar.setBackgroundColor(colors.gray)
- monitorBar.write(fillChar)
- elseif idx > 0 and idx <= barWidth then
- -- empty
- monitorBar.setTextColor(colors.gray)
- monitorBar.setBackgroundColor(colors.gray)
- monitorBar.write(" ")
- else
- -- margin
- monitorBar.setTextColor(colors.white)
- monitorBar.setBackgroundColor(colors.black)
- monitorBar.write(" ")
- end
- end
- end
- -- bottom labels: min and max as percentages
- local minLabel = string.format("%.1f%%", (data.minEnergy / maxTotal) * 100)
- local maxLabel = string.format("%.1f%%", (data.maxEnergy / maxTotal) * 100)
- local leftPadding = margin + markerMinPos - math.floor(#minLabel / 2) - 1
- local bottomLine = string.rep(" ", math.max(0, leftPadding)) .. minLabel
- local gap = (margin + markerMaxPos - math.floor(#maxLabel / 2) - 1) - #bottomLine
- if gap > 0 then
- bottomLine = bottomLine .. string.rep(" ", gap)
- end
- bottomLine = bottomLine .. maxLabel
- monitorBar.setCursorPos(1, 2 + #barLines + 1)
- monitorBar.setTextColor(colors.white)
- monitorBar.setBackgroundColor(colors.black)
- monitorBar.write(bottomLine)
- end
- -- ========================================================
- -- Receive & render loop
- -- ========================================================
- while true do
- local _, data = rednet.receive("generator_status")
- monitorBarUI(data)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement