Advertisement
DOGGYWOOF

E

Mar 11th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. -- Program Scanner and Runner with GUI (Nicely Styled)
  2.  
  3. -- Function to scan for programs in a directory
  4. function scanForPrograms(directory)
  5. local programs = {}
  6. local files = fs.list(directory)
  7.  
  8. for _, file in ipairs(files) do
  9. if fs.isDir(fs.combine(directory, file)) then
  10. -- If it's a directory, recursively scan it
  11. local subPrograms = scanForPrograms(fs.combine(directory, file))
  12. for _, subProgram in ipairs(subPrograms) do
  13. table.insert(programs, fs.combine(file, subProgram))
  14. end
  15. else
  16. -- If it's a file, add it to the list
  17. table.insert(programs, file)
  18. end
  19. end
  20.  
  21. return programs
  22. end
  23.  
  24. -- Function to draw buttons for each program
  25. function drawButtons(programs)
  26. term.setBackgroundColor(colors.black)
  27. term.clear()
  28.  
  29. term.setTextColor(colors.white)
  30. term.setCursorPos(1, 1)
  31. term.write("Select an app to run:")
  32.  
  33. for i, program in ipairs(programs) do
  34. term.setCursorPos(2, i + 2)
  35. term.setBackgroundColor(colors.gray)
  36. term.setTextColor(colors.white)
  37. term.clearLine()
  38. term.write(" " .. program .. " ")
  39. end
  40. end
  41.  
  42. -- Function to run a selected program
  43. function runSelectedProgram(program)
  44. term.setBackgroundColor(colors.black)
  45. term.clear()
  46. term.setCursorPos(1, 1)
  47. shell.run(fs.combine("/disk/packages/", program))
  48. end
  49.  
  50. -- Function to handle mouse clicks on buttons
  51. function handleButtonClick(x, y, programs)
  52. for i, program in ipairs(programs) do
  53. if x >= 2 and x <= #program + 3 and y == i + 2 then
  54. runSelectedProgram(program)
  55. break
  56. end
  57. end
  58. end
  59.  
  60. -- Main function
  61. function main()
  62. local programDirectory = "/disk/packages/"
  63. local programs = scanForPrograms(programDirectory)
  64.  
  65. while true do
  66. drawButtons(programs)
  67.  
  68. local event, button, x, y = os.pullEvent("mouse_click")
  69.  
  70. if event == "mouse_click" and button == 1 then
  71. handleButtonClick(x, y, programs)
  72. end
  73. end
  74. end
  75.  
  76. -- Run the main function
  77. main()
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement