[listframes] refactor to ListFrames class

This commit is contained in:
Ben 2024-06-03 23:36:47 +02:00
parent 3d3a0956b2
commit 20bdf46ea4
Signed by: ben
GPG key ID: 0F54A7ED232D3319
2 changed files with 116 additions and 131 deletions

View file

@ -1,28 +1,31 @@
local backingList = {} ListFrames = {
local subFrames = {} frame = term.current(),
local length = 0 drawEntry = nil,
local frame = term.current() backingList = {},
local drawEntry = nil subFrames = {},
local estimatedTotalHeight = 0 length = 0,
estimatedTotalHeight = 0,
-- pagination start line (not number of entries!)
paginationPos = 1,
}
-- pagination start line (not number of entries!) function ListFrames:new(o)
local paginationPos = 1 o = o or {}
setmetatable(o, self)
self.__index = self
local function setDrawEntryFunc(drawLineFunc) return o
drawEntry = drawLineFunc
end end
local function repositionEntriesAfter(idx) function ListFrames:repositionEntriesAfter(idx)
local tw, th = frame.getSize() local tw, th = self.frame.getSize()
local entryFrame = subFrames[idx] local entryFrame = self.subFrames[idx]
assert(entryFrame ~= nil, "provided entry at idx=" .. idx .. " has no frame") assert(entryFrame ~= nil, "provided entry at idx=" .. idx .. " has no frame")
local xPos, yPos = entryFrame.getPosition() local xPos, yPos = entryFrame.getPosition()
local xSize, ySize = entryFrame.getSize() local xSize, ySize = entryFrame.getSize()
local yStartNext = yPos + ySize local yStartNext = yPos + ySize
while yStartNext <= th do while yStartNext <= th do
idx = idx + 1 idx = idx + 1
entryFrame = subFrames[idx] entryFrame = self.subFrames[idx]
if entryFrame == nil then if entryFrame == nil then
break break
end end
@ -31,142 +34,128 @@ local function repositionEntriesAfter(idx)
if yPos == yStartNext then if yPos == yStartNext then
break break
else else
entryFrame.reposition(1, yStartNext, tw, ySize, frame) entryFrame.reposition(1, yStartNext, tw, ySize, self.frame)
entryFrame.setVisible(true) entryFrame.setVisible(true)
end end
yStartNext = yPos + ySize yStartNext = yPos + ySize
end end
idx = idx + 1 idx = idx + 1
entryFrame = subFrames[idx] entryFrame = self.subFrames[idx]
while entryFrame ~= nil and entryFrame.isVisible() do while entryFrame ~= nil and entryFrame.isVisible() do
entryFrame.setVisible(false) entryFrame.setVisible(false)
idx = idx + 1 idx = idx + 1
entryFrame = subFrames[idx] entryFrame = self.subFrames[idx]
end end
end end
local function redrawEntry(idx) function ListFrames:redrawEntry(idx)
local entryFrame = subFrames[idx] local entryFrame = self.subFrames[idx]
if entryFrame ~= nil then if entryFrame ~= nil then
entryFrame.setCursorPos(1,1) entryFrame.setCursorPos(1,1)
if drawEntry then if self.drawEntry then
drawEntry(entryFrame, backingList[idx]) self.drawEntry(entryFrame, self.backingList[idx])
else else
entryFrame.setBackgroundColor(colors.red) entryFrame.setBackgroundColor(colors.red)
entryFrame.clearLine() entryFrame.clearLine()
entryFrame.setBackgroundColor(colors.black) entryFrame.setBackgroundColor(colors.black)
end end
local tw, th = frame.getSize() local tw, th = self.frame.getSize()
local xPos, yPos = entryFrame.getPosition() local xPos, yPos = entryFrame.getPosition()
local xSize, ySize = entryFrame.getSize() local xSize, ySize = entryFrame.getSize()
local yEnd = yPos+ySize-1 local yEnd = yPos+ySize-1
entryFrame.setVisible(yEnd > 0 and yPos <= th) entryFrame.setVisible(yEnd > 0 and yPos <= th)
yEnd = yEnd+paginationPos-1 yEnd = yEnd+self.paginationPos-1
if yEnd > estimatedTotalHeight then if yEnd > self.estimatedTotalHeight then
estimatedTotalHeight = yEnd self.estimatedTotalHeight = yEnd
end end
end end
end end
local function repositionOrCreateEntryFrame(idx) function ListFrames:repositionOrCreateEntryFrame(idx)
local previousFrame = subFrames[idx-1] local previousFrame = self.subFrames[idx-1]
local yPos = 2-paginationPos local yPos = 2-self.paginationPos
if previousFrame ~= nil then if previousFrame ~= nil then
local xPos local xPos
xPos, yPos = previousFrame.getPosition() xPos, yPos = previousFrame.getPosition()
local xSize, ySize = previousFrame.getSize() local xSize, ySize = previousFrame.getSize()
yPos = yPos + ySize yPos = yPos + ySize
end end
local tw, th = frame.getSize() local tw, th = self.frame.getSize()
local entryFrame = subFrames[idx] local entryFrame = self.subFrames[idx]
if entryFrame == nil then if entryFrame == nil then
subFrames[idx] = window.create(frame, 1, yPos, tw, 1, false) self.subFrames[idx] = window.create(self.frame, 1, yPos, tw, 1, false)
else else
entryFrame.setVisible(false) entryFrame.setVisible(false)
local xSize, ySize = entryFrame.getSize() local xSize, ySize = entryFrame.getSize()
entryFrame.reposition(1, yPos, tw, ySize, frame) entryFrame.reposition(1, yPos, tw, ySize, self.frame)
end end
end end
local function updateItemAt(idx, item) function ListFrames:updateItemAt(idx, item)
backingList[idx] = item self.backingList[idx] = item
repositionOrCreateEntryFrame(idx) self:repositionOrCreateEntryFrame(idx)
if idx > length then if idx > self.length then
length = idx self.length = idx
end end
redrawEntry(idx) self:redrawEntry(idx)
repositionEntriesAfter(idx) self:repositionEntriesAfter(idx)
end end
local function clearFrom(idx) function ListFrames:clearFrom(idx)
estimatedTotalHeight = idx-1 self.estimatedTotalHeight = idx-1
local tw, th = frame.getSize() local tw, th = self.frame.getSize()
for j = length, idx, -1 do for j = self.length, idx, -1 do
backingList[j] = nil self.backingList[j] = nil
if subFrames[j] ~= nil then if self.subFrames[j] ~= nil then
subFrames[j].setVisible(false) self.subFrames[j].setVisible(false)
end end
end end
length = idx-1 self.length = idx-1
for y = idx-paginationPos+1, th, 1 do for y = idx-self.paginationPos+1, th, 1 do
frame.setCursorPos(1, y) self.frame.setCursorPos(1, y)
frame.clearLine() self.frame.clearLine()
end end
end end
local function redraw() function ListFrames:redraw()
if drawEntry then if self.drawEntry then
frame.clear() self.frame.clear()
local tw, th = frame.getSize() local tw, th = self.frame.getSize()
local yPos = 2-paginationPos local yPos = 2-self.paginationPos
for idx = 1, length, 1 do for idx = 1, self.length, 1 do
assert(backingList[idx] ~= nil, "nil element at idx="..idx) assert(self.backingList[idx] ~= nil, "nil element at idx="..idx)
if yPos <= th then if yPos <= th then
repositionOrCreateEntryFrame(idx) self:repositionOrCreateEntryFrame(idx)
redrawEntry(idx) self:redrawEntry(idx)
local xSize, ySize = subFrames[idx].getSize() local xSize, ySize = self.subFrames[idx].getSize()
yPos = yPos + ySize yPos = yPos + ySize
elseif subFrames[idx] ~= nil then elseif self.subFrames[idx] ~= nil then
subFrames[idx].setVisible(false) self.subFrames[idx].setVisible(false)
end end
end end
else else
frame.setBackgroundColor(colors.red) self.frame.setBackgroundColor(colors.red)
frame.clear() self.frame.clear()
frame.setBackgroundColor(colors.black) self.frame.setBackgroundColor(colors.black)
end end
end end
local function setTerm(termlike) function ListFrames:updatePage(scrollPos)
frame = termlike if scrollPos ~= self.paginationPos then
-- reposition visible subFrames self.paginationPos = scrollPos
redraw() self:redraw()
end
local function updatePage(scrollPos)
if scrollPos ~= paginationPos then
paginationPos = scrollPos
redraw()
end end
end end
local function estimatedHeight() function ListFrames:estimatedHeight()
local lastEntryFrame = subFrames[length] local lastEntryFrame = self.subFrames[self.length]
if lastEntryFrame ~= nil and lastEntryFrame.isVisible() then if lastEntryFrame ~= nil and lastEntryFrame.isVisible() then
local xPos, yPos = lastEntryFrame.getPosition() local xPos, yPos = lastEntryFrame.getPosition()
local xSize, ySize = lastEntryFrame.getSize() local xSize, ySize = lastEntryFrame.getSize()
estimatedTotalHeight = yPos+ySize-1+paginationPos-1 self.estimatedTotalHeight = yPos+ySize-1+self.paginationPos-1
end end
return estimatedTotalHeight return self.estimatedTotalHeight
end end
return { return ListFrames
setTerm=setTerm,
setDrawEntryFunc=setDrawEntryFunc,
updateItemAt=updateItemAt,
clearFrom=clearFrom,
redraw=redraw,
updatePage=updatePage,
estimatedHeight=estimatedHeight,
}

