cc/inspect.lua

68 lines
1.5 KiB
Lua
Raw Normal View History

2024-06-03 23:07:50 +00:00
local config = require "config"
local winhlp = require "winhlp"
2024-06-03 21:18:16 +00:00
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}
2024-06-02 19:18:04 +00:00
local function setTerm(termlike)
2024-06-03 21:18:16 +00:00
listview.frame = termlike
2024-06-02 19:18:04 +00:00
end
2024-06-03 23:07:50 +00:00
local function prepareObject(other)
local keys = {}
2024-06-02 19:18:04 +00:00
for key, _ in other do
keys.insert(key)
end
table.sort(keys)
2024-06-03 23:07:50 +00:00
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}
2024-06-02 19:18:04 +00:00
end
local function fetch(wait_for)
local currentIdx = 1
for i, key in pairs(keys) do
2024-06-03 21:18:16 +00:00
listview:updateItemAt(currentIdx, {key=key, value=obj[key]})
2024-06-02 19:18:04 +00:00
currentIdx = currentIdx + 1
end
2024-06-03 21:18:16 +00:00
listview:clearFrom(currentIdx)
2024-06-02 19:18:04 +00:00
end
local function displayTab(self)
2024-06-03 21:18:16 +00:00
listview:updatePage(self.scrollPos)
2024-06-02 19:18:04 +00:00
end
local function estimatedHeight()
return #keys
end
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
}