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
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)
end
end
-- main
local direction = getPeripheralDirection()
local isDirectionIsNotEmpty = checkDirectionIsValid(direction)
if not isDirectionIsNotEmpty then
print("No direction specified, exiting.")
local function main()
local direction = getUserDirection()
if not direction then
return
end
local info = getMethodsPeripheral(direction)
if not info then
print("No info found, exiting.")
end
local methods = getPeripheralMethods(direction)
if not methods then
print("Error: No peripheral found in direction '" .. direction .. "'")
return
end
displayMethodsWithPagination(methods)
end
displayMethodsPeripheral(info)
-- Program entry point
main()

94
menu.lua Normal file
View File

@ -0,0 +1,94 @@
MainMenu = {}
local function getSizeOfScreen()
local x, y = term.getSize()
return x, y
end
local function initMenu()
-- do a menu for computer craft
local menuItems = {
{ name = "Get Method", action = function() shell.run("getMethodsPeripheral") end },
{ name = "Settings", action = function() print("Opening settings...") end },
{ name = "Exit", action = function() print("Exiting...") end }
}
-- put the item in MainMenu
for _, item in ipairs(menuItems) do
table.insert(MainMenu, item)
end
end
local function displayMenu()
local selected = 1
local function drawTitle()
local x, y = getSizeOfScreen()
local title = " Main Menu "
term.setTextColor(colors.yellow)
local decorator = '='
term.write(string.rep(decorator, math.floor((x - #title) / 2)))
term.setCursorPos(math.floor((x - #title) / 2) + 1, 1)
print(title)
term.setCursorPos(math.floor((x + #title) / 2) + 1, 1)
term.write(string.rep(decorator, math.ceil((x - #title) / 2)))
term.setTextColor(colors.white)
term.write(string.rep(decorator, x))
end
local function drawMenuItems(selected)
for i, item in ipairs(MainMenu) do
term.setCursorPos(1, i + 2)
if i == selected then
term.setTextColor(colors.yellow)
else
term.setTextColor(colors.white)
end
-- put the item in the center and add [ ] before item
local itemText = string.format("[%s] %s", i == selected and ">" or " ", item.name)
local x, _ = getSizeOfScreen()
local padding = math.max(0, (x - #itemText) / 2)
term.write(string.rep(" ", padding) .. itemText)
term.setCursorPos(1, i + 2)
term.write(string.rep(" ", x - padding - #itemText))
term.setCursorPos(1, i + 2)
term.setTextColor(colors.white)
end
end
local function handleKey(selected)
local event, key = os.pullEvent("key")
if key == keys.up then
selected = selected - 1
if selected < 1 then selected = #MainMenu end
elseif key == keys.down then
selected = selected + 1
if selected > #MainMenu then selected = 1 end
elseif key == keys.enter then
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1, 1)
MainMenu[selected].action()
return selected, true
end
return selected, false
end
while true do
term.clear()
term.setCursorPos(1, 1)
drawTitle()
term.setTextColor(colors.white)
term.setCursorPos(1, 3)
drawMenuItems(selected)
selected, done = handleKey(selected)
if done then break end
end
term.setTextColor(colors.white)
end
function Main()
initMenu()
displayMenu()
end
Main()