Advertisement
bobmarley12345

Peripheral explorer LUA/CC

Jan 24th, 2023 (edited)
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.11 KB | None | 0 0
  1. function printTextColoured(str, fg_col)
  2.     term.setTextColor(fg_col)
  3.     print(str)
  4.     term.setTextColor(colours.white)
  5. end
  6.  
  7. function writeTextColoured(str, fg_col)
  8.     term.setTextColor(fg_col)
  9.     term.write(str)
  10.     term.setTextColor(colours.white)
  11. end
  12.  
  13. function concat_str(str, join, text)
  14.     if (#str == 0) then
  15.         return text
  16.     else
  17.         return str .. join .. text
  18.     end
  19. end
  20.  
  21. function concatTable(table, joinText, selector)
  22.     local buffer = ""
  23.     local len = #table
  24.     local index = 1
  25.     for i, k in pairs(table) do
  26.         local txt = selector(i, k)
  27.         if (index == len) then
  28.             if (txt == nil) then
  29.                 return buffer
  30.             elseif (#buffer == 0) then
  31.                 return txt
  32.             else
  33.                 return buffer .. joinText .. txt
  34.             end
  35.         elseif (txt ~= nil) then
  36.             buffer = concat_str(buffer, joinText, txt)
  37.         end
  38.  
  39.         index = index + 1
  40.     end
  41.  
  42.     if (buffer == nil) then
  43.         return ""
  44.     end
  45.  
  46.     return buffer
  47. end
  48.  
  49. function waitForUserInput()
  50.     getUserInput("Press enter to continue")
  51. end
  52.  
  53. function getUserInput(displayString)
  54.     printTextColoured(displayString, colours.yellow)
  55.     return read()
  56. end
  57.  
  58. function printAdvMethodInfo(method_data)
  59.     local retTypeTable = method_data["returnTypes"]
  60.     local argsTable = method_data["args"]
  61.     local descStr = method_data["description"]
  62.     if (descStr ~= nil) then
  63.         printTextColoured(descStr, colours.cyan)
  64.     end
  65.  
  66.     if (argsTable ~= nil and #argsTable > 0) then
  67.         printTextColoured("Args: ", colours.lightGrey)
  68.         for index, inner_data in pairs(argsTable) do
  69.             local isVarArg = inner_data["vararg"] == true
  70.             local isNullable = inner_data["nullable"] == true
  71.             local typeName = inner_data["type"]
  72.             local isOptional = inner_data["optional"] == true
  73.             local argName = inner_data["name"]
  74.  
  75.             local flagsBuf = ""
  76.             if (isVarArg) then
  77.                 flagsBuf = concat_str(flagsBuf, ", ", "varargs")
  78.             end
  79.             if (isNullable) then
  80.                 flagsBuf = concat_str(flagsBuf, ", ", "nullable")
  81.             end
  82.             if (isOptional) then
  83.                 flagsBuf = concat_str(flagsBuf, ", ", "optional")
  84.             end
  85.  
  86.             writeTextColoured("  " .. index .. ": ", colours.lightGrey)
  87.             writeTextColoured(typeName, colours.green)
  88.             printTextColoured(" " .. argName, colours.lightBlue)
  89.             -- print("  " .. index .. ": " .. typeName .. " " .. argName)
  90.             if (#flagsBuf > 0) then
  91.                 printTextColoured("     " .. flagsBuf, colours.grey)
  92.             end
  93.  
  94.         end
  95.     end
  96.  
  97.     if (retTypeTable ~= nil and #retTypeTable > 0) then
  98.         local typeStr = concatTable(retTypeTable, ", ", function(k, v)
  99.             if (v == nil or #v == 0) then
  100.                 return nil
  101.             else
  102.                 return v
  103.             end
  104.         end)
  105.  
  106.         writeTextColoured("Return type(s): ", colours.lightGrey)
  107.         printTextColoured(typeStr, colours.green)
  108.     end
  109. end
  110.  
  111. function printAdvancedMethodData(advancedDataTable, specificMethod)
  112.     if (advancedDataTable == nil or #advancedDataTable == 0) then
  113.         return
  114.     end
  115.  
  116.     for md_name, md_data in pairs(advancedDataTable) do
  117.         if (specificMethod == nil or specificMethod == md_name) then
  118.             print(md_name)
  119.             printAdvMethodInfo(md_data)
  120.         end
  121.     end
  122. end
  123.  
  124. function printPeripheralInfo(side)
  125.     local w, h = term.getSize()
  126.  
  127.     local buffer = ""
  128.     local lastPrint = ""
  129.     local methods = peripheral.getMethods(side)
  130.     print(#methods .. " methods available")
  131.     for i, n in pairs(methods) do
  132.         if (#buffer + #n > w) then
  133.             lastPrint = buffer
  134.             printTextColoured(buffer, colours.lightGrey)
  135.             buffer = n
  136.         else
  137.             buffer = concat_str(buffer, ", ", n)
  138.         end
  139.     end
  140.  
  141.     if (lastPrint ~= buffer) then
  142.         printTextColoured(buffer, colours.lightGrey)
  143.     end
  144.  
  145.     local advMd = peripheral.wrap(side)["getAdvancedMethodsData"]
  146.     local advancedMdData = nil
  147.     if (advMd ~= nil) then
  148.         advancedMdData = advMd()
  149.     end
  150.  
  151.     if (advancedMdData == nil) then
  152.         printTextColoured("There is no advanced method data available",  colours.yellow)
  153.         waitForUserInput()
  154.         return
  155.     end
  156.  
  157.  
  158.     local input = getUserInput("Press enter to continue, or enter a method name:")
  159.     if (input == "") then
  160.         return
  161.     end
  162.  
  163.     local methodData = advancedMdData[input]
  164.     if (methodData == nil) then
  165.         printTextColoured("No advanced data for method: " .. input, colours.red)
  166.     else
  167.         printAdvMethodInfo(methodData)
  168.     end
  169.  
  170.     waitForUserInput()
  171. end
  172.  
  173. function clearScreen()
  174.     term.clear()
  175.     term.setCursorPos(1,1)
  176. end
  177.  
  178. function goToMainPage()
  179.     clearScreen()
  180.     print("Enter 'all' to list all sides, or a specific side")
  181.     print("to print all available methods on that side")
  182.     print("Available sides: " .. concatTable(peripheral.getNames(), ", ", function (k, v) return v end))
  183.     print("---------------------------------------------------")
  184. end
  185.  
  186. goToMainPage()
  187. while true do
  188.     local input = read()
  189.     if (input == "all") then
  190.         clearScreen()
  191.         for i, v in pairs(peripheral.getNames()) do
  192.             print(v .. " -> " .. peripheral.getType(v))
  193.         end
  194.         waitForUserInput()
  195.         goToMainPage()
  196.     elseif (input == "left" or input == "right" or input == "front" or input == "back" or input == "top" or input == "bottom") then
  197.         if (peripheral.isPresent(input)) then
  198.             clearScreen()
  199.             printPeripheralInfo(input)
  200.             goToMainPage()
  201.         else
  202.             printTextColoured("No peripherals on side: " .. input, colours.red)
  203.         end
  204.     elseif (input == "exit") then
  205.         return
  206.     elseif (input == "reboot") then
  207.         os.reboot()
  208.         return
  209.     else
  210.         printTextColoured("Invalid command or side: " .. input, colours.red)
  211.     end
  212. end
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement