2024-06-03 21:18:16 +00:00
|
|
|
ListView = {
|
|
|
|
backingList = {},
|
|
|
|
length = 0,
|
|
|
|
paginationPos = 1,
|
|
|
|
frame = term.current(),
|
|
|
|
drawEntry = nil,
|
|
|
|
}
|
2024-06-01 22:52:58 +00:00
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:new(o)
|
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
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])
|
2024-06-02 02:22:07 +00:00
|
|
|
else
|
2024-06-03 21:18:16 +00:00
|
|
|
self.frame.setBackgroundColor(colors.red)
|
|
|
|
self.frame.clearLine()
|
|
|
|
self.frame.setBackgroundColor(colors.black)
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:itemAt(idx)
|
|
|
|
return self.backingList[idx]
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:updateItemAt(idx, item)
|
|
|
|
self.backingList[idx] = item
|
|
|
|
if idx > self.length then
|
|
|
|
self.length = idx
|
2024-06-01 23:39:01 +00:00
|
|
|
end
|
2024-06-03 21:18:16 +00:00
|
|
|
self:redrawEntry(idx)
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:clearFrom(idx)
|
|
|
|
local tw, th = self.frame.getSize()
|
|
|
|
for j = self.length, idx, -1 do
|
|
|
|
self.backingList[j] = nil
|
2024-06-01 23:39:01 +00:00
|
|
|
end
|
2024-06-03 21:18:16 +00:00
|
|
|
self.length = idx-1
|
|
|
|
for y = idx-self.paginationPos+1, th, 1 do
|
|
|
|
self.frame.setCursorPos(1, y)
|
|
|
|
self.frame.clearLine()
|
2024-06-01 23:39:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:redraw()
|
|
|
|
if self.drawEntry then
|
|
|
|
local tw, th = self.frame.getSize()
|
|
|
|
local backingIdx = self.paginationPos
|
2024-06-01 22:52:58 +00:00
|
|
|
for idx = 1, th, 1 do
|
2024-06-03 21:18:16 +00:00
|
|
|
self.frame.setCursorPos(1, idx)
|
|
|
|
if self.backingList[backingIdx] == nil then
|
|
|
|
self.frame.clearLine()
|
2024-06-02 00:08:55 +00:00
|
|
|
else
|
2024-06-03 21:18:16 +00:00
|
|
|
self.drawEntry(self.frame, self.backingList[backingIdx])
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
2024-06-02 00:08:55 +00:00
|
|
|
backingIdx = backingIdx + 1
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
2024-06-02 02:22:07 +00:00
|
|
|
else
|
2024-06-03 21:18:16 +00:00
|
|
|
self.frame.setBackgroundColor(colors.red)
|
|
|
|
self.frame.clear()
|
|
|
|
self.frame.setBackgroundColor(colors.black)
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:updatePage(scrollPos)
|
|
|
|
if scrollPos ~= self.paginationPos then
|
|
|
|
self.paginationPos = scrollPos
|
|
|
|
self:redraw()
|
2024-06-01 22:52:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
function ListView:updatePartial(filter)
|
|
|
|
if self.drawEntry then
|
|
|
|
local tw, th = self.frame.getSize()
|
|
|
|
local backingIdx = self.paginationPos
|
2024-06-02 02:22:07 +00:00
|
|
|
for idx = 1, th, 1 do
|
2024-06-03 21:18:16 +00:00
|
|
|
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])
|
2024-06-02 02:22:07 +00:00
|
|
|
end
|
|
|
|
backingIdx = backingIdx + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-01 22:52:58 +00:00
|
|
|
|
2024-06-03 21:18:16 +00:00
|
|
|
return ListView
|