Add listframes for multiline lists, refactor order list

This commit is contained in:
Ben 2024-06-02 16:41:54 +02:00
parent 35a5aeabb8
commit 7616a5bed3
Signed by: ben
GPG key ID: 0F54A7ED232D3319
5 changed files with 240 additions and 80 deletions

152
listframes.lua Normal file
View file

@ -0,0 +1,152 @@
local backingList = {}
local subFrames = {}
local length = 0
local frame = term.current()
local drawEntry = nil
-- pagination offset in number of lines (not number of entries!)
local paginationOffset = 1
local function setDrawEntryFunc(drawLineFunc)
drawEntry = drawLineFunc
end
local function repositionEntriesAfter(idx)
local tw, th = frame.getSize()
local entryFrame = subFrames[idx]
assert(entryFrame ~= nil, "provided entry at idx=" .. idx .. " has no frame")
local xPos, yPos = entryFrame.getPosition()
local xSize, ySize = entryFrame.getSize()
local yStartNext = yPos + ySize
while yStartNext <= th do
idx = idx + 1
entryFrame = subFrames[idx]
if entryFrame == nil then
break
end
xPos, yPos = entryFrame.getPosition()
xSize, ySize = entryFrame.getSize()
if yPos == yStartNext then
break
else
entryFrame.reposition(1, yStartNext, tw, ySize, frame)
entryFrame.setVisible(true)
end
yStartNext = yPos + ySize
end
idx = idx + 1
entryFrame = subFrames[idx]
while entryFrame ~= nil and entryFrame.isVisible() do
entryFrame.setVisible(false)
idx = idx + 1
entryFrame = subFrames[idx]
end
end
local function redrawEntry(idx)
local entryFrame = subFrames[idx]
if entryFrame ~= nil then
entryFrame.setCursorPos(1,1)
if drawEntry then
drawEntry(entryFrame, backingList[idx])
else
entryFrame.setBackgroundColor(colors.red)
entryFrame.clearLine()
entryFrame.setBackgroundColor(colors.black)
end
local tw, th = frame.getSize()
local xPos, yPos = entryFrame.getPosition()
local xSize, ySize = entryFrame.getSize()
entryFrame.setVisible((yPos+ySize-1) >= paginationOffset and yPos <= th)
end
end
local function repositionOrCreateEntryFrame(idx)
local previousFrame = subFrames[idx-1]
local yPos = 1
if previousFrame ~= nil then
local xPos
xPos, yPos = previousFrame.getPosition()
yPos = yPos + 1
end
local tw, th = frame.getSize()
local entryFrame = subFrames[idx]
if entryFrame == nil then
subFrames[idx] = window.create(frame, 1, yPos, tw, 1, false)
else
entryFrame.setVisible(false)
local xSize, ySize = entryFrame.getSize()
entryFrame.reposition(1, yPos, tw, ySize, frame)
end
end
local function updateItemAt(idx, item)
backingList[idx] = item
repositionOrCreateEntryFrame(idx)
if idx > length then
length = idx
end
redrawEntry(idx)
repositionEntriesAfter(idx)
end
local function clearFrom(idx)
local tw, th = frame.getSize()
for j = length, idx, -1 do
backingList[j] = nil
if subFrames[j] ~= nil then
subFrames[j].setVisible(false)
end
end
length = idx-1
for y = idx-paginationOffset+1, th, 1 do
frame.setCursorPos(1, y)
frame.clearLine()
end
end
local function redraw()
if drawEntry then
local tw, th = frame.getSize()
local yPos = 1
for idx = 1, length, 1 do
assert(backingList[idx] ~= nil, "nil element at idx="..idx)
if yPos < paginationOffset+th then
repositionOrCreateEntryFrame(idx)
redrawEntry(idx)
local xSize, ySize = subFrames[idx].getSize()
yPos = yPos + ySize
elseif subFrames[idx] ~= nil then
subFrames[idx].setVisible(false)
end
end
else
frame.setBackgroundColor(colors.red)
frame.clear()
frame.setBackgroundColor(colors.black)
end
end
local function setTerm(termlike)
frame = termlike
-- reposition visible subFrames
redraw()
end
local function updatePage(newOffset)
if newOffset ~= paginationOffset then
paginationOffset = newOffset
redraw()
end
end
return {
setTerm=setTerm,
setDrawEntryFunc=setDrawEntryFunc,
updateItemAt=updateItemAt,
clearFrom=clearFrom,
redraw=redraw,
updatePage=updatePage,
}

View file

@ -2,22 +2,22 @@ local backingList = {}
local length = 0
local paginationOffset = 1
local frame = term.current()
local drawLine = nil
local drawEntry = nil
local function setTerm(termlike)
frame = termlike
end
local function setDrawLineFunc(drawLineFunc)
drawLine = drawLineFunc
local function setDrawEntryFunc(drawLineFunc)
drawEntry = drawLineFunc
end
local function redrawLine(idx)
local function redrawEntry(idx)
local tw, th = frame.getSize()
if idx >= paginationOffset and idx-paginationOffset <= th then
frame.setCursorPos(1, idx-paginationOffset+1)
if drawLine then
drawLine(frame, backingList[idx])
if drawEntry then
drawEntry(frame, backingList[idx])
else
frame.setBackgroundColor(colors.red)
frame.clearLine()
@ -35,7 +35,7 @@ local function updateItemAt(idx, item)
if idx > length then
length = idx
end
redrawLine(idx)
redrawEntry(idx)
end
local function clearFrom(idx)
@ -51,7 +51,7 @@ local function clearFrom(idx)
end
local function redraw()
if drawLine then
if drawEntry then
local tw, th = frame.getSize()
local backingIdx = paginationOffset
for idx = 1, th, 1 do
@ -59,7 +59,7 @@ local function redraw()
if backingList[backingIdx] == nil then
frame.clearLine()
else
drawLine(frame, backingList[backingIdx])
drawEntry(frame, backingList[backingIdx])
end
backingIdx = backingIdx + 1
end
@ -78,13 +78,13 @@ local function updatePage(newOffset)
end
local function updatePartial(filter)
if drawLine then
if drawEntry then
local tw, th = frame.getSize()
local backingIdx = paginationOffset
for idx = 1, th, 1 do
frame.setCursorPos(1, idx)
if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then
drawLine(frame, backingList[backingIdx])
drawEntry(frame, backingList[backingIdx])
end
backingIdx = backingIdx + 1
end
@ -94,7 +94,7 @@ end
return {
setTerm=setTerm,
setDrawLineFunc=setDrawLineFunc,
setDrawEntryFunc=setDrawEntryFunc,
itemAt=itemAt,
updateItemAt=updateItemAt,
clearFrom=clearFrom,

View file

@ -1,18 +1,20 @@
local config = require "config"
local listframes = require "listframes"
local winhlp = require "winhlp"
local colony = peripheral.wrap(config.colony_interface_side)
local orders = {}
local frame = term.current()
local function setTerm(termlike)
frame = termlike
listframes.setTerm(frame)
end
local function fetch(wait_for)
orders = {}
local colony_orders = colony.getWorkOrders()
if colony_orders ~= nil then
for i, wo in pairs(colony_orders) do
local currentIdx = 1
if colony.isInColony() then
for i, wo in pairs(colony.getWorkOrders()) do
local resources = colony.getWorkOrderResources(wo.id)
local res_status = "?/?/0"
if resources ~= nil then
@ -28,53 +30,57 @@ local function fetch(wait_for)
end
res_status = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end
table.insert(orders, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_status=res_status, claimed=wo.isClaimed})
listframes.updateItemAt(currentIdx, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_status=res_status, claimed=wo.isClaimed})
currentIdx = currentIdx + 1
wait_for(config.step_sleep_time)
end
end
listframes.clearFrom(currentIdx)
end
local function drawEntry(win, obj)
local winX, winY = win.getPosition()
local sizeX, sizeY = win.getSize()
win.reposition(winX, winY, sizeX, 2)
win.setBackgroundColor(colors.black)
win.setTextColor(colors.lightGray)
win.write(obj.id)
win.write(" ")
win.setTextColor(colors.yellow)
if obj.pos then
win.write(obj.pos.x .. "," .. obj.pos.y .. "," .. obj.pos.z)
win.write(" ")
end
if obj.res_status then
winhlp.alignRight(win, #obj.res_status + 4)
win.write(" " .. obj.res_status)
else
winhlp.alignRight(win, 3)
end
win.write(" ")
if obj.claimed then
win.setTextColor(colors.green)
win.write("C")
else
win.write(" ")
end
win.write(" ")
win.setTextColor(colors.gray)
win.setCursorPos(1, 2)
if obj.target then
winhlp.alignRight(win, #obj.target + 1)
win.write(" " .. obj.target)
end
win.setTextColor(colors.white)
win.setCursorPos(1, 2)
if obj.type then
win.write(obj.type)
end
end
listframes.setDrawEntryFunc(drawEntry)
local function displayTab(self)
frame.clear()
local tw,th = frame.getSize()
for i, val in pairs(orders) do
local line_num = (i-self.scrollPos)*2+1
if line_num > 0 and line_num < th-1 then
frame.setTextColor(colors.lightGray)
frame.setCursorPos(1,line_num)
frame.write(val.id)
frame.write(" ")
if val.pos then
frame.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z)
frame.write(" ")
end
frame.setCursorPos(2,line_num+1)
frame.setTextColor(colors.white)
if val.type then
frame.write(val.type)
frame.write(" ")
end
if val.target then
frame.write(val.target)
frame.write(" ")
end
if val.res_status then
frame.setCursorPos(tw-2-#val.res_status, line_num)
frame.write(val.res_status)
else
frame.setCursorPos(tw-2, line_num)
end
frame.write(" ")
if val.claimed then
frame.setTextColor(colors.yellow)
frame.write("C")
frame.setTextColor(colors.white)
else
frame.write(" ")
end
frame.write(" ")
end
end
listframes.updatePage(self.scrollPos)
end
local function onTouch(self, touch_x, touch_y)

View file

@ -21,13 +21,13 @@ local function setTerm(termlike)
end
local function fetch(wait_for)
local reqs = colony.getRequests()
local me_items = nil
if config.has_me then
me_items = main_me.listItems()
end
local currentIdx = 1
for i, req in pairs(reqs) do
if colony.isInColony() then
for i, req in pairs(colony.getRequests()) do
local found = false
if me_items then
local left_amount = req.count
@ -53,6 +53,7 @@ local function fetch(wait_for)
end
wait_for(config.step_sleep_time)
end
end
listview.clearFrom(currentIdx)
end
@ -85,7 +86,7 @@ local function drawLine(termlike, obj)
termlike.write("] ")
end
end
listview.setDrawLineFunc(drawLine)
listview.setDrawEntryFunc(drawLine)
local function displayTab(self)
listview.updatePage(self.scrollPos)

View file

@ -32,6 +32,7 @@ if update_update() then
download_file("winhlp.lua")
download_file("tabview.lua")
download_file("listview.lua")
download_file("listframes.lua")
download_file("requests.lua")
download_file("orders.lua")
download_file("test.lua")