· 4 years ago · Jun 07, 2021, 04:06 PM
1os.loadAPI("/RSync/**syncApi.lua")
2
3w, h = term.getSize()
4term.clear()
5
6local api = syncApi
7
8
9function drawHeader(header)
10 bg = colors.blue
11 paintutils.drawFilledBox(1, 1, w, 3, bg)
12 term.setBackgroundColor(bg)
13 term.setTextColor(colors.white)
14 term.setCursorPos(4, 2)
15 term.write(header)
16 term.setCursorPos(2,2)
17 term.write("<")
18 term.setCursorPos(w - 1,2)
19 term.write(">")
20end
21function syncControlsMenu()
22 paintutils.drawFilledBox(1, 4, w, h, colors.black)
23
24 --Menu items
25 term.setBackgroundColor(colors.black)
26
27 term.setTextColor(colors.red)
28 term.setCursorPos(2,5)
29 term.write(" ^ Upload All Files ")
30
31 term.setTextColor(colors.green)
32 term.setCursorPos(2,7)
33 term.write(" v Download All Files ")
34
35 term.setTextColor(colors.lightGray)
36 term.setCursorPos(2,9)
37 term.write(" Clear Message Queue ")
38
39 term.setTextColor(colors.gray)
40 term.setCursorPos(2, h - 1)
41 term.write(" Sync Client - " .. syncApi.t["ip"] .. ":" .. syncApi.t["port"])
42
43 menuIndex = currentMenu
44 while menuIndex == currentMenu do
45 os.startTimer(0.25)
46 local e, c, x, y = os.pullEvent()
47 if e == "mouse_click" then
48 term.setBackgroundColor(colors.white)
49 term.setTextColor(colors.black)
50 if y == 5 then
51 term.setCursorPos(2,5)
52 term.write(" Uploading all files ")
53 api.clearMessages()
54 api.sendAllFiles()
55 sleep(0.5)
56 api.clearMessages()
57 break
58 end
59 if y == 7 then
60 term.setCursorPos(2,7)
61 term.write(" Downloading all files ")
62 api.clearMessages()
63 api.getAllFiles()
64 sleep(0.5)
65 api.clearMessages()
66 break
67 end
68 if y == 9 then
69 term.setCursorPos(2,9)
70 term.write(" Clearing Message Queue ")
71 api.clearMessages()
72 sleep(0.5)
73 break
74 end
75 end
76 end
77 if menuIndex == currentMenu then
78 syncControlsMenu()
79 end
80end
81search = ""
82typing = false
83listOffset = 1
84currentList = {}
85function getValue(index)
86 i = index + listOffset
87 if i < 1 or i > #currentList then
88 return ""
89 end
90 return currentList[i]
91end
92function drawMenuList()
93 paintutils.drawFilledBox(2, 7, w-1, h-1, colors.black)
94 term.setBackgroundColor(colors.black)
95 term.setTextColor(colors.white)
96 for i = 0, ((h-1) - 7) do
97 term.setCursorPos(2,7 + i)
98 term.write(getValue(i))
99 end
100end
101function searchFunction(list)
102 if search == "" then
103 return list
104 else
105 local returnList = {}
106 for i,v in pairs(list) do
107 if string.find(v,search) then
108 table.insert(returnList,v)
109 end
110 end
111 return returnList
112 end
113end
114function localSyncMenu()
115 allList = api.getLocalFiles()
116 currentList = searchFunction(allList)
117
118 paintutils.drawFilledBox(1, 4, w, h, colors.black)
119
120 --Menu items
121 paintutils.drawFilledBox(2, 5, w-1, 5, colors.gray)
122 term.setBackgroundColor(colors.gray)
123 term.setTextColor(colors.white)
124 term.setCursorPos(2,5)
125 term.write(search)
126
127 drawMenuList()
128
129 menuIndex = currentMenu
130 while menuIndex == currentMenu do
131 os.startTimer(0.25)
132 local e, c, x, y = os.pullEvent()
133 if e == "mouse_click" then
134 if y == 5 then
135 typing = true
136 while typing do
137 currentList = searchFunction(allList)
138 drawMenuList()
139
140 paintutils.drawFilledBox(2, 5, w-1, 5, colors.lightGray)
141 term.setBackgroundColor(colors.lightGray)
142 term.setTextColor(colors.white)
143 term.setCursorPos(2,5)
144 term.write(search)
145
146 local e, key, isHeld = os.pullEvent()
147 if e == "key" then
148 if key == keys.backspace then
149 search = string.sub(search, 0, math.max(#search-1, 0))
150 elseif key == keys.enter then
151 typing = false
152 end
153 elseif e == "char" then
154 search = search .. key
155 end
156 end
157 break
158 elseif y >= 7 and y <= h-1 then
159 index = y - 7
160 item = getValue(index)
161
162 paintutils.drawFilledBox(2, y, w-1, y, colors.grey)
163 term.setBackgroundColor(colors.gray)
164 term.setTextColor(colors.white)
165 term.setCursorPos(2,y)
166 term.write(item)
167
168 api.clearMessages()
169 api.sendFile(item)
170
171 sleep(0.5)
172 break
173 end
174 elseif e == "mouse_scroll" then
175 listOffset = listOffset + c;
176 if listOffset < 1 then
177 listOffset = 1
178 end
179 if listOffset > #currentList then
180 listOffset = #currentList
181 end
182 break
183 end
184 end
185 if menuIndex == currentMenu then
186 localSyncMenu()
187 end
188end
189function serverSyncMenu()
190 allList = api.getFilesList()
191 currentList = searchFunction(allList)
192 paintutils.drawFilledBox(1, 4, w, h, colors.black)
193
194 --Menu items
195 paintutils.drawFilledBox(2, 5, w-1, 5, colors.gray)
196 term.setBackgroundColor(colors.gray)
197 term.setTextColor(colors.white)
198 term.setCursorPos(2,5)
199 term.write(search)
200
201 drawMenuList()
202
203 menuIndex = currentMenu
204 while menuIndex == currentMenu do
205 os.startTimer(0.25)
206 local e, c, x, y = os.pullEvent()
207 if e == "mouse_click" then
208 if y == 5 then
209 typing = true
210 while typing do
211 currentList = searchFunction(allList)
212 drawMenuList()
213
214 paintutils.drawFilledBox(2, 5, w-1, 5, colors.lightGray)
215 term.setBackgroundColor(colors.lightGray)
216 term.setTextColor(colors.white)
217 term.setCursorPos(2,5)
218 term.write(search)
219
220 local e, key, isHeld = os.pullEvent()
221 if e == "key" then
222 if key == keys.backspace then
223 search = string.sub(search, 0, math.max(#search-1, 0))
224 elseif key == keys.enter then
225 typing = false
226 end
227 elseif e == "char" then
228 search = search .. key
229 end
230 end
231 break
232 elseif y >= 7 and y <= h-1 then
233 index = y - 7
234 item = getValue(index)
235
236 paintutils.drawFilledBox(2, y, w-1, y, colors.grey)
237 term.setBackgroundColor(colors.gray)
238 term.setTextColor(colors.white)
239 term.setCursorPos(2,y)
240 term.write(item)
241
242 api.clearMessages()
243 api.getFile(item)
244
245 sleep(0.5)
246 break
247 end
248 elseif e == "mouse_scroll" then
249 listOffset = listOffset + c;
250 if listOffset < 1 then
251 listOffset = 1
252 end
253 if listOffset > #currentList then
254 listOffset = #currentList
255 end
256 break
257 end
258 end
259 if menuIndex == currentMenu then
260 serverSyncMenu()
261 end
262
263end
264menuCount = 3
265currentMenu = 1
266menus = {
267 ["Sync Controls"] = syncControlsMenu,
268 ["Upload to Server"] = localSyncMenu,
269 ["Download from Server"] = serverSyncMenu
270}
271menuIndexs = {
272 "Sync Controls",
273 "Upload to Server",
274 "Download from Server"
275}
276function drawUI()
277 menuName = menuIndexs[currentMenu]
278 menus[menuName]()
279end
280
281function frontend()
282 while true do
283 drawUI()
284 end
285end
286
287function backend()
288 while true do
289 menuName = menuIndexs[currentMenu]
290 drawHeader(menuName)
291
292 local e, c, x, y = os.pullEvent("mouse_click")
293 if typing == false then
294 if y < 4 then
295 if x < 4 then
296 currentMenu = currentMenu - 1
297 if currentMenu < 1 then
298 currentMenu = menuCount
299 end
300 else
301 currentMenu = currentMenu + 1
302 if currentMenu > menuCount then
303 currentMenu = 1
304 end
305 end
306 end
307 end
308 end
309end
310
311parallel.waitForAll(frontend,backend)
312
313
314paintutils.drawFilledBox(2, 5, w-1, h - 1, colors.gray)