From 3d3a0956b2a0a71e380a4ade15b441d4d88bb50a Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Mon, 3 Jun 2024 23:18:16 +0200 Subject: [PATCH] [listview] refactor to class --- inspect.lua | 38 +++++++-------- listview.lua | 131 +++++++++++++++++++++++---------------------------- requests.lua | 96 ++++++++++++++++++------------------- 3 files changed, 123 insertions(+), 142 deletions(-) diff --git a/inspect.lua b/inspect.lua index 13748f9..c9453d0 100644 --- a/inspect.lua +++ b/inspect.lua @@ -1,13 +1,24 @@ -local listview = require "listview" 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 obj = {a="b", c="d"} -local frame = term.current() local function setTerm(termlike) - frame = termlike - listview.setTerm(frame) + listview.frame = termlike end local function setObject(other) @@ -22,33 +33,20 @@ end local function fetch(wait_for) local currentIdx = 1 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 end - listview.clearFrom(currentIdx) + listview:clearFrom(currentIdx) end local function displayTab(self) - listview.updatePage(self.scrollPos) + listview:updatePage(self.scrollPos) end local function estimatedHeight() return #keys 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 { name="inspect", diff --git a/listview.lua b/listview.lua index cfb1b96..4699aba 100644 --- a/listview.lua +++ b/listview.lua @@ -1,94 +1,91 @@ -local backingList = {} -local length = 0 -local paginationPos = 1 -local frame = term.current() -local drawEntry = nil +ListView = { + backingList = {}, + length = 0, + paginationPos = 1, + frame = term.current(), + drawEntry = nil, +} -local function setTerm(termlike) - frame = termlike +function ListView:new(o) + o = o or {} + setmetatable(o, self) + self.__index = self + return o 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]) +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 - frame.setBackgroundColor(colors.red) - frame.clearLine() - frame.setBackgroundColor(colors.black) + self.frame.setBackgroundColor(colors.red) + self.frame.clearLine() + self.frame.setBackgroundColor(colors.black) end end end -local function itemAt(idx) - return backingList[idx] +function ListView:itemAt(idx) + return self.backingList[idx] end -local function updateItemAt(idx, item) - backingList[idx] = item - if idx > length then - length = idx +function ListView:updateItemAt(idx, item) + self.backingList[idx] = item + if idx > self.length then + self.length = idx end - redrawEntry(idx) + self: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 +function ListView:clearFrom(idx) + local tw, th = self.frame.getSize() + for j = self.length, idx, -1 do + self.backingList[j] = nil end - length = idx-1 - for y = idx-paginationPos+1, th, 1 do - frame.setCursorPos(1, y) - frame.clearLine() + self.length = idx-1 + for y = idx-self.paginationPos+1, th, 1 do + self.frame.setCursorPos(1, y) + self.frame.clearLine() end end -local function redraw() - if drawEntry then - local tw, th = frame.getSize() - local backingIdx = paginationPos +function ListView:redraw() + if self.drawEntry then + local tw, th = self.frame.getSize() + local backingIdx = self.paginationPos for idx = 1, th, 1 do - frame.setCursorPos(1, idx) - if backingList[backingIdx] == nil then - frame.clearLine() + self.frame.setCursorPos(1, idx) + if self.backingList[backingIdx] == nil then + self.frame.clearLine() else - drawEntry(frame, backingList[backingIdx]) + self.drawEntry(self.frame, self.backingList[backingIdx]) end backingIdx = backingIdx + 1 end else - frame.setBackgroundColor(colors.red) - frame.clear() - frame.setBackgroundColor(colors.black) + self.frame.setBackgroundColor(colors.red) + self.frame.clear() + self.frame.setBackgroundColor(colors.black) end end -local function updatePage(scrollPos) - if scrollPos ~= paginationPos then - paginationPos = scrollPos - redraw() +function ListView:updatePage(scrollPos) + if scrollPos ~= self.paginationPos then + self.paginationPos = scrollPos + self:redraw() end end -local function updatePartial(filter) - if drawEntry then - local tw, th = frame.getSize() - local backingIdx = paginationPos +function ListView:updatePartial(filter) + if self.drawEntry then + local tw, th = self.frame.getSize() + local backingIdx = self.paginationPos for idx = 1, th, 1 do - frame.setCursorPos(1, idx) - if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then - drawEntry(frame, backingList[backingIdx]) + 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 @@ -96,14 +93,4 @@ local function updatePartial(filter) end -return { - setTerm=setTerm, - setDrawEntryFunc=setDrawEntryFunc, - itemAt=itemAt, - updateItemAt=updateItemAt, - length=getLength, - clearFrom=clearFrom, - redraw=redraw, - updatePage=updatePage, - updatePartial=updatePartial, -} +return ListView diff --git a/requests.lua b/requests.lua index 2ea7d51..8cc438c 100644 --- a/requests.lua +++ b/requests.lua @@ -1,6 +1,5 @@ local config = require "config" local permset = require "permset" -local listview = require "listview" local winhlp = require "winhlp" -- list of allowed item names, will be requested @@ -13,50 +12,6 @@ end 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) termlike.setBackgroundColor(colors.black) termlike.setTextColor(colors.white) @@ -86,27 +41,68 @@ local function drawLine(termlike, obj) termlike.write("] ") 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) - listview.updatePage(self.scrollPos) + listview:updatePage(self.scrollPos) end local function estimatedHeight() - return listview.length() + return listview.length end local function onTouch(self, touch_x, touch_y) local tw, th = self.window.getSize() 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 permset.has(allowed_items, obj.answer.name) then permset.remove(allowed_items, obj.answer.name) else permset.add(allowed_items, obj.answer.name) 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 end end