cc/inspect.lua

68 lines
1.5 KiB
Lua

local config = require "config"
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 function setTerm(termlike)
listview.frame = termlike
end
local function prepareObject(other)
local keys = {}
for key, _ in other do
keys.insert(key)
end
table.sort(keys)
return other, keys
end
local obj = {}
local keys = {}
local colony = peripheral.wrap(config.colony_interface_side)
if colony.isInColony() then
obj, keys = prepareObject{colony.getWorkOrders()[1]}
end
local function setObject(other)
obj, keys = prepareObject{other}
end
local function fetch(wait_for)
local currentIdx = 1
for i, key in pairs(keys) do
listview:updateItemAt(currentIdx, {key=key, value=obj[key]})
currentIdx = currentIdx + 1
end
listview:clearFrom(currentIdx)
end
local function displayTab(self)
listview:updatePage(self.scrollPos)
end
local function estimatedHeight()
return #keys
end
return {
name="inspect",
setTerm=setTerm,
setObject=setObject,
fetch=fetch,
displayTab=displayTab,
estimatedHeight=estimatedHeight,
}