Advertisement
Guest User

pi.lua

a guest
Feb 6th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.61 KB | None | 0 0
  1. vos.loadApi('core.api')
  2. vos.loadApi('ui.api')
  3.  
  4. multishell.setTitle(TAB_ID, 'Peripherals')
  5.  
  6. local currentPeripheral
  7. local currentMethod
  8.  
  9. --[[ -- PeripheralsPage  -- ]] --
  10. peripheralsPage = UI.Page({
  11.   grid = UI.ScrollingGrid({
  12.     columns = {
  13.       { 'Type', 'type', 10 },
  14.       { 'Side', 'side', 8 }
  15.     },  
  16.     sortColumn = 'type',
  17.     pageSize = UI.term.height-2,
  18.     width = UI.term.width,
  19.     autospace = true,
  20.     y = 1,
  21.     x = 1
  22.   }),
  23.   statusBar = UI.StatusBar({
  24.     status = 'Select peripheral'
  25.   }),
  26.   accelerators = {
  27.     q = 'quit',
  28.   },
  29. })
  30.  
  31. function peripheralsPage:eventHandler(event)
  32.   if event.type == 'quit' then
  33.     Event.exitPullEvents()
  34.   elseif event.type == 'grid_select' then
  35.     currentPeripheral = self.grid:getSelected()
  36.     methodsPage.grid.index = 1
  37.     methodsPage.grid.scrollOffset = 1
  38.     UI.pager:setPage(methodsPage)
  39.   end
  40.   return UI.Page.eventHandler(self, event)
  41. end
  42.  
  43. function peripheralsPage:updatePeripherals()
  44.   local sides = peripheral.getNames()
  45.   local t = { }
  46.   for _,side in pairs(sides) do
  47.     table.insert(t, {
  48.       type = peripheral.getType(side),
  49.       side = side
  50.     })
  51.   end
  52.  
  53.   self.grid:setTable(t)
  54.   self.grid:adjustWidth()
  55.  
  56.   if UI.pager:getCurrentPage() == peripheralsPage then
  57.     peripheralsPage:draw()
  58.   end
  59. end
  60.  
  61. Event.addHandler('peripheral', function()
  62.   peripheralsPage:updatePeripherals()
  63. end)
  64.  
  65. Event.addHandler('peripheral_detach', function()
  66.   peripheralsPage:updatePeripherals()
  67. end)
  68.  
  69. --[[-- argumentsPage --]]--
  70. function getArguments(method)
  71.  
  72.   local fields = { }
  73.   for key,arg in ipairs(method.args) do
  74.     table.insert(fields, {
  75.       label = arg.name,
  76.       type = string.lower(arg.type),
  77.       required = not arg.optional,
  78.       key = key,
  79.       width = 20,
  80.       limit = 100,
  81.       display = UI.Form.D.entry
  82.     })
  83.   end
  84.  
  85.   table.insert(fields,
  86.       { text = 'Run', event = 'accept', display = UI.Form.D.button,
  87.         y = #method.args + 2, width = 10 })
  88.   table.insert(fields,
  89.       { text = 'Cancel', event = 'cancel', display = UI.Form.D.button,
  90.         x = 35, y = #method.args + 2, width = 10 })
  91.  
  92.   argumentsPage = UI.Page({
  93.     method = method,
  94.     methodName = UI.Text({
  95.       value = method.name,
  96.       width = #method.name,
  97.       y = 2,
  98.       x = 2,
  99.     }),
  100.     form = UI.Form({
  101.       fields = fields,
  102.       labelWidth = 15,
  103.       valueWidth = 25,
  104.       x = 2,
  105.       y = 4,
  106.       width = UI.term.width - 2,
  107.       height = UI.term.height - 5
  108.     }),
  109.     statusBar = UI.StatusBar({
  110.       status = currentPeripheral.type .. ' : ' .. method.name
  111.     }),
  112.   })
  113.  
  114.   function argumentsPage:eventHandler(event)
  115.     if event.type == 'accept' then
  116.       local success, msg = methodsPage:executeMethod(method, unpack(self.form.values))
  117.       if not success then
  118.         self.statusBar:setStatus(msg)
  119.         self.statusBar:draw()
  120.       end
  121.       return true
  122.     elseif event.type == 'cancel' then
  123.       UI.pager:setPreviousPage()
  124.       return true
  125.     end
  126.     return UI.Page.eventHandler(self, event)
  127.   end
  128.  
  129.   UI.pager:setPage(argumentsPage)
  130. end
  131.  
  132. --[[ -- MethodsPage  -- ]] --
  133. methodsPage = UI.Page({
  134.   grid = UI.ScrollingGrid({
  135.     columns = {
  136.       { 'Name', 'name', UI.term.width }
  137.     },  
  138.     sortColumn = 'name',
  139.     pageSize = 6,
  140.     width = UI.term.width,
  141.     sortMethod = function() return false end,
  142.     y = 1,
  143.     x = 1
  144.   }),
  145.   viewportConsole = UI.ViewportWindow({
  146.     y = 8,
  147.     height = UI.term.height-8,
  148.     backgroundColor = colors.brown
  149.   }),
  150.   statusBar = UI.StatusBar({
  151.     status = 'q to return',
  152.     x = 1
  153.   }),
  154.   accelerators = {
  155.     q = 'back',
  156.     backspace = 'back',
  157.     i = 'info',
  158.   },
  159. })
  160.  
  161. function methodsPage:enable()
  162.  
  163.   local p = peripheral.wrap(currentPeripheral.side)
  164.   if not p.getAdvancedMethodsData then
  165.     self.grid.t = { }
  166.     for name,f in pairs(p) do
  167.       table.insert(self.grid.t, {
  168.         name = name,
  169.         noext = true,
  170.       })
  171.     end
  172.   else
  173.     self.grid.t = p.getAdvancedMethodsData()
  174.     for name,f in pairs(self.grid.t) do
  175.       f.name = name
  176.     end
  177.   end
  178.   self.grid.selected = nil
  179.  
  180.   self.statusBar:setStatus(currentPeripheral.type)
  181. end
  182.  
  183. function methodsPage.grid:setSelected(selected)
  184.   self.selected = selected
  185. --  drawMethodInfo(methodsPage.viewportConsole, selected)
  186. end
  187.  
  188. function methodsPage.viewportConsole:draw()
  189.   if methodsPage.grid:getSelected() then
  190. --    drawMethodInfo(self, methodsPage.grid:getSelected())
  191.   end
  192. end
  193.  
  194. function methodsPage:executeMethod(method, ...)
  195.   local args = { ... }
  196.   local result
  197.   local success, err = pcall(function()
  198.     result = peripheral.call(currentPeripheral.side, method.name, unpack(args))
  199.   end)
  200.   if not success then
  201.     return false, err or 'Error calling method'
  202.   elseif result then
  203.     resultsPage:setResult(result)
  204.     resultsPage.statusBar:setStatus(currentPeripheral.type .. ' : ' .. method.name)
  205.     UI.pager:setPage(resultsPage)
  206.     return true
  207.   end
  208.   return false, 'No result'
  209. end
  210.  
  211. function methodsPage:eventHandler(event)
  212.   if event.type == 'back' then
  213.     UI.pager:setPage(peripheralsPage)
  214.     return true
  215.   elseif event.type == 'grid_select' then
  216.     local method = self.grid:getSelected()
  217.     if not method.args or #method.args == 0 then
  218.       local success, msg = self:executeMethod(method)
  219.       if not success then
  220.         self.statusBar:setStatus(msg)
  221.         self.statusBar:draw()
  222.       end
  223.     else
  224.       getArguments(method)
  225.     end
  226.     return true
  227.   elseif event.type == 'info' then
  228.     if self.extendedInfo then
  229.       UI.pager:setPage(methodDetailsPage)
  230.     end
  231.     return true
  232.   elseif event.type == 'grid_focus_row' then
  233.     drawMethodInfo(self.viewportConsole, self.grid:getSelected())
  234.   end
  235.   return UI.Page.eventHandler(self, event)
  236. end
  237.  
  238. --[[-- methodDetailsPage --]]--
  239. methodDetailsPage = UI.Page({
  240.   viewportConsole = UI.ViewportWindow({
  241.     height = UI.term.height-1,
  242.     backgroundColor = colors.brown,
  243.     textColor = colors.white
  244.   }),
  245.   statusBar = UI.StatusBar({
  246.     status = 'enter to return'
  247.   }),
  248.   accelerators = {
  249.     q = 'back',
  250.     backspace = 'back',
  251.     enter = 'back',
  252.   },
  253. })
  254.  
  255. function methodDetailsPage:enable()
  256.   self.viewportConsole.offset = 0
  257.   self.viewportConsole.vpHeight = UI.term.height-1
  258. end
  259.  
  260. function methodDetailsPage.viewportConsole:draw()
  261.   drawMethodInfo(self, methodsPage.grid:getSelected())
  262. end
  263.  
  264. function methodDetailsPage:eventHandler(event)
  265.   if event.type == 'back' then
  266.     UI.pager:setPreviousPage()
  267.     return true
  268.   end
  269.   UI.Page.eventHandler(self, event)
  270. end
  271.  
  272. --[[ -- resultsPage  -- ]] --
  273. resultsPage = UI.Page({
  274.   grid = UI.ScrollingGrid({
  275.     columns = {
  276.       { 'Name', 'name', 10 },
  277.       { 'Value', 'value', 10 }
  278.     },  
  279.     sortColumn = 'name',
  280.     pageSize = UI.term.height-3,
  281.     width = UI.term.width,
  282.     autospace = true,
  283.     y = 1
  284.   }),
  285.   statusBar = UI.StatusBar({
  286.     status = 'q to return'
  287.   }),
  288.   accelerators = {
  289.     backspace = 'back',
  290.     q = 'return',
  291.   },
  292. })
  293.  
  294. function resultsPage:setResult(result)
  295.   local t = { }
  296.   if type(result) == 'table' then
  297.     for k,v in pairs(result) do
  298.       local entry = {
  299.         name = k,
  300.         value = tostring(v)
  301.       }
  302.       if type(v) == 'table' then
  303.         if Util.size(v) == 0 then
  304.           entry.value = 'table: (empty)'
  305.         else
  306.           entry.value = 'table'
  307.           entry.table = v
  308.         end
  309.       end
  310.       table.insert(t, entry)
  311.     end
  312.   else
  313.     table.insert(t, {
  314.       name = 'result',
  315.       value = tostring(result)
  316.     })
  317.   end
  318.   self.grid.t = t
  319.   self.grid:adjustWidth()
  320. end
  321.  
  322. function resultsPage:enable()
  323.   self.currentResult = self.grid.t
  324.   self.resultPath = { }
  325. end
  326.  
  327. function resultsPage:eventHandler(event)
  328.   if event.type == 'grid_select' then
  329.     local nameValue = self.grid:getSelected()
  330.     if nameValue.table then
  331.       if Util.size(nameValue.table) > 0 then
  332.         table.insert(self.resultPath, self.grid.t)
  333.         self:setResult(nameValue.table)
  334.         self:draw()
  335.       else
  336.         self.statusBar:timedStatus('Table is empty', 3)
  337.       end
  338.     end
  339.     return true
  340.   elseif event.type == 'back' then
  341.     if #self.resultPath > 0 then
  342.       self.grid.t = table.remove(self.resultPath)
  343.       self.grid:adjustWidth()
  344.       self.grid:draw()
  345.     else
  346.       UI.pager:setPreviousPage()
  347.     end
  348.     return true
  349.   elseif event.type == 'return' then
  350.     UI.pager:setPreviousPage()
  351.     return true
  352.   end
  353.   UI.Page.eventHandler(self, event)
  354. end
  355.  
  356. --[[ -- Common logic  -- ]] --
  357. function drawMethodInfo(c, method)
  358.  
  359.   c:reset()
  360.   c:setCursorPos(1, 2)
  361.  
  362.   if method.noext then
  363.     c:print('No extended Information')
  364.     return 2
  365.   end
  366.  
  367.   if method.description then
  368.     c:print(method.description)
  369.     c:print('')
  370.   end
  371.  
  372.   local str = method.name .. '('
  373.   for k,arg in ipairs(method.args) do
  374.     str = str .. arg.name
  375.     if k < #method.args then
  376.       str = str .. ', '
  377.     end
  378.   end
  379.   c:print(str .. ')')
  380.  
  381. --  local sb = UI.StringBuffer(0)
  382. --  if #method.returnTypes == 9000 then
  383. --    sb:clear()
  384. --    sb:append('Returns: ')
  385. --    for k,ret in ipairs(method.returnTypes) dofile(
  386. --      sb:append(ret)
  387. --      if k < #method.returnTypes then
  388. --        sb:append(', ')
  389. --      end
  390. --    end
  391. --    c:print(sb:get())
  392. --  end
  393.  
  394.   if #method.args > 0 then
  395.     for _,arg in ipairs(method.args) do
  396.       c:print('')
  397.       c:print(arg.name .. ': ' .. arg.description)
  398.       c:print('')
  399.       c:print('optional nullable type    vararg')
  400.       c:print('-------- -------- ----    ------')
  401.       sb:clear()
  402.       sb:insert(tostring(arg.optional), 0)
  403.       sb:insert(tostring(arg.nullable), 9)
  404.       sb:insert(arg.type, 18)
  405.       sb:insert(tostring(arg.vararg), 26)
  406.       c:print(sb:get())
  407.     end
  408.   end
  409.  
  410.   term.setBackgroundColor(colors.black)
  411.   return y
  412. end
  413.  
  414. local args = { ... }
  415.  
  416. if #args > 0 then
  417.   local side = args[1]
  418.   local sides = peripheral.getNames()
  419.   local t = { }
  420.   for _,s in pairs(sides) do
  421.     if side == s then
  422.       currentPeripheral = {
  423.         type = peripheral.getType(side),
  424.         side = side
  425.       }
  426.       break
  427.     end
  428.   end
  429. end
  430.  
  431. --[[ -- Startup logic  -- ]] --
  432. local args = { ... }
  433.  
  434. if #args > 0 then
  435.   local side = args[1]
  436.   local sides = peripheral.getNames()
  437.   local t = { }
  438.   for _,s in pairs(sides) do
  439.     if side == s then
  440.       currentPeripheral = {
  441.         type = peripheral.getType(side),
  442.         side = side
  443.       }
  444.       break
  445.     end
  446.   end
  447. end
  448.  
  449. Logger.disable()
  450.  
  451. peripheralsPage:updatePeripherals()
  452. if currentPeripheral then
  453.   UI.pager:setPage(methodsPage)
  454. else
  455.   UI.pager:setPage(peripheralsPage)
  456. end
  457.  
  458. Event.pullEvents()
  459. UI.term:reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement