Refactor buildCatalogue and monitor drawing functions for improved performance and reduced flickering
This commit is contained in:
@@ -38,15 +38,33 @@ local function scanInventory(deviceName)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Build a full catalogue: itemName -> { {chest=name, total=N}, ... }
|
-- Build a full catalogue: itemName -> { {chest=name, total=N}, ... }
|
||||||
|
-- Scans all chests in parallel for speed
|
||||||
local function buildCatalogue()
|
local function buildCatalogue()
|
||||||
local catalogue = {} -- itemName -> list of {chest, total}
|
local chests = getChests()
|
||||||
for _, chest in ipairs(getChests()) do
|
local results = {} -- index -> { chest=name, contents={} }
|
||||||
local contents = scanInventory(chest)
|
|
||||||
for itemName, info in pairs(contents) do
|
-- Build parallel scan tasks
|
||||||
|
local tasks = {}
|
||||||
|
for i, chest in ipairs(chests) do
|
||||||
|
results[i] = { chest = chest, contents = {} }
|
||||||
|
tasks[i] = function()
|
||||||
|
results[i].contents = scanInventory(chest)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Run all scans simultaneously
|
||||||
|
if #tasks > 0 then
|
||||||
|
parallel.waitForAll(table.unpack(tasks))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Merge results into catalogue
|
||||||
|
local catalogue = {}
|
||||||
|
for _, result in ipairs(results) do
|
||||||
|
for itemName, info in pairs(result.contents) do
|
||||||
if not catalogue[itemName] then
|
if not catalogue[itemName] then
|
||||||
catalogue[itemName] = {}
|
catalogue[itemName] = {}
|
||||||
end
|
end
|
||||||
table.insert(catalogue[itemName], { chest = chest, total = info.total })
|
table.insert(catalogue[itemName], { chest = result.chest, total = info.total })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return catalogue
|
return catalogue
|
||||||
@@ -57,6 +75,7 @@ end
|
|||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
local mon = nil
|
local mon = nil
|
||||||
|
local buf = nil -- offscreen buffer to prevent flickering
|
||||||
|
|
||||||
local function setupMonitor()
|
local function setupMonitor()
|
||||||
mon = peripheral.wrap(MONITOR_SIDE)
|
mon = peripheral.wrap(MONITOR_SIDE)
|
||||||
@@ -66,45 +85,52 @@ local function setupMonitor()
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Helpers
|
-- Drawing target (buf or mon, set in drawDashboard)
|
||||||
|
local draw = nil
|
||||||
|
|
||||||
|
-- Helpers (draw to buffer)
|
||||||
local function monWrite(x, y, text, fg, bg)
|
local function monWrite(x, y, text, fg, bg)
|
||||||
mon.setCursorPos(x, y)
|
draw.setCursorPos(x, y)
|
||||||
if fg then mon.setTextColor(fg) end
|
if fg then draw.setTextColor(fg) end
|
||||||
if bg then mon.setBackgroundColor(bg) end
|
if bg then draw.setBackgroundColor(bg) end
|
||||||
mon.write(text)
|
draw.write(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function monFill(y, color)
|
local function monFill(y, color)
|
||||||
local w, _ = mon.getSize()
|
local w, _ = draw.getSize()
|
||||||
mon.setCursorPos(1, y)
|
draw.setCursorPos(1, y)
|
||||||
mon.setBackgroundColor(color)
|
draw.setBackgroundColor(color)
|
||||||
mon.write(string.rep(" ", w))
|
draw.write(string.rep(" ", w))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function monCenter(y, text, fg, bg)
|
local function monCenter(y, text, fg, bg)
|
||||||
local w, _ = mon.getSize()
|
local w, _ = draw.getSize()
|
||||||
local x = math.floor((w - #text) / 2) + 1
|
local x = math.floor((w - #text) / 2) + 1
|
||||||
monWrite(x, y, text, fg, bg)
|
monWrite(x, y, text, fg, bg)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function monBar(x, y, width, ratio, barColor, bgColor)
|
local function monBar(x, y, width, ratio, barColor, bgColor)
|
||||||
local filled = math.floor(ratio * width)
|
local filled = math.floor(ratio * width)
|
||||||
mon.setCursorPos(x, y)
|
draw.setCursorPos(x, y)
|
||||||
mon.setBackgroundColor(barColor)
|
draw.setBackgroundColor(barColor)
|
||||||
mon.write(string.rep(" ", filled))
|
draw.write(string.rep(" ", filled))
|
||||||
mon.setBackgroundColor(bgColor)
|
draw.setBackgroundColor(bgColor)
|
||||||
mon.write(string.rep(" ", width - filled))
|
draw.write(string.rep(" ", width - filled))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Draw the full dashboard
|
-- Draw the full dashboard (double-buffered)
|
||||||
local function drawDashboard()
|
local function drawDashboard()
|
||||||
if not mon then return end
|
if not mon then return end
|
||||||
|
|
||||||
local w, h = mon.getSize()
|
local w, h = mon.getSize()
|
||||||
|
|
||||||
-- Clear
|
-- Create offscreen buffer
|
||||||
mon.setBackgroundColor(colors.black)
|
buf = window.create(mon, 1, 1, w, h, false)
|
||||||
mon.clear()
|
draw = buf
|
||||||
|
|
||||||
|
-- Clear buffer
|
||||||
|
draw.setBackgroundColor(colors.black)
|
||||||
|
draw.clear()
|
||||||
|
|
||||||
-- ===== Title bar =====
|
-- ===== Title bar =====
|
||||||
monFill(1, colors.blue)
|
monFill(1, colors.blue)
|
||||||
@@ -221,6 +247,10 @@ local function drawDashboard()
|
|||||||
-- Bottom accent
|
-- Bottom accent
|
||||||
monFill(h, colors.blue)
|
monFill(h, colors.blue)
|
||||||
monCenter(h, " \4 Auto-Sort Active \4 ", colors.lightBlue, colors.blue)
|
monCenter(h, " \4 Auto-Sort Active \4 ", colors.lightBlue, colors.blue)
|
||||||
|
|
||||||
|
-- Flush buffer to monitor in one go (no flicker)
|
||||||
|
buf.setVisible(true)
|
||||||
|
buf.setVisible(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user