Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Neobox OS Alpha Menu
- -- Simple operating system menu with navigation and file manager
- -- Menu configuration
- local menuItems = {
- {name = "Calculator", description = "Simple calculator", type = "calculator"},
- {name = "Programs", description = "Program List", program = "programs"},
- {name = "File Manager", description = "Browse Files", type = "filemanager"},
- {name = "Shell", description = "Command Line", type = "shell"},
- {name = "Reboot", description = "Restart", type = "reboot"},
- {name = "Shutdown", description = "Power Off", type = "shutdown"}
- }
- -- State variables
- local selectedIndex = 1
- local running = true
- -- Display functions
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function drawHeader()
- local w, h = term.getSize()
- -- Header
- term.setTextColor(colors.blue)
- local title = "| Neobox OS Alpha |"
- local titleX = math.floor((w - #title) / 2) + 1
- term.setCursorPos(titleX, 2)
- term.write(title)
- -- Subtitle
- term.setTextColor(colors.lightGray)
- local subtitle = "Select a program to run"
- local subtitleX = math.floor((w - #subtitle) / 2) + 1
- term.setCursorPos(subtitleX, 3)
- term.write(subtitle)
- -- Separator line
- term.setCursorPos(1, 5)
- term.setTextColor(colors.gray)
- term.write(string.rep("-", w))
- end
- local function drawMenuItem(index, item, isSelected)
- local w, h = term.getSize()
- local y = 6 + index
- if y > h - 2 then
- return
- end
- term.setCursorPos(1, y)
- if isSelected then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.write(" > " .. item.name)
- -- Fill spaces
- local spaces = w - #item.name - 3 - #item.description
- if spaces > 0 then
- term.write(string.rep(" ", spaces))
- end
- term.write(item.description .. " ")
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.write(" " .. item.name)
- -- Description on the right
- local spaces = w - #item.name - 3 - #item.description
- if spaces > 0 then
- term.write(string.rep(" ", spaces))
- term.setTextColor(colors.lightGray)
- term.write(item.description)
- end
- end
- -- Reset colors
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- local function drawFooter()
- local w, h = term.getSize()
- term.setCursorPos(1, h - 1)
- term.setTextColor(colors.gray)
- term.write(string.rep("-", w))
- term.setCursorPos(2, h)
- term.setTextColor(colors.lightGray)
- term.write("Arrow Keys: navigate | Enter: select | Q: back")
- end
- local function drawMenu()
- clearScreen()
- drawHeader()
- for i, item in ipairs(menuItems) do
- drawMenuItem(i, item, i == selectedIndex)
- end
- drawFooter()
- end
- -- File Manager functionality
- local function showFileManager()
- local currentPath = ""
- local files = {}
- local selectedFile = 1
- local fileManagerRunning = true
- local scrollOffset = 0
- local maxDisplayFiles = 15
- local function loadFiles(path)
- files = {}
- local fullPath = path == "" and "/" or path
- -- Add parent directory option if not in root
- if path ~= "" then
- table.insert(files, {name = "..", type = "directory", path = fs.getDir(path)})
- end
- -- Get all files and directories
- if fs.exists(fullPath) then
- local items = fs.list(fullPath)
- for _, item in ipairs(items) do
- local itemPath = fs.combine(fullPath, item)
- local fileType = fs.isDir(itemPath) and "directory" or "file"
- table.insert(files, {name = item, type = fileType, path = itemPath})
- end
- end
- selectedFile = 1
- scrollOffset = 0
- end
- local function drawFileManager()
- clearScreen()
- local w, h = term.getSize()
- -- Header
- term.setTextColor(colors.blue)
- local title = "| File Manager |"
- local titleX = math.floor((w - #title) / 2) + 1
- term.setCursorPos(titleX, 2)
- term.write(title)
- -- Current path
- term.setTextColor(colors.lightGray)
- local pathText = "Path: /" .. currentPath
- term.setCursorPos(2, 4)
- term.write(pathText)
- -- Separator
- term.setCursorPos(1, 5)
- term.setTextColor(colors.gray)
- term.write(string.rep("-", w))
- -- File list with scrolling
- local displayStart = scrollOffset + 1
- local displayEnd = math.min(#files, scrollOffset + maxDisplayFiles)
- for i = displayStart, displayEnd do
- local file = files[i]
- local y = 6 + (i - displayStart)
- term.setCursorPos(1, y)
- if i == selectedFile then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.write(" > ")
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.write(" ")
- end
- -- File icon and name
- if file.type == "directory" then
- term.setTextColor(i == selectedFile and colors.black or colors.yellow)
- term.write("[DIR] ")
- else
- term.setTextColor(i == selectedFile and colors.black or colors.white)
- term.write("[FILE] ")
- end
- -- Truncate long filenames
- local displayName = file.name
- if #displayName > w - 10 then
- displayName = string.sub(displayName, 1, w - 13) .. "..."
- end
- term.write(displayName)
- -- Reset colors
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- -- Scroll indicator
- if #files > maxDisplayFiles then
- term.setCursorPos(w - 8, h - 2)
- term.setTextColor(colors.lightGray)
- term.write(string.format("%d/%d", selectedFile, #files))
- end
- -- Footer
- term.setCursorPos(1, h - 1)
- term.setTextColor(colors.gray)
- term.write(string.rep("-", w))
- term.setCursorPos(2, h)
- term.setTextColor(colors.lightGray)
- term.write("Arrow Keys: navigate | Enter: open/run | Q: back")
- end
- local function executeFile(filePath)
- clearScreen()
- term.setTextColor(colors.yellow)
- print("Running: " .. filePath)
- print()
- local success, error = pcall(function()
- if string.find(filePath, "%.lua$") then
- dofile(filePath)
- else
- shell.run(filePath)
- end
- end)
- if not success then
- term.setTextColor(colors.red)
- print("Error: " .. tostring(error))
- end
- term.setTextColor(colors.yellow)
- print("\nPress any key to continue...")
- os.pullEvent("key")
- drawFileManager()
- end
- -- Initialize file manager
- loadFiles(currentPath)
- drawFileManager()
- -- Handle file manager input
- while fileManagerRunning do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selectedFile = selectedFile - 1
- if selectedFile < 1 then
- selectedFile = #files
- end
- -- Adjust scroll offset
- if selectedFile < scrollOffset + 1 then
- scrollOffset = selectedFile - 1
- elseif selectedFile > scrollOffset + maxDisplayFiles then
- scrollOffset = selectedFile - maxDisplayFiles
- end
- drawFileManager()
- elseif key == keys.down then
- selectedFile = selectedFile + 1
- if selectedFile > #files then
- selectedFile = 1
- end
- -- Adjust scroll offset
- if selectedFile < scrollOffset + 1 then
- scrollOffset = selectedFile - 1
- elseif selectedFile > scrollOffset + maxDisplayFiles then
- scrollOffset = selectedFile - maxDisplayFiles
- end
- drawFileManager()
- elseif key == keys.enter then
- if #files > 0 then
- local selectedItem = files[selectedFile]
- if selectedItem.type == "directory" then
- if selectedItem.name == ".." then
- currentPath = selectedItem.path
- else
- currentPath = selectedItem.path
- end
- loadFiles(currentPath)
- drawFileManager()
- else
- executeFile(selectedItem.path)
- end
- end
- elseif key == keys.q then
- fileManagerRunning = false
- end
- end
- drawMenu()
- end
- -- Calculator with scrollable menu
- local function calculator()
- local options = {
- "Addition",
- "Subtraction",
- "Multiplication",
- "Division",
- "Back to Menu"
- }
- local function drawCalculatorMenu(selected)
- term.clear()
- term.setCursorPos(1,1)
- print("=== Calculator ===")
- print("Use UP/DOWN arrows to navigate, ENTER to select.\n")
- for i, option in ipairs(options) do
- if i == selected then
- term.setTextColor(colors.yellow)
- print("> " .. option)
- term.setTextColor(colors.white)
- else
- print(" " .. option)
- end
- end
- end
- local function readNumber(prompt)
- while true do
- write(prompt)
- local input = read()
- local num = tonumber(input)
- if num then
- return num
- else
- print("Invalid input, please enter a number.")
- end
- end
- end
- local selected = 1
- drawCalculatorMenu(selected)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selected = selected - 1
- if selected < 1 then selected = #options end
- drawCalculatorMenu(selected)
- elseif key == keys.down then
- selected = selected + 1
- if selected > #options then selected = 1 end
- drawCalculatorMenu(selected)
- elseif key == keys.enter then
- if options[selected] == "Back to Menu" then
- term.clear()
- return
- else
- local a = readNumber("Enter first number: ")
- local b = readNumber("Enter second number: ")
- local result
- if options[selected] == "Addition" then
- result = a + b
- print(string.format("Result: %.2f + %.2f = %.2f", a, b, result))
- elseif options[selected] == "Subtraction" then
- result = a - b
- print(string.format("Result: %.2f - %.2f = %.2f", a, b, result))
- elseif options[selected] == "Multiplication" then
- result = a * b
- print(string.format("Result: %.2f * %.2f = %.2f", a, b, result))
- elseif options[selected] == "Division" then
- if b == 0 then
- print("Error: Division by zero is not allowed.")
- else
- result = a / b
- print(string.format("Result: %.2f / %.2f = %.2f", a, b, result))
- end
- end
- print("\nPress any key to return to calculator menu...")
- os.pullEvent("key")
- drawCalculatorMenu(selected)
- end
- end
- end
- end
- -- Shutdown procedure with farewell message
- local function shutdown()
- clearScreen()
- term.setTextColor(colors.red)
- print("\nNeobox OS Alpha shutting down. Goodbye!")
- sleep(1.5)
- os.shutdown()
- end
- -- Launch selected program or special function
- local function launchProgram(item)
- if item.type == "shell" then
- clearScreen()
- shell.run("shell")
- drawMenu()
- return
- elseif item.type == "filemanager" then
- showFileManager()
- return
- elseif item.type == "reboot" then
- os.reboot()
- return
- elseif item.type == "shutdown" then
- shutdown()
- return
- elseif item.type == "calculator" then
- calculator()
- drawMenu()
- return
- end
- if item.program then
- clearScreen()
- if fs.exists(item.program) or fs.exists("rom/programs/" .. item.program .. ".lua") then
- local success, error = pcall(function()
- shell.run(item.program)
- end)
- if not success then
- term.setTextColor(colors.red)
- print("Error running program: " .. tostring(error))
- end
- else
- term.setTextColor(colors.red)
- print("Program not found: " .. item.program)
- end
- term.setTextColor(colors.yellow)
- print("\nPress any key to return to menu...")
- os.pullEvent("key")
- drawMenu()
- end
- end
- -- Main loop
- drawMenu()
- while running do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selectedIndex = selectedIndex - 1
- if selectedIndex < 1 then
- selectedIndex = #menuItems
- end
- drawMenu()
- elseif key == keys.down then
- selectedIndex = selectedIndex + 1
- if selectedIndex > #menuItems then
- selectedIndex = 1
- end
- drawMenu()
- elseif key == keys.enter then
- launchProgram(menuItems[selectedIndex])
- elseif key == keys.q then
- running = false
- end
- end
- clearScreen()
- term.setCursorPos(1,1)
- print("Neobox OS Alpha shutting down. Goodbye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement