bobmarley12345

listmethods

May 25th, 2021 (edited)
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. TermWidth, TermHeight = term.getSize()
  2. CurrentCursorY = 0
  3.  
  4. FlipColourNext = false
  5.  
  6. function flipColour()
  7.     if (FlipColourNext) then
  8.         term.setTextColor(colours.lightGrey)
  9.         FlipColourNext = false
  10.     else
  11.         term.setTextColor(colours.grey)
  12.         FlipColourNext = true
  13.     end
  14. end
  15.  
  16. function doesTextFit(text)
  17.     local x, y = term.getCursorPos()
  18.     local len = string.len(text)
  19.     local endPos = x + len + 3
  20.     if (endPos > TermWidth) then
  21.         return false
  22.     end
  23.  
  24.     return true
  25. end
  26.  
  27. function printFormat(text)
  28.     local x, y = term.getCursorPos()
  29.     if (doesTextFit(text) == false) then
  30.         term.setCursorPos(1, y + 1)
  31.     end
  32.  
  33.     flipColour()
  34.     term.write(text)
  35. end
  36.  
  37. function printMethods(apiName)
  38.     term.clear()
  39.     term.setCursorPos(1, 1)
  40.  
  41.     print("API Name: " .. apiName)
  42.  
  43.     local methods = peripheral.getMethods(apiName)
  44.  
  45.     for i,v in ipairs(methods) do
  46.         printFormat(v)
  47.         term.setTextColour(colours.green)
  48.         term.write(", ")
  49.     end
  50. end
  51.  
  52. function main()
  53.     term.setCursorPos(1, 1)
  54.     CurrentCursorY = 1
  55.     term.clear()
  56.  
  57.     local periList = peripheral.getNames()
  58.     for i = 1, #periList do
  59.         print(periList[i] .. ": \"" .. peripheral.getType(periList[i]) .. "\"")
  60.     end
  61.  
  62.     local w, h = term.getSize()
  63.     term.setCursorPos(1, h - 3)
  64.     print("Enter a side (or peripheral name) to list the methods of")
  65.  
  66.     term.setCursorPos(1, h)
  67.     term.write("API> ")
  68.     local apiName = read()
  69.     if (peripheral.wrap(apiName) == nil) then
  70.         term.clear()
  71.         term.setCursorPos(1, 1)
  72.         print("That API doesn't exist!")
  73.         return
  74.     end
  75.  
  76.     printMethods(apiName)
  77. end
  78.  
  79. main()
Advertisement
Add Comment
Please, Sign In to add comment