Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vos.loadApi('core.api')
- vos.loadApi('ui.api')
- multishell.setTitle(TAB_ID, 'Peripherals')
- local currentPeripheral
- local currentMethod
- --[[ -- PeripheralsPage -- ]] --
- peripheralsPage = UI.Page({
- grid = UI.ScrollingGrid({
- columns = {
- { 'Type', 'type', 10 },
- { 'Side', 'side', 8 }
- },
- sortColumn = 'type',
- pageSize = UI.term.height-2,
- width = UI.term.width,
- autospace = true,
- y = 1,
- x = 1
- }),
- statusBar = UI.StatusBar({
- status = 'Select peripheral'
- }),
- accelerators = {
- q = 'quit',
- },
- })
- function peripheralsPage:eventHandler(event)
- if event.type == 'quit' then
- Event.exitPullEvents()
- elseif event.type == 'grid_select' then
- currentPeripheral = self.grid:getSelected()
- methodsPage.grid.index = 1
- methodsPage.grid.scrollOffset = 1
- UI.pager:setPage(methodsPage)
- end
- return UI.Page.eventHandler(self, event)
- end
- function peripheralsPage:updatePeripherals()
- local sides = peripheral.getNames()
- local t = { }
- for _,side in pairs(sides) do
- table.insert(t, {
- type = peripheral.getType(side),
- side = side
- })
- end
- self.grid:setTable(t)
- self.grid:adjustWidth()
- if UI.pager:getCurrentPage() == peripheralsPage then
- peripheralsPage:draw()
- end
- end
- Event.addHandler('peripheral', function()
- peripheralsPage:updatePeripherals()
- end)
- Event.addHandler('peripheral_detach', function()
- peripheralsPage:updatePeripherals()
- end)
- --[[-- argumentsPage --]]--
- function getArguments(method)
- local fields = { }
- for key,arg in ipairs(method.args) do
- table.insert(fields, {
- label = arg.name,
- type = string.lower(arg.type),
- required = not arg.optional,
- key = key,
- width = 20,
- limit = 100,
- display = UI.Form.D.entry
- })
- end
- table.insert(fields,
- { text = 'Run', event = 'accept', display = UI.Form.D.button,
- y = #method.args + 2, width = 10 })
- table.insert(fields,
- { text = 'Cancel', event = 'cancel', display = UI.Form.D.button,
- x = 35, y = #method.args + 2, width = 10 })
- argumentsPage = UI.Page({
- method = method,
- methodName = UI.Text({
- value = method.name,
- width = #method.name,
- y = 2,
- x = 2,
- }),
- form = UI.Form({
- fields = fields,
- labelWidth = 15,
- valueWidth = 25,
- x = 2,
- y = 4,
- width = UI.term.width - 2,
- height = UI.term.height - 5
- }),
- statusBar = UI.StatusBar({
- status = currentPeripheral.type .. ' : ' .. method.name
- }),
- })
- function argumentsPage:eventHandler(event)
- if event.type == 'accept' then
- local success, msg = methodsPage:executeMethod(method, unpack(self.form.values))
- if not success then
- self.statusBar:setStatus(msg)
- self.statusBar:draw()
- end
- return true
- elseif event.type == 'cancel' then
- UI.pager:setPreviousPage()
- return true
- end
- return UI.Page.eventHandler(self, event)
- end
- UI.pager:setPage(argumentsPage)
- end
- --[[ -- MethodsPage -- ]] --
- methodsPage = UI.Page({
- grid = UI.ScrollingGrid({
- columns = {
- { 'Name', 'name', UI.term.width }
- },
- sortColumn = 'name',
- pageSize = 6,
- width = UI.term.width,
- sortMethod = function() return false end,
- y = 1,
- x = 1
- }),
- viewportConsole = UI.ViewportWindow({
- y = 8,
- height = UI.term.height-8,
- backgroundColor = colors.brown
- }),
- statusBar = UI.StatusBar({
- status = 'q to return',
- x = 1
- }),
- accelerators = {
- q = 'back',
- backspace = 'back',
- i = 'info',
- },
- })
- function methodsPage:enable()
- local p = peripheral.wrap(currentPeripheral.side)
- if not p.getAdvancedMethodsData then
- self.grid.t = { }
- for name,f in pairs(p) do
- table.insert(self.grid.t, {
- name = name,
- noext = true,
- })
- end
- else
- self.grid.t = p.getAdvancedMethodsData()
- for name,f in pairs(self.grid.t) do
- f.name = name
- end
- end
- self.grid.selected = nil
- self.statusBar:setStatus(currentPeripheral.type)
- end
- function methodsPage.grid:setSelected(selected)
- self.selected = selected
- -- drawMethodInfo(methodsPage.viewportConsole, selected)
- end
- function methodsPage.viewportConsole:draw()
- if methodsPage.grid:getSelected() then
- -- drawMethodInfo(self, methodsPage.grid:getSelected())
- end
- end
- function methodsPage:executeMethod(method, ...)
- local args = { ... }
- local result
- local success, err = pcall(function()
- result = peripheral.call(currentPeripheral.side, method.name, unpack(args))
- end)
- if not success then
- return false, err or 'Error calling method'
- elseif result then
- resultsPage:setResult(result)
- resultsPage.statusBar:setStatus(currentPeripheral.type .. ' : ' .. method.name)
- UI.pager:setPage(resultsPage)
- return true
- end
- return false, 'No result'
- end
- function methodsPage:eventHandler(event)
- if event.type == 'back' then
- UI.pager:setPage(peripheralsPage)
- return true
- elseif event.type == 'grid_select' then
- local method = self.grid:getSelected()
- if not method.args or #method.args == 0 then
- local success, msg = self:executeMethod(method)
- if not success then
- self.statusBar:setStatus(msg)
- self.statusBar:draw()
- end
- else
- getArguments(method)
- end
- return true
- elseif event.type == 'info' then
- if self.extendedInfo then
- UI.pager:setPage(methodDetailsPage)
- end
- return true
- elseif event.type == 'grid_focus_row' then
- drawMethodInfo(self.viewportConsole, self.grid:getSelected())
- end
- return UI.Page.eventHandler(self, event)
- end
- --[[-- methodDetailsPage --]]--
- methodDetailsPage = UI.Page({
- viewportConsole = UI.ViewportWindow({
- height = UI.term.height-1,
- backgroundColor = colors.brown,
- textColor = colors.white
- }),
- statusBar = UI.StatusBar({
- status = 'enter to return'
- }),
- accelerators = {
- q = 'back',
- backspace = 'back',
- enter = 'back',
- },
- })
- function methodDetailsPage:enable()
- self.viewportConsole.offset = 0
- self.viewportConsole.vpHeight = UI.term.height-1
- end
- function methodDetailsPage.viewportConsole:draw()
- drawMethodInfo(self, methodsPage.grid:getSelected())
- end
- function methodDetailsPage:eventHandler(event)
- if event.type == 'back' then
- UI.pager:setPreviousPage()
- return true
- end
- UI.Page.eventHandler(self, event)
- end
- --[[ -- resultsPage -- ]] --
- resultsPage = UI.Page({
- grid = UI.ScrollingGrid({
- columns = {
- { 'Name', 'name', 10 },
- { 'Value', 'value', 10 }
- },
- sortColumn = 'name',
- pageSize = UI.term.height-3,
- width = UI.term.width,
- autospace = true,
- y = 1
- }),
- statusBar = UI.StatusBar({
- status = 'q to return'
- }),
- accelerators = {
- backspace = 'back',
- q = 'return',
- },
- })
- function resultsPage:setResult(result)
- local t = { }
- if type(result) == 'table' then
- for k,v in pairs(result) do
- local entry = {
- name = k,
- value = tostring(v)
- }
- if type(v) == 'table' then
- if Util.size(v) == 0 then
- entry.value = 'table: (empty)'
- else
- entry.value = 'table'
- entry.table = v
- end
- end
- table.insert(t, entry)
- end
- else
- table.insert(t, {
- name = 'result',
- value = tostring(result)
- })
- end
- self.grid.t = t
- self.grid:adjustWidth()
- end
- function resultsPage:enable()
- self.currentResult = self.grid.t
- self.resultPath = { }
- end
- function resultsPage:eventHandler(event)
- if event.type == 'grid_select' then
- local nameValue = self.grid:getSelected()
- if nameValue.table then
- if Util.size(nameValue.table) > 0 then
- table.insert(self.resultPath, self.grid.t)
- self:setResult(nameValue.table)
- self:draw()
- else
- self.statusBar:timedStatus('Table is empty', 3)
- end
- end
- return true
- elseif event.type == 'back' then
- if #self.resultPath > 0 then
- self.grid.t = table.remove(self.resultPath)
- self.grid:adjustWidth()
- self.grid:draw()
- else
- UI.pager:setPreviousPage()
- end
- return true
- elseif event.type == 'return' then
- UI.pager:setPreviousPage()
- return true
- end
- UI.Page.eventHandler(self, event)
- end
- --[[ -- Common logic -- ]] --
- function drawMethodInfo(c, method)
- c:reset()
- c:setCursorPos(1, 2)
- if method.noext then
- c:print('No extended Information')
- return 2
- end
- if method.description then
- c:print(method.description)
- c:print('')
- end
- local str = method.name .. '('
- for k,arg in ipairs(method.args) do
- str = str .. arg.name
- if k < #method.args then
- str = str .. ', '
- end
- end
- c:print(str .. ')')
- -- local sb = UI.StringBuffer(0)
- -- if #method.returnTypes == 9000 then
- -- sb:clear()
- -- sb:append('Returns: ')
- -- for k,ret in ipairs(method.returnTypes) dofile(
- -- sb:append(ret)
- -- if k < #method.returnTypes then
- -- sb:append(', ')
- -- end
- -- end
- -- c:print(sb:get())
- -- end
- if #method.args > 0 then
- for _,arg in ipairs(method.args) do
- c:print('')
- c:print(arg.name .. ': ' .. arg.description)
- c:print('')
- c:print('optional nullable type vararg')
- c:print('-------- -------- ---- ------')
- sb:clear()
- sb:insert(tostring(arg.optional), 0)
- sb:insert(tostring(arg.nullable), 9)
- sb:insert(arg.type, 18)
- sb:insert(tostring(arg.vararg), 26)
- c:print(sb:get())
- end
- end
- term.setBackgroundColor(colors.black)
- return y
- end
- local args = { ... }
- if #args > 0 then
- local side = args[1]
- local sides = peripheral.getNames()
- local t = { }
- for _,s in pairs(sides) do
- if side == s then
- currentPeripheral = {
- type = peripheral.getType(side),
- side = side
- }
- break
- end
- end
- end
- --[[ -- Startup logic -- ]] --
- local args = { ... }
- if #args > 0 then
- local side = args[1]
- local sides = peripheral.getNames()
- local t = { }
- for _,s in pairs(sides) do
- if side == s then
- currentPeripheral = {
- type = peripheral.getType(side),
- side = side
- }
- break
- end
- end
- end
- Logger.disable()
- peripheralsPage:updatePeripherals()
- if currentPeripheral then
- UI.pager:setPage(methodsPage)
- else
- UI.pager:setPage(peripheralsPage)
- end
- Event.pullEvents()
- UI.term:reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement