apemanzilla

opdoc_source

Dec 2nd, 2015
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require "UIButton"
  2. require "UIContainer"
  3. require "UITabs"
  4. require "UIText"
  5.  
  6. local function quit()
  7.     application:stop() term.clear() term.setCursorPos(1,1)
  8. end
  9.  
  10. application.view:createShortcut("terminate","ctrl-t",quit)
  11. local close = application.view:addChild(UIButton(application.view.width-1,0,1,1,"X"))
  12. close.colour = colors.red
  13. close.textColour = colors.black
  14. close.onClick = quit
  15.  
  16. local peripherals = peripheral.getNames()
  17.  
  18. for i,v in ipairs(peripherals) do
  19.     local p = peripheral.wrap(v)
  20.     if not p.listMethods then
  21.         table.remove(peripherals,i)
  22.     end
  23. end
  24.  
  25. local tabs = application.view:addChild(UITabs(1,1,application.view.width-2,peripherals))
  26.  
  27. local m_cont = application.view:addChild(UIContainer(1,3,application.view.width-2,application.view.height-4))
  28.  
  29. local text_cont = UIContainer(1,3,application.view.width-2,application.view.height-4)
  30. local back_btn = text_cont:addChild(UIButton(0,0,text_cont.width-1,1,"Back"))
  31. back_btn.textColour = colors.red
  32.  
  33. local m_texts = {}
  34.  
  35. local function display_method(pname,mname)
  36.     m_cont:remove()
  37.     text_cont:remove()
  38.     application.view:addChild(text_cont)
  39.     for _,text in ipairs(m_texts) do
  40.         text:remove()
  41.     end
  42.     local p = peripheral.wrap(pname)
  43.     local m = p.getAdvancedMethodsData(mname)
  44.     local y = 1
  45.     local header = "@tb" .. mname .. "("
  46.     for i,v in ipairs(m.args) do
  47.         if m.args[i+1] then
  48.             header = header .. ((v.optional and "@t9[") or "@t9") .. v.name .. ((v.optional and "]@t7, ") or "@t7, ")
  49.         else
  50.             header = header .. ((v.optional and "@t9[") or "@t9") .. v.name .. ((v.optional and "]") or "")
  51.         end
  52.     end
  53.     header = header .. "@tb)"
  54.     local header_text = text_cont:addChild(UIText(0,y,text_cont.width-1,1,header))
  55.     header_text.internalWidth = header_text.width - 1
  56.     header_text.height = header_text:getContentHeight()
  57.     table.insert(m_texts,header_text)
  58.     y = y + header_text.height + 1
  59.  
  60.     local desc = ((m.description ~= "" and m.description) or "@t8<No description found>")
  61.     local desc_text = text_cont:addChild(UIText(1,y,text_cont.width-2,1,desc))
  62.     desc_text.internalWidth = desc_text.width - 1
  63.     desc_text.height = desc_text:getContentHeight()
  64.     table.insert(m_texts,desc_text)
  65.     y = y + desc_text.height + 1
  66.  
  67.     if #m.args > 0 then
  68.         local args_text = text_cont:addChild(UIText(0,y,text_cont.width-1,1,"Arguments"))
  69.         args_text.internalWidth = args_text.width - 1
  70.         args_text.height = args_text:getContentHeight()
  71.         table.insert(m_texts,args_text)
  72.         y = y + args_text.height + 1
  73.  
  74.         for i,v in ipairs(m.args) do
  75.             local arg = "@t9"..v.name.."@t7: " .. ((v.description ~= "" and v.description) or "@t8<No description found>")
  76.             local arg_text = text_cont:addChild(UIText(1,y,text_cont.width-2,1,arg))
  77.             arg_text.internalWidth = arg_text.width - 1
  78.             arg_text.height = arg_text:getContentHeight()
  79.             table.insert(m_texts,arg_text)
  80.             y = y + arg_text.height
  81.         end
  82.         y = y + 1
  83.     end
  84.     if m.returnTypes ~= "()" then
  85.         local ret = "Returns @t9" .. m.returnTypes
  86.         local ret_text = text_cont:addChild(UIText(0,y,text_cont.width-1,1,ret))
  87.         ret_text.internalWidth = ret_text.width - 1
  88.         ret_text.height = ret_text:getContentHeight()
  89.         table.insert(m_texts,ret_text)
  90.         y = y + ret_text.height
  91.     end
  92.     text_cont:setVerticalOffset(0)
  93. end
  94.  
  95. local m_btns = {}
  96.  
  97. local function show_method_buttons(pname)
  98.     m_cont:remove()
  99.     text_cont:remove()
  100.     application.view:addChild(m_cont)
  101.     local p = peripheral.wrap(pname)
  102.     local m = (p.getAdvancedMethodsData and p.getAdvancedMethodsData()) or {}
  103.     -- remove old children
  104.     for _,btn in ipairs(m_btns) do
  105.         btn:remove()
  106.     end
  107.     m_btns = {}
  108.     -- add new buttons
  109.     local ypos = 0
  110.     for name,data in pairs(m) do
  111.         local b = m_cont:addChild(UIButton(0,ypos,m_cont.width-1,1,name))
  112.         function b:onClick()
  113.             display_method(pname,name)
  114.         end
  115.         table.insert(m_btns,b)
  116.         ypos = ypos + 1
  117.     end
  118.     m_cont:setVerticalOffset(0)
  119. end
  120.  
  121. function back_btn:onClick()
  122.     show_method_buttons(peripherals[tabs.selected])
  123. end
  124.  
  125. function tabs:onSelect()
  126.     show_method_buttons(peripherals[self.selected])
  127. end
RAW Paste Data