SHOW:
|
|
- or go back to the newest paste.
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 | ||
28 | term.setBackgroundColor(colors.black) | |
29 | term.clear() | |
30 | ||
31 | term.setTextColor(colors.white) | |
32 | term.setCursorPos(1, 1) | |
33 | term.write("Select an app to run:") | |
34 | ||
35 | for i, program in ipairs(programs) do | |
36 | term.setCursorPos(2, i + 2) | |
37 | term.setBackgroundColor(colors.gray) | |
38 | term.setTextColor(colors.white) | |
39 | term.clearLine() | |
40 | term.write(" " .. program .. " ") | |
41 | end | |
42 | ||
43 | -- Draw navigation bar | |
44 | drawNavBar() | |
45 | end | |
46 | ||
47 | -- Function to run a selected program | |
48 | function runSelectedProgram(program) | |
49 | term.setBackgroundColor(colors.black) | |
50 | term.clear() | |
51 | term.setCursorPos(1, 1) | |
52 | shell.run(fs.combine("/disk/packages/", program)) | |
53 | end | |
54 | ||
55 | -- Function to handle mouse clicks on buttons | |
56 | function handleButtonClick(x, y, programs) | |
57 | for i, program in ipairs(programs) do | |
58 | if x >= 2 and x <= #program + 3 and y == i + 2 then | |
59 | runSelectedProgram(program) | |
60 | return | |
61 | end | |
62 | end | |
63 | ||
64 | -- Handle navigation bar clicks | |
65 | local screenWidth, screenHeight = term.getSize() | |
66 | local navBarWidth = 20 | |
67 | local navBarStart = math.floor((screenWidth - navBarWidth) / 2) | |
68 | ||
69 | if y == screenHeight then | |
70 | - | if x >= navBarStart + 4 and x <= navBarStart + 6 then |
70 | + | if x >= navBarStart + 5 and x <= navBarStart + 8 then |
71 | -- Clicked on () | |
72 | shell.run("/disk/os/gui") | |
73 | - | elseif x >= navBarStart + 9 and x <= navBarStart + 11 then |
73 | + | |
74 | elseif x >= navBarStart + 10 and x <= navBarStart + 13 then | |
75 | -- Clicked on lll | |
76 | shell.run("/disk/os/recents") | |
77 | - | elseif x >= navBarStart + 14 and x <= navBarStart + 16 then |
77 | + | elseif x >= navBarStart + 15 and x <= navBarStart + 18 then |
78 | -- Clicked on < | |
79 | -- Handle back button if needed | |
80 | end | |
81 | end | |
82 | end | |
83 | ||
84 | -- Function to draw navigation bar | |
85 | function drawNavBar() | |
86 | local screenWidth, screenHeight = term.getSize() | |
87 | local navBarWidth = 20 | |
88 | local navBarStart = math.floor((screenWidth - navBarWidth) / 2) | |
89 | ||
90 | -- Save current cursor position | |
91 | local oldX, oldY = term.getCursorPos() | |
92 | ||
93 | -- Draw Home button | |
94 | - | term.setCursorPos(navBarStart + 4, screenHeight) |
94 | + | term.setCursorPos(navBarStart + 5, screenHeight) |
95 | term.setBackgroundColor(colors.black) | |
96 | term.setTextColor(colors.white) | |
97 | term.write(" () ") | |
98 | ||
99 | -- Draw Recents button | |
100 | - | term.setCursorPos(navBarStart + 9, screenHeight) |
100 | + | term.setCursorPos(navBarStart + 10, screenHeight) |
101 | term.setBackgroundColor(colors.black) | |
102 | term.setTextColor(colors.white) | |
103 | term.write(" lll ") | |
104 | ||
105 | -- Draw Back button | |
106 | - | term.setCursorPos(navBarStart + 14, screenHeight) |
106 | + | term.setCursorPos(navBarStart + 15, screenHeight) |
107 | term.setBackgroundColor(colors.black) | |
108 | term.setTextColor(colors.white) | |
109 | term.write(" < ") | |
110 | ||
111 | -- Restore cursor position | |
112 | term.setCursorPos(oldX, oldY) | |
113 | end | |
114 | ||
115 | -- Main function | |
116 | function main() | |
117 | local programDirectory = "/disk/packages/" | |
118 | local programs = scanForPrograms(programDirectory) | |
119 | ||
120 | while true do | |
121 | drawButtons(programs) | |
122 | ||
123 | local event, button, x, y = os.pullEvent("mouse_click") | |
124 | ||
125 | if event == "mouse_click" and button == 1 then | |
126 | handleButtonClick(x, y, programs) | |
127 | end | |
128 | end | |
129 | end | |
130 | ||
131 | -- Run the main function | |
132 | main() | |
133 |