[listview] refactor to class

This commit is contained in:
Ben 2024-06-03 23:18:16 +02:00
parent f2f951662e
commit 3d3a0956b2
Signed by: ben
GPG key ID: 0F54A7ED232D3319
3 changed files with 123 additions and 142 deletions

View file

@ -1,13 +1,24 @@
local listview = require "listview"
local winhlp = require "winhlp" local winhlp = require "winhlp"
local function drawLine(termlike, pair)
termlike.setBackgroundColor(colors.black)
termlike.setTextColor(colors.yellow)
termlike.write(tostring(pair.key))
termlike.setTextColor(colors.white)
local value = tostring(pair.value)
local padding = winhlp.alignRightPadding(termlike, #value + 1)
termlike.write(string.format("%" .. padding+1 .. "s", " "))
termlike.write(value)
end
local listview = (require "listview"):new{drawEntry=drawLine}
local keys = {[1]="a", [2]="c"} local keys = {[1]="a", [2]="c"}
local obj = {a="b", c="d"} local obj = {a="b", c="d"}
local frame = term.current()
local function setTerm(termlike) local function setTerm(termlike)
frame = termlike listview.frame = termlike
listview.setTerm(frame)
end end
local function setObject(other) local function setObject(other)
@ -22,33 +33,20 @@ end
local function fetch(wait_for) local function fetch(wait_for)
local currentIdx = 1 local currentIdx = 1
for i, key in pairs(keys) do for i, key in pairs(keys) do
listview.updateItemAt(currentIdx, {key=key, value=obj[key]}) listview:updateItemAt(currentIdx, {key=key, value=obj[key]})
currentIdx = currentIdx + 1 currentIdx = currentIdx + 1
end end
listview.clearFrom(currentIdx) listview:clearFrom(currentIdx)
end end
local function displayTab(self) local function displayTab(self)
listview.updatePage(self.scrollPos) listview:updatePage(self.scrollPos)
end end
local function estimatedHeight() local function estimatedHeight()
return #keys return #keys
end end
local function drawLine(termlike, pair)
termlike.setBackgroundColor(colors.black)
termlike.setTextColor(colors.yellow)
termlike.write(tostring(pair.key))
termlike.setTextColor(colors.white)
local value = tostring(pair.value)
local padding = winhlp.alignRightPadding(termlike, #value + 1)
termlike.write(string.format("%" .. padding+1 .. "s", " "))
termlike.write(value)
end
listview.setDrawEntryFunc(drawLine)
return { return {
name="inspect", name="inspect",

View file

@ -1,109 +1,96 @@
local backingList = {} ListView = {
local length = 0 backingList = {},
local paginationPos = 1 length = 0,
local frame = term.current() paginationPos = 1,
local drawEntry = nil frame = term.current(),
drawEntry = nil,
local function setTerm(termlike)
frame = termlike
end
local function setDrawEntryFunc(drawLineFunc)
drawEntry = drawLineFunc
end
local function redrawEntry(idx)
local tw, th = frame.getSize()
if idx >= paginationPos and idx-paginationPos <= th then
frame.setCursorPos(1, idx-paginationPos+1)
if drawEntry then
drawEntry(frame, backingList[idx])
else
frame.setBackgroundColor(colors.red)
frame.clearLine()
frame.setBackgroundColor(colors.black)
end
end
end
local function itemAt(idx)
return backingList[idx]
end
local function updateItemAt(idx, item)
backingList[idx] = item
if idx > length then
length = idx
end
redrawEntry(idx)
end
local function getLength()
return length
end
local function clearFrom(idx)
local tw, th = frame.getSize()
for j = length, idx, -1 do
backingList[j] = nil
end
length = idx-1
for y = idx-paginationPos+1, th, 1 do
frame.setCursorPos(1, y)
frame.clearLine()
end
end
local function redraw()
if drawEntry then
local tw, th = frame.getSize()
local backingIdx = paginationPos
for idx = 1, th, 1 do
frame.setCursorPos(1, idx)
if backingList[backingIdx] == nil then
frame.clearLine()
else
drawEntry(frame, backingList[backingIdx])
end
backingIdx = backingIdx + 1
end
else
frame.setBackgroundColor(colors.red)
frame.clear()
frame.setBackgroundColor(colors.black)
end
end
local function updatePage(scrollPos)
if scrollPos ~= paginationPos then
paginationPos = scrollPos
redraw()
end
end
local function updatePartial(filter)
if drawEntry then
local tw, th = frame.getSize()
local backingIdx = paginationPos
for idx = 1, th, 1 do
frame.setCursorPos(1, idx)
if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then
drawEntry(frame, backingList[backingIdx])
end
backingIdx = backingIdx + 1
end
end
end
return {
setTerm=setTerm,
setDrawEntryFunc=setDrawEntryFunc,
itemAt=itemAt,
updateItemAt=updateItemAt,
length=getLength,
clearFrom=clearFrom,
redraw=redraw,
updatePage=updatePage,
updatePartial=updatePartial,
} }
function ListView:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function ListView:redrawEntry(idx)
local tw, th = self.frame.getSize()
if idx >= self.paginationPos and idx-self.paginationPos <= th then
self.frame.setCursorPos(1, idx-self.paginationPos+1)
if self.drawEntry then
self.drawEntry(self.frame, self.backingList[idx])
else
self.frame.setBackgroundColor(colors.red)
self.frame.clearLine()
self.frame.setBackgroundColor(colors.black)
end
end
end
function ListView:itemAt(idx)
return self.backingList[idx]
end
function ListView:updateItemAt(idx, item)
self.backingList[idx] = item
if idx > self.length then
self.length = idx
end
self:redrawEntry(idx)
end
function ListView:clearFrom(idx)
local tw, th = self.frame.getSize()
for j = self.length, idx, -1 do
self.backingList[j] = nil
end
self.length = idx-1
for y = idx-self.paginationPos+1, th, 1 do
self.frame.setCursorPos(1, y)
self.frame.clearLine()
end
end
function ListView:redraw()
if self.drawEntry then
local tw, th = self.frame.getSize()
local backingIdx = self.paginationPos
for idx = 1, th, 1 do
self.frame.setCursorPos(1, idx)
if self.backingList[backingIdx] == nil then
self.frame.clearLine()
else
self.drawEntry(self.frame, self.backingList[backingIdx])
end
backingIdx = backingIdx + 1
end
else
self.frame.setBackgroundColor(colors.red)
self.frame.clear()
self.frame.setBackgroundColor(colors.black)
end
end
function ListView:updatePage(scrollPos)
if scrollPos ~= self.paginationPos then
self.paginationPos = scrollPos
self:redraw()
end
end
function ListView:updatePartial(filter)
if self.drawEntry then
local tw, th = self.frame.getSize()
local backingIdx = self.paginationPos
for idx = 1, th, 1 do
self.frame.setCursorPos(1, idx)
if self.backingList[backingIdx] ~= nil and filter(self.backingList[backingIdx]) then
self.self.drawEntry(self.self.frame, self.backingList[backingIdx])
end
backingIdx = backingIdx + 1
end
end
end
return ListView

View file

@ -1,6 +1,5 @@
local config = require "config" local config = require "config"
local permset = require "permset" local permset = require "permset"
local listview = require "listview"
local winhlp = require "winhlp" local winhlp = require "winhlp"
-- list of allowed item names, will be requested -- list of allowed item names, will be requested
@ -13,50 +12,6 @@ end
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
listview.setTerm(frame)
end
local function fetch(wait_for)
local me_items = nil
if config.has_me then
me_items = main_me.listItems()
end
local currentIdx = 1
if colony.isInColony() then
for i, req in pairs(colony.getRequests()) do
local found = false
if me_items then
local left_amount = req.count
for j, it in pairs(req.items) do
for k, have_it in pairs(me_items) do
-- TODO: check NBT
if have_it.name == it.name then
found = true
listview.updateItemAt(currentIdx, {req=req, answer=have_it})
currentIdx = currentIdx + 1
if left_amount > 0 and permset.has(allowed_items, it.name) then
local export_amount = math.min(have_it.amount, left_amount)
left_amount = left_amount - export_amount
main_me.exportItem({name=it.name, count=export_amount}, config.main_me_inventory_side)
end
end
end
end
end
if found == false then
listview.updateItemAt(currentIdx, {req=req, answer=nil})
currentIdx = currentIdx + 1
end
wait_for(config.step_sleep_time)
end
end
listview.clearFrom(currentIdx)
end
local function drawLine(termlike, obj) local function drawLine(termlike, obj)
termlike.setBackgroundColor(colors.black) termlike.setBackgroundColor(colors.black)
termlike.setTextColor(colors.white) termlike.setTextColor(colors.white)
@ -86,27 +41,68 @@ local function drawLine(termlike, obj)
termlike.write("] ") termlike.write("] ")
end end
end end
listview.setDrawEntryFunc(drawLine) local listview = (require "listview"):new{drawEntry=drawLine}
local function setTerm(termlike)
listview.frame = termlike
end
local function fetch(wait_for)
local me_items = nil
if config.has_me then
me_items = main_me.listItems()
end
local currentIdx = 1
if colony.isInColony() then
for i, req in pairs(colony.getRequests()) do
local found = false
if me_items then
local left_amount = req.count
for j, it in pairs(req.items) do
for k, have_it in pairs(me_items) do
-- TODO: check NBT
if have_it.name == it.name then
found = true
listview:updateItemAt(currentIdx, {req=req, answer=have_it})
currentIdx = currentIdx + 1
if left_amount > 0 and permset.has(allowed_items, it.name) then
local export_amount = math.min(have_it.amount, left_amount)
left_amount = left_amount - export_amount
main_me.exportItem({name=it.name, count=export_amount}, config.main_me_inventory_side)
end
end
end
end
end
if found == false then
listview:updateItemAt(currentIdx, {req=req, answer=nil})
currentIdx = currentIdx + 1
end
wait_for(config.step_sleep_time)
end
end
listview:clearFrom(currentIdx)
end
local function displayTab(self) local function displayTab(self)
listview.updatePage(self.scrollPos) listview:updatePage(self.scrollPos)
end end
local function estimatedHeight() local function estimatedHeight()
return listview.length() return listview.length
end end
local function onTouch(self, touch_x, touch_y) local function onTouch(self, touch_x, touch_y)
local tw, th = self.window.getSize() local tw, th = self.window.getSize()
if touch_x > tw-5 then if touch_x > tw-5 then
local obj = listview.itemAt(touch_y+self.scrollPos-1) local obj = listview:itemAt(touch_y+self.scrollPos-1)
if obj ~= nil and obj.answer ~= nil then if obj ~= nil and obj.answer ~= nil then
if permset.has(allowed_items, obj.answer.name) then if permset.has(allowed_items, obj.answer.name) then
permset.remove(allowed_items, obj.answer.name) permset.remove(allowed_items, obj.answer.name)
else else
permset.add(allowed_items, obj.answer.name) permset.add(allowed_items, obj.answer.name)
end end
listview.updatePartial(function (item) return item.answer ~= nil and item.answer.name == obj.answer.name end) listview:updatePartial(function (item) return item.answer ~= nil and item.answer.name == obj.answer.name end)
return true return true
end end
end end