1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 08:26:16 +00:00
opus/sys/apps/Peripherals.lua

220 lines
4.6 KiB
Lua
Raw Normal View History

2016-12-23 04:22:04 +00:00
local injector = requireInjector or load(http.get('http://pastebin.com/raw/c0TWsScv').readAll())()
require = injector(getfenv(1))
2016-12-11 19:24:52 +00:00
local Util = require('util')
local Event = require('event')
local UI = require('ui')
multishell.setTitle(multishell.getCurrent(), 'Devices')
--[[ -- PeripheralsPage -- ]] --
2016-12-14 18:54:13 +00:00
local peripheralsPage = UI.Page {
grid = UI.ScrollingGrid {
2016-12-11 19:24:52 +00:00
columns = {
{ heading = 'Type', key = 'type' },
2016-12-14 18:54:13 +00:00
{ heading = 'Side', key = 'side' },
2016-12-11 19:24:52 +00:00
},
sortColumn = 'type',
height = UI.term.height - 1,
autospace = true,
2016-12-14 18:54:13 +00:00
},
statusBar = UI.StatusBar {
2016-12-11 19:24:52 +00:00
status = 'Select peripheral'
2016-12-14 18:54:13 +00:00
},
2016-12-11 19:24:52 +00:00
accelerators = {
q = 'quit',
},
2016-12-14 18:54:13 +00:00
}
2016-12-11 19:24:52 +00:00
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 -- ]] --
2016-12-14 18:54:13 +00:00
local methodsPage = UI.Page {
grid = UI.ScrollingGrid {
2016-12-11 19:24:52 +00:00
columns = {
{ heading = 'Name', key = 'name', width = UI.term.width }
},
sortColumn = 'name',
height = 7,
2016-12-14 18:54:13 +00:00
},
viewportConsole = UI.ViewportWindow {
2016-12-11 19:24:52 +00:00
y = 8,
2016-12-14 18:54:13 +00:00
height = UI.term.height - 8,
backgroundColor = colors.brown,
},
statusBar = UI.StatusBar {
2016-12-11 19:24:52 +00:00
status = 'q to return',
2016-12-14 18:54:13 +00:00
},
2016-12-11 19:24:52 +00:00
accelerators = {
q = 'back',
backspace = 'back',
},
2016-12-14 18:54:13 +00:00
}
2016-12-11 19:24:52 +00:00
function methodsPage:enable(p)
self.peripheral = p or self.peripheral
local p = peripheral.wrap(self.peripheral.side)
2017-04-05 16:35:03 +00:00
if p.getDocs then
self.grid.values = { }
for k,v in pairs(p.getDocs()) do
table.insert(self.grid.values, {
name = k,
doc = v,
})
end
elseif not p.getAdvancedMethodsData then
2016-12-11 19:24:52 +00:00
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
2016-12-14 18:54:13 +00:00
self.viewportConsole.offy = 0
2016-12-11 19:24:52 +00:00
self.grid:update()
self.grid:setIndex(1)
self.statusBar:setStatus(self.peripheral.type)
UI.Page.enable(self)
end
function methodsPage:eventHandler(event)
if event.type == 'back' then
UI:setPage(peripheralsPage)
return true
elseif event.type == 'grid_focus_row' then
2016-12-14 18:54:13 +00:00
self.viewportConsole.offy = 0
self.viewportConsole:draw()
2016-12-11 19:24:52 +00:00
end
return UI.Page.eventHandler(self, event)
end
2016-12-14 18:54:13 +00:00
function methodsPage.viewportConsole:draw()
local c = self
local method = methodsPage.grid:getSelected()
2016-12-11 19:24:52 +00:00
c:clear()
c:setCursorPos(1, 1)
if method.noext then
2016-12-14 18:54:13 +00:00
c.cursorY = 2
2016-12-11 19:24:52 +00:00
c:print('No extended Information')
return 2
end
2017-04-05 16:35:03 +00:00
if method.doc then
c:print(method.doc, nil, colors.yellow)
c.ymax = c.cursorY + 1
return
end
2016-12-11 19:24:52 +00:00
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
2016-12-14 18:54:13 +00:00
c.ymax = c.cursorY + 1
2016-12-11 19:24:52 +00:00
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()