opus/apps/Peripherals.lua

214 lines
4.5 KiB
Lua

require = requireInjector(getfenv(1))
local Util = require('util')
local Event = require('event')
local UI = require('ui')
multishell.setTitle(multishell.getCurrent(), 'Devices')
--[[ -- PeripheralsPage -- ]] --
local peripheralsPage = UI.Page({
grid = UI.ScrollingGrid({
columns = {
{ heading = 'Type', key = 'type' },
{ heading = 'Side', key = 'side' }
},
sortColumn = 'type',
height = UI.term.height - 1,
autospace = true,
}),
statusBar = UI.StatusBar({
status = 'Select peripheral'
}),
accelerators = {
q = 'quit',
},
})
function peripheralsPage.grid:draw()
local sides = peripheral.getNames()
Util.clear(self.values)
for _,side in pairs(sides) do
table.insert(self.values, {
type = peripheral.getType(side),
side = side
})
end
self:update()
self:adjustWidth()
UI.Grid.draw(self)
end
function peripheralsPage:updatePeripherals()
if UI:getCurrentPage() == self then
self.grid:draw()
self:sync()
end
end
function peripheralsPage:eventHandler(event)
if event.type == 'quit' then
Event.exitPullEvents()
elseif event.type == 'grid_select' then
UI:setPage('methods', event.selected)
end
return UI.Page.eventHandler(self, event)
end
--[[ -- MethodsPage -- ]] --
local methodsPage = UI.Page({
grid = UI.ScrollingGrid({
columns = {
{ heading = 'Name', key = 'name', width = UI.term.width }
},
sortColumn = 'name',
height = 7,
}),
container = UI.Window({
y = 8,
height = UI.term.height-8,
viewportConsole = UI.ViewportWindow({
backgroundColor = colors.brown
}),
}),
statusBar = UI.StatusBar({
status = 'q to return',
}),
accelerators = {
q = 'back',
backspace = 'back',
},
})
function methodsPage:enable(p)
self.peripheral = p or self.peripheral
local p = peripheral.wrap(self.peripheral.side)
if not p.getAdvancedMethodsData then
self.grid.values = { }
for name,f in pairs(p) do
table.insert(self.grid.values, {
name = name,
noext = true,
})
end
else
self.grid.values = p.getAdvancedMethodsData()
for name,f in pairs(self.grid.values) do
f.name = name
end
end
self.grid:update()
self.grid:setIndex(1)
self.statusBar:setStatus(self.peripheral.type)
UI.Page.enable(self)
end
function methodsPage.container.viewportConsole:draw()
if methodsPage.grid:getSelected() then
methodsPage:drawMethodInfo(self, methodsPage.grid:getSelected())
end
end
function methodsPage:eventHandler(event)
if event.type == 'back' then
UI:setPage(peripheralsPage)
return true
elseif event.type == 'grid_focus_row' then
self.container.viewportConsole.height = 1
self.container.viewportConsole.offset = 0
self.container.viewportConsole.y = 1
self:drawMethodInfo(self.container.viewportConsole, event.selected)
end
return UI.Page.eventHandler(self, event)
end
function methodsPage:drawMethodInfo(c, method)
c:clear()
c:setCursorPos(1, 1)
if method.noext then
c:print('No extended Information')
return 2
end
if method.description then
c:print(method.description)
end
c.cursorY = c.cursorY + 2
c.cursorX = 1
if method.returnTypes ~= '()' then
c:print(method.returnTypes .. ' ', nil, colors.yellow)
end
c:print(method.name, nil, colors.black)
c:print('(')
local maxArgLen = 1
for k,arg in ipairs(method.args) do
if #arg.description > 0 then
maxArgLen = math.max(#arg.name, maxArgLen)
end
local argName = arg.name
local fg = colors.green
if arg.optional then
argName = string.format('[%s]', arg.name)
fg = colors.orange
end
c:print(argName, nil, fg)
if k < #method.args then
c:print(', ')
end
end
c:print(')')
c.cursorY = c.cursorY + 1
if #method.args > 0 then
for _,arg in ipairs(method.args) do
if #arg.description > 0 then
c.cursorY = c.cursorY + 1
c.cursorX = 1
local fg = colors.green
if arg.optional then
fg = colors.orange
end
c:print(arg.name .. ': ', nil, fg)
c.cursorX = maxArgLen + 3
c:print(arg.description, nil, nil, maxArgLen + 3)
end
end
end
c.height = c.cursorY + 1
term.setBackgroundColor(colors.black)
return y
end
Event.addHandler('peripheral', function()
peripheralsPage:updatePeripherals()
end)
Event.addHandler('peripheral_detach', function()
peripheralsPage:updatePeripherals()
end)
UI:setPage(peripheralsPage)
UI:setPages({
methods = methodsPage,
})
Event.pullEvents()
UI.term:reset()