Advertisement
DOGGYWOOF

Untitled

Mar 12th, 2024 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 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. local fullPath = fs.combine(directory, file)
  10.  
  11. if fs.isDir(fullPath) then
  12. -- If it's a directory, recursively scan it
  13. local subPrograms = scanForPrograms(fullPath)
  14. for _, subProgram in ipairs(subPrograms) do
  15. table.insert(programs, fs.combine(file, subProgram))
  16. end
  17. else
  18. -- If it's a file, add it to the list
  19. table.insert(programs, file)
  20. end
  21. end
  22.  
  23. return programs
  24. end
  25.  
  26. -- Function to draw buttons for each program
  27. function drawButtons(programs, selectedApp)
  28. local screenWidth, screenHeight = term.getSize()
  29.  
  30. term.setBackgroundColor(colors.black)
  31. term.clear()
  32.  
  33. -- Draw app name at the top
  34. term.setTextColor(colors.white)
  35. term.setCursorPos(1, 1)
  36. term.write("APP NAME: " .. (selectedApp or ""))
  37.  
  38. -- Draw buttons
  39. for i, program in ipairs(programs) do
  40. term.setCursorPos(2, i + 2)
  41. term.setBackgroundColor(colors.gray)
  42. term.setTextColor(colors.white)
  43. term.clearLine()
  44. term.write(" " .. program .. " ")
  45. end
  46.  
  47. -- Draw navigation bar
  48. drawNavBar()
  49. end
  50.  
  51. -- Function to run a selected program
  52. function runSelectedProgram(program)
  53. term.setBackgroundColor(colors.black)
  54. term.clear()
  55. term.setCursorPos(1, 1)
  56. shell.run(fs.combine("/disk/packages/", program))
  57. end
  58.  
  59. -- Function to show the right-click menu for an app
  60. function showAppMenu(x, y, program)
  61. local menuOptions = {"Uninstall", "Modify code"}
  62.  
  63. -- Display the menu
  64. local choice = menu(menuOptions, x, y)
  65.  
  66. -- Perform the selected action
  67. if choice == "Uninstall" then
  68. -- Implement code to remove the app from /disk/packages
  69. term.setBackgroundColor(colors.black)
  70. term.clear()
  71. term.setCursorPos(1, 1)
  72. print("Uninstalling: " .. program)
  73. -- Add your uninstallation logic here
  74.  
  75. elseif choice == "Modify code" then
  76. -- Edit the app using the shell command "edit"
  77. term.setBackgroundColor(colors.black)
  78. term.clear()
  79. term.setCursorPos(1, 1)
  80. print("Editing code for: " .. program)
  81. shell.run("edit", fs.combine("/disk/packages/", program))
  82. end
  83. end
  84.  
  85. -- Function to handle mouse clicks on buttons
  86. function handleButtonClick(x, y, programs)
  87. for i, program in ipairs(programs) do
  88. if x >= 2 and x <= #program + 3 and y == i + 2 then
  89. if shell.openTab then
  90. -- Right-click behavior
  91. local _, _, _, isRightClick = os.pullEvent("mouse_click")
  92. if isRightClick then
  93. showAppMenu(x, y, program)
  94. return
  95. end
  96. end
  97.  
  98. -- Left-click behavior
  99. runSelectedProgram(program)
  100. return
  101. end
  102. end
  103.  
  104. -- Handle navigation bar clicks
  105. local screenWidth, screenHeight = term.getSize()
  106. local navBarWidth = 20
  107. local navBarStart = math.floor((screenWidth - navBarWidth) / 2)
  108.  
  109. if y == screenHeight then
  110. if x >= navBarStart + 4 and x <= navBarStart + 6 then
  111. -- Clicked on lll
  112. shell.run("/disk/os/recents")
  113. elseif x >= navBarStart + 9 and x <= navBarStart + 11 then
  114. -- Clicked on ()
  115. shell.run("/disk/os/gui")
  116. return -- Terminate the program after running /disk/os/gui
  117. elseif x >= navBarStart + 14 and x <= navBarStart + 16 then
  118. -- Clicked on <
  119. -- Handle back button if needed
  120. end
  121. end
  122. end
  123.  
  124. -- Function to draw navigation bar
  125. function drawNavBar()
  126. local screenWidth, screenHeight = term.getSize()
  127. local navBarWidth = 20
  128. local navBarStart = math.floor((screenWidth - navBarWidth) / 2)
  129.  
  130. -- Save current cursor position
  131. local oldX, oldY = term.getCursorPos()
  132.  
  133. -- Draw Recents button
  134. term.setCursorPos(navBarStart + 4, screenHeight)
  135. term.setBackgroundColor(colors.black)
  136. term.setTextColor(colors.white)
  137. term.write(" lll ")
  138.  
  139. -- Draw Home button
  140. term.setCursorPos(navBarStart + 9, screenHeight)
  141. term.setBackgroundColor(colors.black)
  142. term.setTextColor(colors.white)
  143. term.write(" () ")
  144.  
  145. -- Draw Back button
  146. term.setCursorPos(navBarStart + 14, screenHeight)
  147. term.setBackgroundColor(colors.black)
  148. term.setTextColor(colors.white)
  149. term.write(" < ")
  150.  
  151. -- Restore cursor position
  152. term.setCursorPos(oldX, oldY)
  153. end
  154.  
  155. -- Main function
  156. function main()
  157. local programDirectory = "/disk/packages/"
  158. local programs = scanForPrograms(programDirectory)
  159. local selectedApp = nil
  160.  
  161. while true do
  162. drawButtons(programs, selectedApp)
  163.  
  164. local event, button, x, y = os.pullEvent("mouse_click")
  165.  
  166. if event == "mouse_click" and button == 1 then
  167. handleButtonClick(x, y, programs)
  168. end
  169. end
  170. end
  171.  
  172. -- Run the main function
  173. main()
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement