Advertisement
wolfe_br

Peripheral Info

Jul 3rd, 2022 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. -- Usage: pastebin run Lqn82CnT side
  2. -- Where "side" is one of the block's sides
  3.  
  4. args = {...}
  5. selected_side = args[1]
  6. sides = peripheral.getNames()
  7.  
  8. -- This function reads a Lua function and returns its arguments
  9. function getFunctionArguments (fn)
  10.   local args = {}
  11.  
  12.   -- Gets data about the function
  13.   local info = debug.getinfo(fn)
  14.  
  15.   -- Processes each of the arguments (might not work for Peripherals though)
  16.   for i = 1, info.nparams, 1 do
  17.     table.insert(args, debug.getlocal(fn, i))
  18.   end
  19.  
  20.   -- Done
  21.   return args
  22. end
  23.  
  24. -- Gets a list of methods pretty-printed and sorted
  25. function getPeripheralMethods (side)
  26.   -- Gets methods
  27.   local methods = peripheral.getMethods(side)
  28.  
  29.   -- Sorts
  30.   table.sort(methods)
  31.  
  32.   -- Pretty-prints
  33.   local results = {}
  34.   for _, method in pairs(methods) do
  35.     -- Gets the method
  36.     local fn = selected_peripheral[method]
  37.  
  38.     -- Gets args list
  39.     local args = getFunctionArguments(fn)
  40.  
  41.     -- Prints it
  42.     table.insert(results, string.format('%s(%s)', method, table.concat(args, ', ')))
  43.   end
  44.  
  45.   -- Done
  46.   return results
  47. end
  48.  
  49. -- Prints a formatted string
  50. function printf (format, ...)
  51.   print(string.format(format, ...))
  52. end
  53.  
  54. -- Lists available peripherals
  55. printf('Available Sides/Peripherals:')
  56.  
  57. -- Side detection
  58. for _, side in pairs(sides) do  
  59.   -- Debug
  60.   printf('-> %s', side)
  61.  
  62.   -- Check for a monitor on that side
  63.   if (not monitor) and 'monitor' == peripheral.getType(side) then
  64.     monitor = peripheral.wrap(side)
  65.   end
  66.  
  67.   -- Check for connection with selected side
  68.   if side == selected_side then
  69.     -- Wrap the peripheral
  70.     selected_peripheral = peripheral.wrap(side)
  71.   end
  72. end
  73.  
  74. -- Checks if a side was provided
  75. if not selected_side then
  76.   printf('No side provided!')
  77.   return 1
  78. elseif not selected_peripheral then
  79.   printf('Invalid peripheral!')
  80.   printf('Side: %s', selected_side)
  81.   return 1
  82. end
  83.  
  84. -- Route stdout to monitor
  85. if monitor then
  86.   term.redirect(monitor)
  87.   monitor.setTextScale(0.5)
  88. end
  89.  
  90. -- Clears output and prepares to write
  91. term.clear()
  92. term.setCursorPos(1, 1)
  93.  
  94. -- Starts getting info
  95. printf('Peripheral API Info')
  96. printf('Side: %s', selected_side)
  97. printf('Type: %s', peripheral.getType(selected_side))
  98.  
  99. -- Gets available methods
  100. print('')
  101. print(table.concat(getPeripheralMethods(selected_side), '\n'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement