add menu to exec script
This commit is contained in:
@ -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):")
|
print("Enter the direction of the peripheral (left, right, top, bottom, front, back):")
|
||||||
local direction = io.read()
|
print("Type 'quit' or 'exit' to cancel.")
|
||||||
if not direction or direction == "quit" or direction == "exit" then
|
return io.read()
|
||||||
print("No direction specified, exiting.")
|
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
|
return
|
||||||
end
|
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
|
return direction
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkDirectionIsValid(direction)
|
local function main()
|
||||||
if not direction or direction == "quit" or direction == "exit" then
|
local direction = getUserDirection()
|
||||||
print("No direction specified, exiting.")
|
if not direction then
|
||||||
return false
|
return
|
||||||
end
|
end
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getMethodsPeripheral(direction)
|
local methods = getPeripheralMethods(direction)
|
||||||
local info = peripheral.getMethods(direction)
|
if not methods then
|
||||||
return info
|
print("Error: No peripheral found in direction '" .. direction .. "'")
|
||||||
end
|
return
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
displayMethodsWithPagination(methods)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- main
|
-- Program entry point
|
||||||
local direction = getPeripheralDirection()
|
main()
|
||||||
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)
|
|
||||||
|
|||||||
94
menu.lua
Normal file
94
menu.lua
Normal 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()
|
||||||
Reference in New Issue
Block a user