View file

@ -1,46 +1,8 @@
local config = require "config" local config = require "config"
local listframes = require "listframes"
local winhlp = require "winhlp" local winhlp = require "winhlp"
local colony = peripheral.wrap(config.colony_interface_side) local colony = peripheral.wrap(config.colony_interface_side)
local frame = term.current()
local function setTerm(termlike)
frame = termlike
listframes.setTerm(frame)
end
local function fetch(wait_for)
local currentIdx = 1
if colony.isInColony() then
for i, wo in pairs(colony.getWorkOrders()) do
local resources = colony.getWorkOrderResources(wo.id)
local resStatus = "?/?/0"
if resources ~= nil then
local unavailable_count = 0
local deliver_count = 0
for j, res in pairs(resources) do
if not res.available then
unavailable_count = unavailable_count + 1
end
if res.delivering then
deliver_count = deliver_count + 1
end
end
resStatus = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end
listframes.updateItemAt(currentIdx, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, status=resStatus, claimed=wo.isClaimed})
currentIdx = currentIdx + 1
wait_for(config.step_sleep_time)
if not colony.isInColony() then
break
end
end
end
listframes.clearFrom(currentIdx)
end
local function drawEntry(win, obj) local function drawEntry(win, obj)
winhlp.setHeight(win, 2) winhlp.setHeight(win, 2)
win.setBackgroundColor(colors.black) win.setBackgroundColor(colors.black)
@ -82,14 +44,48 @@ local function drawEntry(win, obj)
win.write(obj.type) win.write(obj.type)
end end
end end
listframes.setDrawEntryFunc(drawEntry) local listframes = (require "listframes"):new{drawEntry=drawEntry}
local function setTerm(termlike)
listframes.frame = termlike
end
local function fetch(wait_for)
local currentIdx = 1
if colony.isInColony() then
for i, wo in pairs(colony.getWorkOrders()) do
local resources = colony.getWorkOrderResources(wo.id)
local resStatus = "?/?/0"
if resources ~= nil then
local unavailable_count = 0
local deliver_count = 0
for j, res in pairs(resources) do
if not res.available then
unavailable_count = unavailable_count + 1
end
if res.delivering then
deliver_count = deliver_count + 1
end
end
resStatus = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end
listframes:updateItemAt(currentIdx, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, status=resStatus, claimed=wo.isClaimed})
currentIdx = currentIdx + 1
wait_for(config.step_sleep_time)
if not colony.isInColony() then
break
end
end
end
listframes:clearFrom(currentIdx)
end
local function displayTab(self) local function displayTab(self)
listframes.updatePage(self.scrollPos) listframes:updatePage(self.scrollPos)
end end
local function estimatedHeight() local function estimatedHeight()
return listframes.estimatedHeight() return listframes:estimatedHeight()
end end