79 lines
1.7 KiB
Lua
79 lines
1.7 KiB
Lua
local backingList = {}
|
|
local length = 0
|
|
local paginationOffset = 1
|
|
local frame = term.current()
|
|
local drawLine = nil
|
|
|
|
local function setTerm(termlike)
|
|
frame = termlike
|
|
end
|
|
|
|
local function setDrawLineFunc(drawLineFunc)
|
|
drawLine = drawLineFunc
|
|
end
|
|
|
|
local function redrawLine(idx)
|
|
if drawLine then
|
|
local tw, th = frame.getSize()
|
|
if idx >= paginationOffset and idx-paginationOffset <= th then
|
|
frame.setCursorPos(1,idx-paginationOffset+1)
|
|
drawLine(frame, backingList[idx])
|
|
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
|
|
redrawLine(idx)
|
|
end
|
|
|
|
local function clearAfter(idx)
|
|
local tw, th = frame.getSize()
|
|
for j = length, idx+1, -1 do
|
|
backingList[j] = nil
|
|
end
|
|
length = idx
|
|
for y = paginationOffset-idx+2, th, 1 do
|
|
frame.setCursorPos(1, y)
|
|
frame.clearLine()
|
|
end
|
|
end
|
|
|
|
local function redraw()
|
|
if drawLine then
|
|
local tw, th = frame.getSize()
|
|
local backingIdx = paginationOffset
|
|
for idx = 1, th, 1 do
|
|
if backingList[backingIdx] ~= nil then
|
|
frame.setCursorPos(idx)
|
|
drawLine(frame, backingList[backingIdx])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function updatePage(newOffset)
|
|
if newOffset ~= paginationOffset then
|
|
paginationOffset = newOffset
|
|
redraw()
|
|
end
|
|
end
|
|
|
|
|
|
return {
|
|
setTerm=setTerm,
|
|
setDrawLineFunc=setDrawLineFunc,
|
|
itemAt=itemAt,
|
|
updateItemAt=updateItemAt,
|
|
clearAfter=clearAfter,
|
|
redraw=redraw,
|
|
updatePage=updatePage,
|
|
}
|