Advertisement
Guest User

apibrowser

a guest
Jun 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.36 KB | None | 0 0
  1. local args = {...}
  2.  
  3. if #args == 0 then
  4.   error("Usage: apibrowser <peripheral>")
  5. end
  6.  
  7. local pName = args[1]
  8.  
  9. local p = peripheral.wrap(pName)
  10. local sizeX, sizeY = term.getSize()
  11.  
  12. local sel = 1
  13. local methods = peripheral.getMethods(pName)
  14.  
  15. function draw()
  16.   term.clear()
  17.  
  18.   term.setCursorPos(1, 1)
  19.   term.setBackgroundColor(colors.white)
  20.   term.setTextColor(colors.black)
  21.   term.clearLine()
  22.   term.write(pName .. " (" .. #peripheral.getMethods(pName) .. ")")
  23.   term.setBackgroundColor(colors.black)
  24.   term.setTextColor(colors.white)
  25.  
  26.   local y = 2
  27.   while y < sizeY - 8 do
  28.     local i = y - 3 + sel
  29.  
  30.     term.setCursorPos(1, y)
  31.  
  32.     if y == 3 then
  33.       term.write("> ")
  34.     else
  35.       term.write("  ")
  36.     end
  37.  
  38.     if i >= 1 and i <= #methods then
  39.       term.write(methods[i])
  40.     end
  41.  
  42.     y = y + 1
  43.   end
  44.  
  45.   if p.doc then
  46.     term.setCursorPos(1, sizeY - 8)
  47.     print(p.doc(methods[sel]))
  48.   end
  49. end
  50.  
  51. draw()
  52.  
  53. while true do
  54.   local event = {os.pullEvent()}
  55.  
  56.   if event[1] == "key" then
  57.     if event[2] == 208 then
  58.       if sel < #methods then
  59.         sel = sel + 1
  60.       end
  61.     elseif event[2] == 200 then
  62.       if sel > 1 then
  63.         sel = sel - 1
  64.       end
  65.     end
  66.   elseif event[1] == "key_up" then
  67.     if event[2] == 16 then
  68.       term.clear()
  69.       term.setCursorPos(1, 1)
  70.       error()
  71.     end
  72.   end
  73.  
  74.   draw()
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement