Advertisement
Loneranger419

makeLog

Feb 1st, 2025 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 KB | Source Code | 0 0
  1. local logFile = "me_log.txt" -- Log file for storing output
  2.  
  3. local device = peripheral.find("meBridge")
  4.  
  5. if not device then
  6.     print("No peripheral found on!")
  7.     return
  8. end
  9.  
  10. local side = peripheral.getName(device)
  11.  
  12. local methods = peripheral.getMethods(side)
  13.  
  14. if not methods or #methods == 0 then
  15.     print("No functions found for this peripheral.")
  16.     return
  17. end
  18.  
  19. local pageSize = 10 -- Number of methods per page
  20. local scrollOffset = 0
  21. local selectedIndex = nil
  22.  
  23. -- Number of lines before the list starts (title & spacing)
  24. local TITLE_OFFSET = 2  
  25.  
  26. -- Function to display methods
  27. local function displayMethods()
  28.     term.clear()
  29.     term.setCursorPos(1,1)
  30.     print("Select a function (Scroll with Mouse, Click to Select):\n")
  31.    
  32.     for i = 1, pageSize do
  33.         local index = i + scrollOffset
  34.         if index > #methods then break end
  35.        
  36.         if selectedIndex == index then
  37.             term.setTextColor(colors.yellow)
  38.         end
  39.  
  40.         print(index .. ". " .. methods[index])
  41.  
  42.         term.setTextColor(colors.white) -- Reset color
  43.     end
  44.  
  45.     print("\nScroll Up/Down to navigate. Click to select.")
  46. end
  47.  
  48. displayMethods()
  49.  
  50. while true do
  51.     local event, button, x, y = os.pullEvent()
  52.    
  53.     if event == "mouse_scroll" then
  54.         if button == 1 and scrollOffset + pageSize < #methods then
  55.             scrollOffset = scrollOffset + 1
  56.         elseif button == -1 and scrollOffset > 0 then
  57.             scrollOffset = scrollOffset - 1
  58.         end
  59.         displayMethods()
  60.  
  61.     elseif event == "mouse_click" then
  62.         local methodIndex = y - TITLE_OFFSET -- 🔹 Correctly adjust for title lines
  63.         methodIndex = methodIndex + scrollOffset -- 🔹 Adjust for scrolling
  64.  
  65.         if methodIndex >= 1 and methodIndex <= #methods then
  66.             selectedIndex = methodIndex
  67.             break
  68.         end
  69.     end
  70. end
  71.  
  72. local selectedMethod = methods[selectedIndex]
  73. term.clear()
  74. term.setCursorPos(1,1)
  75. print("Running: " .. selectedMethod .. "()\n")
  76.  
  77. -- Attempt to run the selected function
  78. local success, result = pcall(device[selectedMethod])
  79.  
  80. -- Process result
  81. local outputLines = {}
  82.  
  83. if success then
  84.     if type(result) == "table" then
  85.         local serialized = textutils.serialize(result)
  86.         for line in serialized:gmatch("[^\r\n]+") do
  87.             table.insert(outputLines, line)
  88.         end
  89.     else
  90.         table.insert(outputLines, tostring(result))
  91.     end
  92. else
  93.     table.insert(outputLines, "Error: Function requires arguments or failed to execute.")
  94. end
  95.  
  96. -- Save outputLines to log file
  97. local file = fs.open(logFile, "w") -- Open log file for writing
  98. for _, line in ipairs(outputLines) do
  99.     file.writeLine(line)
  100. end
  101. file.close()
  102. print("\nLog saved to " .. logFile)
  103.  
  104. -- Upload log file to Pastebin
  105. print("\nUploading to Pastebin...")
  106. local success = shell.run("pastebin", "put", logFile)
  107.  
  108. if success then
  109.     print("\nPastebin upload successful!")
  110. else
  111.     print("\nPastebin upload failed. Ensure you have an internet connection.")
  112. end
  113.  
  114. -- Exit the script immediately
  115. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement