This isn't as elegant as the other solutions, and it's not based on your code, but it's what I use in my programs. It views/scrolls a page at a time instead of a line at a time. You'll need to make changes to suit your needs. Note that this only works for ordered tables, not sparse tables…
local testData = { "Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10", "Line 11", "Line 12" } --#substitute this with your actual data
local linesPerPage = 10 --# the number of lines per page (change this to suit your needs)
local pageNum = 1 --# which page we're currently on - we're starting at the first page
local numPages = math.ceil(#testData / linesPerPage) --# total number of pages based on number of entries
local function listPage()
term.clear() --# this is just for this example, you may wish to clear just the area the data fills
local xPos = 1 --# cursor X position (change this to be your actual 'top' x position)
local yPos = 1 --# cursor Y position (change this to be your actual 'top' y position)
local startY = 1 --# this should be equal to the initial yPos value
local currentEntry = ((pageNum - 1) * (linesPerPage - 1)) + pageNum
for i = currentEntry, #testData do --# start the listing loop from the current entry
term.setCursorPos(xPos, yPos) --# set the cursor position
term.write(testData[i]) --# write the data to screen
yPos = yPos + 1 --# increment the Y position so the next entry is written on the next display line
if yPos = startY + linesPerPage then break end --# we've listed the set number of entries per page so stop the loop
end
end
local function getInput()
while true do
local _, scroll = os.pullEvent("mouse_scroll")
if scroll == 1 then
pageNum = math.min(pageNum + 1, numPages) --# if the user scrolls down, increment the page number
elseif scroll == -1 then
pageNum = math.max(1, pageNum - 1) --# if the user scrolls up, decrement the page number
end
listPage()
end
end
listPage()
getInput()