Advertisement
DOGGYWOOF

Untitled

Mar 12th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 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. local screenWidth, screenHeight = term.getSize()
  27. local navBarWidth = 20
  28. local navBarStart = math.floor((screenWidth - navBarWidth) / 2)
  29.  
  30. term.setBackgroundColor(colors.black)
  31. term.clear()
  32.  
  33. term.setTextColor(colors.white)
  34. term.setCursorPos(1, 1)
  35. term.write("Select an app to run:")
  36.  
  37. for i, program in ipairs(programs) do
  38. term.setCursorPos(2, i + 2)
  39. term.setBackgroundColor(colors.gray)
  40. term.setTextColor(colors.white)
  41. term.clearLine()
  42. term.write(" " .. program .. " ")
  43. end
  44.  
  45. -- Draw navigation bar
  46. term.setCursorPos(navBarStart + 4, screenHeight)
  47. term.setBackgroundColor(colors.black)
  48. term.setTextColor(colors.white)
  49. term.write(" lll ")
  50.  
  51. term.setCursorPos(navBarStart + 9, screenHeight)
  52. term.setBackgroundColor(colors.black)
  53. term.setTextColor(colors.white)
  54. term.write(" () ")
  55.  
  56. term.setCursorPos(navBarStart + 14, screenHeight)
  57. term.setBackgroundColor(colors.black)
  58. term.setTextColor(colors.white)
  59. term.write(" < ")
  60. end
  61.  
  62. -- Function to run a selected program
  63. function runSelectedProgram(program)
  64. term.setBackgroundColor(colors.black)
  65. term.clear()
  66. term.setCursorPos(1, 1)
  67. shell.run(fs.combine("/disk/packages/", program))
  68. end
  69.  
  70. -- Function to handle mouse clicks on buttons
  71. function handleButtonClick(x, y, programs)
  72. for i, program in ipairs(programs) do
  73. if x >= 2 and x <= #program + 3 and y == i + 2 then
  74. runSelectedProgram(program)
  75. break
  76. end
  77. end
  78.  
  79. -- Handle navigation bar clicks
  80. local screenWidth, screenHeight = term.getSize()
  81. local navBarWidth = 20
  82. local navBarStart = math.floor((screenWidth - navBarWidth) / 2)
  83.  
  84. if y == screenHeight then
  85. if x >= navBarStart + 4 and x <= navBarStart + 6 then
  86. -- Clicked on lll
  87. shell.run("/disk/os/recents")
  88. elseif x >= navBarStart + 9 and x <= navBarStart + 11 then
  89. -- Clicked on ()
  90. shell.run("/disk/os/gui")
  91. -- Back button does nothing
  92. end
  93. end
  94. end
  95.  
  96. -- Main function
  97. function main()
  98. local programDirectory = "/disk/packages/"
  99. local programs = scanForPrograms(programDirectory)
  100.  
  101. while true do
  102. drawButtons(programs)
  103.  
  104. local event, button, x, y = os.pullEvent("mouse_click")
  105.  
  106. if event == "mouse_click" and button == 1 then
  107. handleButtonClick(x, y, programs)
  108. end
  109. end
  110. end
  111.  
  112. -- Run the main function
  113. main()
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement