Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Program Scanner and Runner with GUI (Nicely Styled)
- -- Function to scan for programs in a directory
- function scanForPrograms(directory)
- local programs = {}
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- if fs.isDir(fs.combine(directory, file)) then
- -- If it's a directory, recursively scan it
- local subPrograms = scanForPrograms(fs.combine(directory, file))
- for _, subProgram in ipairs(subPrograms) do
- table.insert(programs, fs.combine(file, subProgram))
- end
- else
- -- If it's a file, add it to the list
- table.insert(programs, file)
- end
- end
- return programs
- end
- -- Function to draw buttons for each program
- function drawButtons(programs)
- local screenWidth, screenHeight = term.getSize()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.write("Select an app to run:")
- for i, program in ipairs(programs) do
- term.setCursorPos(2, i + 2)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clearLine()
- term.write(" " .. program .. " ")
- end
- -- Draw navigation bar
- drawNavBar()
- end
- -- Function to run a selected program
- function runSelectedProgram(program)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- shell.run(fs.combine("/disk/packages/", program))
- end
- -- Function to handle mouse clicks on buttons
- function handleButtonClick(x, y, programs)
- for i, program in ipairs(programs) do
- if x >= 2 and x <= #program + 3 and y == i + 2 then
- runSelectedProgram(program)
- return
- end
- end
- -- Handle navigation bar clicks
- local screenWidth, screenHeight = term.getSize()
- local navBarWidth = 20
- local navBarStart = math.floor((screenWidth - navBarWidth) / 2)
- if y == screenHeight then
- if x >= navBarStart + 4 and x <= navBarStart + 6 then
- -- Clicked on lll
- shell.run("/disk/os/recents")
- elseif x >= navBarStart + 9 and x <= navBarStart + 11 then
- -- Clicked on ()
- shell.run("/disk/os/gui")
- return -- Terminate the program after running /disk/os/gui
- elseif x >= navBarStart + 14 and x <= navBarStart + 16 then
- -- Clicked on <
- -- Handle back button if needed
- end
- end
- end
- -- Function to draw navigation bar
- function drawNavBar()
- local screenWidth, screenHeight = term.getSize()
- local navBarWidth = 20
- local navBarStart = math.floor((screenWidth - navBarWidth) / 2)
- -- Save current cursor position
- local oldX, oldY = term.getCursorPos()
- -- Draw Recents button
- term.setCursorPos(navBarStart + 4, screenHeight)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(" lll ")
- -- Draw Home button
- term.setCursorPos(navBarStart + 9, screenHeight)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(" () ")
- -- Draw Back button
- term.setCursorPos(navBarStart + 14, screenHeight)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(" < ")
- -- Restore cursor position
- term.setCursorPos(oldX, oldY)
- end
- -- Main function
- function main()
- local programDirectory = "/disk/packages/"
- local programs = scanForPrograms(programDirectory)
- while true do
- drawButtons(programs)
- local event, button, x, y = os.pullEvent("mouse_click")
- if event == "mouse_click" and button == 1 then
- handleButtonClick(x, y, programs)
- end
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement