add menu to exec script

This commit is contained in:
2025-08-04 14:01:20 +02:00
parent 088c5f47a8
commit c008bc89e2
2 changed files with 183 additions and 40 deletions

View File

@ -1,51 +1,100 @@
local function getPeripheralDirection()
-- 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):")
local direction = io.read()
if not direction or direction == "quit" or direction == "exit" then
print("No direction specified, exiting.")
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 checkDirectionIsValid(direction)
if not direction or direction == "quit" or direction == "exit" then
print("No direction specified, exiting.")
return false
local function main()
local direction = getUserDirection()
if not direction then
return
end
return true
end
local function getMethodsPeripheral(direction)
local info = peripheral.getMethods(direction)
return info
end
local function displayMethodsPeripheral(info)
local nbOfMethods = 0
for methodName, methodInfo in pairs(info) do
nbOfMethods = nbOfMethods + 1
if nbOfMethods > 10 then
print("Too many methods, please press a key to continue...")
event, p1 = os.pullEvent("key")
nbOfMethods = 0
term.clear()
end
print(methodName .. ' ' .. methodInfo)
local methods = getPeripheralMethods(direction)
if not methods then
print("Error: No peripheral found in direction '" .. direction .. "'")
return
end
displayMethodsWithPagination(methods)
end
-- main
local direction = getPeripheralDirection()
local isDirectionIsNotEmpty = checkDirectionIsValid(direction)
if not isDirectionIsNotEmpty then
print("No direction specified, exiting.")
return
end
local info = getMethodsPeripheral(direction)
if not info then
print("No info found, exiting.")
return
end
displayMethodsPeripheral(info)
-- Program entry point
main()