cc/inspect.lua

61 lines
1.3 KiB
Lua
Raw Normal View History

2024-06-02 19:18:04 +00:00
local listview = require "listview"
local winhlp = require "winhlp"
2024-06-02 19:18:04 +00:00
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)
2024-06-02 19:18:04 +00:00
end
local function setObject(other)
obj = other
keys = {}
for key, _ in other do
keys.insert(key)
end
table.sort(keys)
end
local function fetch(wait_for)
local currentIdx = 1
for i, key in pairs(keys) do
listview.updateItemAt(currentIdx, {key=key, value=obj[key]})
2024-06-02 19:18:04 +00:00
currentIdx = currentIdx + 1
end
listview.clearFrom(currentIdx)
end
local function displayTab(self)
listview.updatePage(self.scrollPos)
end
local function estimatedHeight()
return #keys
end
local function drawLine(termlike, pair)
2024-06-02 19:47:51 +00:00
termlike.setBackgroundColor(colors.black)
termlike.setTextColor(colors.yellow)
termlike.write(tostring(pair.key))
2024-06-02 19:47:51 +00:00
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)
2024-06-02 19:47:51 +00:00
end
listview.setDrawEntryFunc(drawLine)
2024-06-02 19:18:04 +00:00
return {
name="inspect",
setTerm=setTerm,
setObject=setObject,
fetch=fetch,
displayTab=displayTab,
estimatedHeight=estimatedHeight,
2024-06-02 19:18:04 +00:00
}