Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Get the current username from the status file
- local function getCurrentUser()
- local file = fs.open(".currentusr", "r")
- if file then
- local username = file.readLine()
- file.close()
- return username
- else
- error("Failed to read current username")
- end
- end
- -- List all files in the user's packages directory
- local function listPrograms(username)
- local packagePath = "/disk/users/" .. username .. "/packages"
- if not fs.exists(packagePath) then
- error("Packages directory not found")
- end
- local programs = fs.list(packagePath)
- if #programs == 0 then
- return nil
- end
- return programs
- end
- -- Draw the UI for the launcher
- local function drawUI(programs, selected)
- term.clear()
- term.setCursorPos(1, 1)
- print("---- Program Launcher ----")
- print("Use UP/DOWN to navigate, ENTER to run, Q to quit.")
- -- Display programs in a list format
- for i, program in ipairs(programs) do
- if i == selected then
- term.setTextColor(colors.yellow)
- print("> " .. program)
- term.setTextColor(colors.white)
- else
- print(" " .. program)
- end
- end
- end
- -- Main loop to handle user input and selection
- local function launcherUI(username, programs)
- local selected = 1
- drawUI(programs, selected)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- if selected > 1 then
- selected = selected - 1
- drawUI(programs, selected)
- end
- elseif key == keys.down then
- if selected < #programs then
- selected = selected + 1
- drawUI(programs, selected)
- end
- elseif key == keys.enter then
- local selectedProgram = programs[selected]
- shell.run("/disk/users/" .. username .. "/packages/" .. selectedProgram)
- break -- After running a program, quit the launcher
- elseif key == keys.q then
- break -- Quit the launcher
- end
- end
- end
- -- Main program execution
- local function main()
- local username = getCurrentUser()
- local programs = listPrograms(username)
- if programs then
- launcherUI(username, programs)
- else
- term.clear()
- term.setCursorPos(1, 1)
- print("No programs found.")
- end
- end
- -- Execute the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement