[listview] refactor to class
This commit is contained in:
parent
f2f951662e
commit
3d3a0956b2
38
inspect.lua
38
inspect.lua
|
@ -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",
|
||||||
|
|
131
listview.lua
131
listview.lua
|
@ -1,94 +1,91 @@
|
||||||
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)
|
function ListView:new(o)
|
||||||
frame = termlike
|
o = o or {}
|
||||||
|
setmetatable(o, self)
|
||||||
|
self.__index = self
|
||||||
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setDrawEntryFunc(drawLineFunc)
|
function ListView:redrawEntry(idx)
|
||||||
drawEntry = drawLineFunc
|
local tw, th = self.frame.getSize()
|
||||||
end
|
if idx >= self.paginationPos and idx-self.paginationPos <= th then
|
||||||
|
self.frame.setCursorPos(1, idx-self.paginationPos+1)
|
||||||
local function redrawEntry(idx)
|
if self.drawEntry then
|
||||||
local tw, th = frame.getSize()
|
self.drawEntry(self.frame, self.backingList[idx])
|
||||||
if idx >= paginationPos and idx-paginationPos <= th then
|
|
||||||
frame.setCursorPos(1, idx-paginationPos+1)
|
|
||||||
if drawEntry then
|
|
||||||
drawEntry(frame, backingList[idx])
|
|
||||||
else
|
else
|
||||||
frame.setBackgroundColor(colors.red)
|
self.frame.setBackgroundColor(colors.red)
|
||||||
frame.clearLine()
|
self.frame.clearLine()
|
||||||
frame.setBackgroundColor(colors.black)
|
self.frame.setBackgroundColor(colors.black)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function itemAt(idx)
|
function ListView:itemAt(idx)
|
||||||
return backingList[idx]
|
return self.backingList[idx]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function updateItemAt(idx, item)
|
function ListView:updateItemAt(idx, item)
|
||||||
backingList[idx] = item
|
self.backingList[idx] = item
|
||||||
if idx > length then
|
if idx > self.length then
|
||||||
length = idx
|
self.length = idx
|
||||||
end
|
end
|
||||||
redrawEntry(idx)
|
self:redrawEntry(idx)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getLength()
|
function ListView:clearFrom(idx)
|
||||||
return length
|
local tw, th = self.frame.getSize()
|
||||||
end
|
for j = self.length, idx, -1 do
|
||||||
|
self.backingList[j] = nil
|
||||||
local function clearFrom(idx)
|
|
||||||
local tw, th = frame.getSize()
|
|
||||||
for j = length, idx, -1 do
|
|
||||||
backingList[j] = nil
|
|
||||||
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 ListView:redraw()
|
||||||
if drawEntry then
|
if self.drawEntry then
|
||||||
local tw, th = frame.getSize()
|
local tw, th = self.frame.getSize()
|
||||||
local backingIdx = paginationPos
|
local backingIdx = self.paginationPos
|
||||||
for idx = 1, th, 1 do
|
for idx = 1, th, 1 do
|
||||||
frame.setCursorPos(1, idx)
|
self.frame.setCursorPos(1, idx)
|
||||||
if backingList[backingIdx] == nil then
|
if self.backingList[backingIdx] == nil then
|
||||||
frame.clearLine()
|
self.frame.clearLine()
|
||||||
else
|
else
|
||||||
drawEntry(frame, backingList[backingIdx])
|
self.drawEntry(self.frame, self.backingList[backingIdx])
|
||||||
end
|
end
|
||||||
backingIdx = backingIdx + 1
|
backingIdx = backingIdx + 1
|
||||||
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 updatePage(scrollPos)
|
function ListView:updatePage(scrollPos)
|
||||||
if scrollPos ~= paginationPos then
|
if scrollPos ~= self.paginationPos then
|
||||||
paginationPos = scrollPos
|
self.paginationPos = scrollPos
|
||||||
redraw()
|
self:redraw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function updatePartial(filter)
|
function ListView:updatePartial(filter)
|
||||||
if drawEntry then
|
if self.drawEntry then
|
||||||
local tw, th = frame.getSize()
|
local tw, th = self.frame.getSize()
|
||||||
local backingIdx = paginationPos
|
local backingIdx = self.paginationPos
|
||||||
for idx = 1, th, 1 do
|
for idx = 1, th, 1 do
|
||||||
frame.setCursorPos(1, idx)
|
self.frame.setCursorPos(1, idx)
|
||||||
if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then
|
if self.backingList[backingIdx] ~= nil and filter(self.backingList[backingIdx]) then
|
||||||
drawEntry(frame, backingList[backingIdx])
|
self.self.drawEntry(self.self.frame, self.backingList[backingIdx])
|
||||||
end
|
end
|
||||||
backingIdx = backingIdx + 1
|
backingIdx = backingIdx + 1
|
||||||
end
|
end
|
||||||
|
@ -96,14 +93,4 @@ local function updatePartial(filter)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return {
|
return ListView
|
||||||
setTerm=setTerm,
|
|
||||||
setDrawEntryFunc=setDrawEntryFunc,
|
|
||||||
itemAt=itemAt,
|
|
||||||
updateItemAt=updateItemAt,
|
|
||||||
length=getLength,
|
|
||||||
clearFrom=clearFrom,
|
|
||||||
redraw=redraw,
|
|
||||||
updatePage=updatePage,
|
|
||||||
updatePartial=updatePartial,
|
|
||||||
}
|
|
||||||
|
|
96
requests.lua
96
requests.lua
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue