Files
scriptLua/getMethodsPeripheral.lua
2025-08-04 14:01:20 +02:00

101 lines
2.2 KiB
Lua

-- Configuration
local PAGINATION_SIZE = 10
local VALID_DIRECTIONS = { "left", "right", "top", "bottom", "front", "back" }
-- Input validation
local function isValidDirection(direction)
if not direction then return false end
local lowerDirection = string.lower(direction)
for _, validDir in ipairs(VALID_DIRECTIONS) do
if lowerDirection == validDir then
return true
end
end
return false
end
local function isExitCommand(input)
return input == "quit" or input == "exit"
end
-- User interaction
local function promptForDirection()
print("Enter the direction of the peripheral (left, right, top, bottom, front, back):")
print("Type 'quit' or 'exit' to cancel.")
return io.read()
end
local function waitForKeyPress()
print("Press any key to continue...")
os.pullEvent("key")
term.clear()
end
-- Peripheral operations
local function getPeripheralMethods(direction)
return peripheral.getMethods(direction)
end
-- Display functions
local function displayMethodPage(methods, startIndex, endIndex)
for i = startIndex, endIndex do
local methodName = methods[i]
print(string.format("%d. %s", i, methodName))
end
end
local function displayMethodsWithPagination(methods)
if not methods or #methods == 0 then
print("No methods found for this peripheral.")
return
end
print(string.format("Found %d methods:", #methods))
print(string.rep("-", 30))
for i = 1, #methods, PAGINATION_SIZE do
local endIndex = math.min(i + PAGINATION_SIZE - 1, #methods)
displayMethodPage(methods, i, endIndex)
if endIndex < #methods then
waitForKeyPress()
end
end
end
-- Main program logic
local function getUserDirection()
local direction = promptForDirection()
if not direction or isExitCommand(direction) then
print("Operation cancelled.")
return nil
end
if not isValidDirection(direction) then
print("Error: Invalid direction. Please use: " .. table.concat(VALID_DIRECTIONS, ", "))
return nil
end
return direction
end
local function main()
local direction = getUserDirection()
if not direction then
return
end
local methods = getPeripheralMethods(direction)
if not methods then
print("Error: No peripheral found in direction '" .. direction .. "'")
return
end
displayMethodsWithPagination(methods)
end
-- Program entry point
main()