Add storage capacity bar to monitor dashboard for better inventory management
This commit is contained in:
@@ -161,6 +161,57 @@ local function drawDashboard()
|
|||||||
monFill(3, colors.lightBlue)
|
monFill(3, colors.lightBlue)
|
||||||
monCenter(3, string.rep("\140", math.min(w - 4, 60)), colors.cyan, colors.lightBlue)
|
monCenter(3, string.rep("\140", math.min(w - 4, 60)), colors.cyan, colors.lightBlue)
|
||||||
|
|
||||||
|
-- ===== Storage capacity bar =====
|
||||||
|
-- Count total slots and used slots across all chests (in parallel)
|
||||||
|
local totalSlots = 0
|
||||||
|
local usedSlots = 0
|
||||||
|
local capTasks = {}
|
||||||
|
local capResults = {}
|
||||||
|
for i, chestName in ipairs(chests) do
|
||||||
|
capResults[i] = { total = 0, used = 0 }
|
||||||
|
capTasks[i] = function()
|
||||||
|
local inv = peripheral.wrap(chestName)
|
||||||
|
if inv then
|
||||||
|
local size = inv.size()
|
||||||
|
capResults[i].total = size
|
||||||
|
local contents = inv.list()
|
||||||
|
for _ in pairs(contents) do
|
||||||
|
capResults[i].used = capResults[i].used + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if #capTasks > 0 then
|
||||||
|
parallel.waitForAll(table.unpack(capTasks))
|
||||||
|
end
|
||||||
|
for _, r in ipairs(capResults) do
|
||||||
|
totalSlots = totalSlots + r.total
|
||||||
|
usedSlots = usedSlots + r.used
|
||||||
|
end
|
||||||
|
local freeSlots = totalSlots - usedSlots
|
||||||
|
local usedRatio = totalSlots > 0 and (usedSlots / totalSlots) or 0
|
||||||
|
|
||||||
|
local capY = 4
|
||||||
|
monFill(capY, colors.black)
|
||||||
|
local capLabel = string.format(" Storage: %d/%d slots (%d free)", usedSlots, totalSlots, freeSlots)
|
||||||
|
monWrite(2, capY, capLabel, colors.lightGray, colors.black)
|
||||||
|
|
||||||
|
-- Draw capacity bar
|
||||||
|
local barStart = #capLabel + 4
|
||||||
|
local barWidth = w - barStart - 2
|
||||||
|
if barWidth > 4 then
|
||||||
|
local barColor = colors.lime
|
||||||
|
if usedRatio > 0.9 then barColor = colors.red
|
||||||
|
elseif usedRatio > 0.7 then barColor = colors.orange
|
||||||
|
elseif usedRatio > 0.5 then barColor = colors.yellow
|
||||||
|
end
|
||||||
|
monBar(barStart, capY, barWidth, usedRatio, barColor, colors.gray)
|
||||||
|
-- Show percentage on the bar
|
||||||
|
local pctStr = string.format(" %d%% ", math.floor(usedRatio * 100))
|
||||||
|
local pctX = barStart + math.floor(barWidth / 2) - math.floor(#pctStr / 2)
|
||||||
|
monWrite(pctX, capY, pctStr, colors.white, barColor)
|
||||||
|
end
|
||||||
|
|
||||||
-- ===== Item catalogue =====
|
-- ===== Item catalogue =====
|
||||||
local catalogue = buildCatalogue()
|
local catalogue = buildCatalogue()
|
||||||
|
|
||||||
@@ -176,7 +227,7 @@ local function drawDashboard()
|
|||||||
table.sort(itemList, function(a, b) return a.total > b.total end)
|
table.sort(itemList, function(a, b) return a.total > b.total end)
|
||||||
|
|
||||||
-- Header row
|
-- Header row
|
||||||
local row = 5
|
local row = 6
|
||||||
monFill(row, colors.gray)
|
monFill(row, colors.gray)
|
||||||
monWrite(2, row, "#", colors.lightGray, colors.gray)
|
monWrite(2, row, "#", colors.lightGray, colors.gray)
|
||||||
monWrite(5, row, "Item", colors.lightGray, colors.gray)
|
monWrite(5, row, "Item", colors.lightGray, colors.gray)
|
||||||
|
|||||||
Reference in New Issue
Block a user