dacman9

DacLauncher2 Temporary

Sep 9th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 KB | None | 0 0
  1. --Variables
  2. local locations = ""
  3. local x,y = term.getSize()
  4. local orig = term.current()
  5. local main = window.create(term.current(),1,1,x,y,true)
  6. local version = 2
  7. local url = "https://raw.githubusercontent.com/TheDacinator/DacLauncher/master/main"
  8. local origurl = url
  9. local updateurl = false
  10. local origgoesto = false
  11. local useonline = true
  12. local scroll = 1
  13. local list = {}
  14. local goesto = {}
  15. local source = false
  16. local clipboard = false
  17. local origlist = false
  18. local bookmarks = {"https://raw.githubusercontent.com/TheDacinator/DacLauncher/master/main","disk",""}
  19. local booknicks = {"Homepage","Drive","Main Directory"}
  20. if fs.exists("dlbookmarks") then
  21. local file = fs.open("dlbookmarks","r")
  22. bookmarks,booknicks = table.unpack(textutils.unserialize(file.readAll()))
  23. file.close()
  24. end
  25. --Functions
  26. local drawTop = function()
  27. paintutils.drawBox(1,1,x,1,colors.white)
  28. term.setCursorPos(1,1)
  29. term.setTextColor(colors.green)
  30. term.setBackgroundColor(colors.white)
  31. term.write("Dac")
  32. term.setTextColor(colors.lime)
  33. term.write("Launcher")
  34. local vers = "Version: "..version
  35. term.setCursorPos(x-string.len(vers)+1,1)
  36. term.setTextColor(colors.orange)
  37. term.write(vers)
  38. end
  39. local drawBar = function(rd)
  40. paintutils.drawBox(1,2,x,2,colors.lightGray)
  41. term.setCursorPos(x,2)
  42. term.blit("+","1","7")
  43. term.setCursorPos(x-4,2)
  44. if useonline then
  45. term.setTextColor(colors.white)
  46. term.setBackgroundColor(colors.lightBlue)
  47. term.write("HTTP")
  48. else
  49. term.setTextColor(colors.white)
  50. term.setBackgroundColor(colors.green)
  51. term.write("FILE")
  52. end
  53. term.setCursorPos(1,2)
  54. term.setBackgroundColor(colors.lightGray)
  55. term.setTextColor(colors.gray)
  56. if rd then
  57. url = read()
  58. origlist = false
  59. if string.sub(url,1,7) == "http://" or string.sub(url,1,8) == "https://" then
  60. useonline = true
  61. else
  62. useonline = false
  63. end
  64. else
  65. term.write(string.sub(url,1,x-5))
  66. end
  67. end
  68. local rclick = function(px,py,args,parent)
  69. local maxl = 1
  70. if #args > 0 then
  71. for i = 1,#args do
  72. if string.len(args[i]) > maxl then
  73. maxl = string.len(args[i])
  74. end
  75. end
  76. if px > x-maxl+1 then
  77. px = x-maxl+1
  78. end
  79. if py > y-#args+1 then
  80. py = y-#args+1
  81. end
  82. local win = window.create(parent,px,py,maxl,#args,true)
  83. win.setTextColor(colors.green)
  84. win.setBackgroundColor(colors.white)
  85. for i = 1,#args do
  86. win.setCursorPos(1,i)
  87. win.write(args[i]..string.rep(" ",maxl-string.len(args[i])))
  88. end
  89. local e,a,b,c = os.pullEvent("mouse_click")
  90. local out = false
  91. if b >= px and b < px+maxl and c >= py and c < py+#args then
  92. out = c-py+1
  93. end
  94. win.setVisible(false)
  95. main.redraw()
  96. return out
  97. end
  98. end
  99. local drawItems = function()
  100. term.setBackgroundColor(colors.black)
  101. if string.sub(url,1,7) == "http://" or string.sub(url,1,8) == "https://" then
  102. useonline = true
  103. else
  104. useonline = false
  105. end
  106. if useonline and (not origlist) then
  107. local file = http.get(url)
  108. local page = textutils.unserialize(file.readAll())
  109. if page then
  110. source = page[1]
  111. list = page[2]
  112. goesto = page[3]
  113. locations = page[4]
  114. else
  115. url = ""
  116. drawBar()
  117. term.setBackgroundColor(colors.black)
  118. end
  119. file.close()
  120. elseif not origlist then
  121. if fs.isDir(url) then
  122. list = fs.list(url)
  123. else
  124. url = ""
  125. list = fs.list("")
  126. drawBar()
  127. term.setBackgroundColor(colors.black)
  128. end
  129. for i = 1,#list do
  130. if fs.isDir(fs.combine(url,list[i])) then
  131. goesto[i] = fs.combine(url,list[i])
  132. else
  133. goesto[i] = false
  134. end
  135. end
  136. if fs.getDir(url) == ".." then
  137. source = false
  138. else
  139. source = fs.getDir(url)
  140. end
  141. end
  142. for i = scroll,#list do
  143. if goesto[i] then
  144. term.setTextColor(colors.green)
  145. else
  146. term.setTextColor(colors.white)
  147. end
  148. term.setCursorPos(1,i-scroll+3)
  149. term.write(list[i]..string.rep(" ",x-string.len(list[i])))
  150. if i-scroll+4 > y then
  151. break
  152. end
  153. end
  154. local cx,cy = term.getCursorPos()
  155. if cy < y then
  156. paintutils.drawFilledBox(1,cy+1,x,y)
  157. end
  158. end
  159. local paste = function()
  160. if clipboard then
  161. if clipboard[2] then
  162. fs.makeDir(paste[1])
  163. else
  164. local file = fs.open(fs.combine(url,clipboard[1]),"w")
  165. file.write(clipboard[3])
  166. file.close()
  167. end
  168. drawItems()
  169. end
  170. end
  171. local answer = function(question)
  172. local win = window.create(orig,1,y,x,1,true)
  173. win.setCursorPos(1,1)
  174. win.setBackgroundColor(colors.lightGray)
  175. win.setTextColor(colors.gray)
  176. win.write(question..string.rep(" ",x-string.len(question)))
  177. local old = term.current()
  178. term.redirect(orig)
  179. term.setCursorPos(string.len(question)+1,y)
  180. term.setBackgroundColor(colors.lightGray)
  181. term.setTextColor(colors.black)
  182. local out = read()
  183. term.redirect(old)
  184. win.setVisible(false)
  185. main.redraw()
  186. drawBar()
  187. drawTop()
  188. drawItems()
  189. return out
  190. end
  191. local savebm = function()
  192. local file = fs.open("dlbookmarks","w")
  193. file.write(textutils.serialize({bookmarks,booknicks}))
  194. file.close()
  195. end
  196. local jointbl = function(a,b)
  197. local out = a
  198. for key,value in ipairs(b) do
  199. table.insert(out,value)
  200. end
  201. return out
  202. end
  203. local make = function(isDir)
  204. if isDir then
  205. fs.makeDir(fs.combine(url,answer("Enter Directory Name: ")))
  206. else
  207. local file = fs.open(fs.combine(url,answer("Enter File Name: ")),"w")
  208. file.write("")
  209. file.close()
  210. end
  211. origlist = false
  212. drawTop()
  213. drawBar()
  214. drawItems()
  215. end
  216. local runargs = function(c)
  217. local inp = answer("Enter Program Arguments: ")
  218. local func = loadstring("")
  219. local values = {}
  220. if useonline then
  221. local file = http.get(locations[c-3+scroll])
  222. func = loadstring(file.readAll())
  223. file.close()
  224. else
  225. values = {fs.combine(url,list[c-3+scroll])}
  226. end
  227. for value in string.gmatch(inp,"[^, ]+") do
  228. table.insert(values,value)
  229. end
  230. term.clear()
  231. term.setCursorPos(1,1)
  232. if useonline then
  233. func(table.unpack(values))
  234. else
  235. shell.run(table.unpack(values))
  236. end
  237. drawItems()
  238. drawTop()
  239. drawBar()
  240. end
  241. --Code
  242. term.redirect(main)
  243. drawTop()
  244. drawBar()
  245. drawItems()
  246. while true do
  247. local e,a,b,c = os.pullEvent()
  248. if e == "mouse_click" then
  249. if a == 1 then
  250. if b < x-4 and c == 2 then
  251. drawBar(true)
  252. drawBar()
  253. drawItems()
  254. elseif c > 2 and list[c-3+scroll] and goesto[c-3+scroll] then
  255. url = goesto[c-3+scroll]
  256. origlist = false
  257. scroll = 1
  258. drawBar()
  259. drawItems()
  260. elseif c > 2 and list[c-3+scroll] and (not goesto[c-3+scroll]) then
  261. if useonline then
  262. if locations[c-3+scroll] then
  263. term.clear()
  264. term.setCursorPos(1,1)
  265. local web = http.get(locations[c-3+scroll])
  266. loadstring(web.readAll())()
  267. web.close()
  268. drawTop()
  269. drawBar()
  270. drawItems()
  271. end
  272. else
  273. term.clear()
  274. term.setCursorPos(1,1)
  275. shell.run(fs.combine(url,list[c-3+scroll]))
  276. drawTop()
  277. drawBar()
  278. drawItems()
  279. end
  280. elseif c == 2 and b == x then
  281. local selection = rclick(x,3,jointbl({"Add","Remove"},booknicks),orig)
  282. if selection == 1 then
  283. table.insert(bookmarks,answer("Enter new URL: "))
  284. table.insert(booknicks,answer("Enter bookmark nickname: "))
  285. savebm()
  286. elseif selection == 2 then
  287. local selection = rclick(x,3,booknicks,orig)
  288. if selection then
  289. table.remove(bookmarks,selection)
  290. table.remove(booknicks,selection)
  291. savebm()
  292. end
  293. elseif selection and selection > 2 then
  294. url = bookmarks[selection-2]
  295. drawBar()
  296. drawItems()
  297. end
  298. end
  299. elseif a == 2 then
  300. if c > 2 and list[c-3+scroll] and (not goesto[c-3+scroll]) then
  301. if useonline then
  302. local selection = rclick(b,c,{"Copy","Run with Arguments"},orig)
  303. if selection == 1 then
  304. local file = http.get(locations[c-3+scroll])
  305. clipboard = {list[c-3+scroll],goesto[c-3+scroll],file.readAll()}
  306. file.close()
  307. elseif selection == 2 then
  308. runargs(c)
  309. end
  310. else
  311. local selection = rclick(b,c,{"Copy","Paste","Delete","Rename","Make File","Make Directory","Edit","Run with Arguments"},orig)
  312. if selection then
  313. if selection == 1 then
  314. local file = fs.open(fs.combine(url,list[c-3+scroll]),"r")
  315. clipboard = {list[c-3+scroll],goesto[c-3+scroll],file.readAll()}
  316. file.close()
  317. elseif selection == 2 then
  318. paste()
  319. elseif selection == 3 then
  320. fs.delete(fs.combine(url,list[c-3+scroll]))
  321. paintutils.drawFilledBox(1,3,x,y,colors.black)
  322. drawItems()
  323. elseif selection == 4 then
  324. local file = fs.open(fs.combine(url,list[c-3+scroll]),"r")
  325. local data = file.readAll()
  326. file.close()
  327. fs.delete(fs.combine(url,list[c-3+scroll]))
  328. local file = fs.open(fs.combine(url,answer("Enter new name: ")),"w")
  329. drawItems()
  330. file.write(data)
  331. file.close()
  332. elseif selection == 5 then
  333. make(false)
  334. elseif selection == 6 then
  335. make(true)
  336. elseif selection == 7 then
  337. term.clear()
  338. term.setCursorPos(1,1)
  339. shell.run("rom/programs/edit",fs.combine(url,list[c-3+scroll]))
  340. drawTop()
  341. drawBar()
  342. drawItems()
  343. elseif selection == 8 then
  344. runargs(c)
  345. end
  346. end
  347. end
  348. elseif c > 2 and list[c-3+scroll] and goesto[c-3+scroll] and (not useonline) then
  349. local selection = rclick(b,c,{"Paste","Delete","Make File","Make Directory"},orig)
  350. if selection == 1 then
  351. paste()
  352. elseif selection == 2 then
  353. fs.delete(fs.combine(url,list[c-3+scroll]))
  354. paintutils.drawFilledBox(1,3,x,y,colors.black)
  355. drawItems()
  356. elseif selection == 3 then
  357. make(false)
  358. elseif selection == 4 then
  359. make(true)
  360. end
  361. elseif c > 2 and (not useonline) then
  362. local selection = rclick(b,c,{"Paste","Make File","Make Directory"},orig)
  363. if selection == 1 then
  364. paste()
  365. elseif selection == 2 then
  366. make(false)
  367. elseif selection == 3 then
  368. make(true)
  369. end
  370. end
  371. end
  372. elseif e == "mouse_scroll" then
  373. if c > 2 then
  374. if a == 1 and scroll <= #list-(y-2) then
  375. scroll = scroll+1
  376. elseif a == -1 and scroll > 1 then
  377. scroll = scroll-1
  378. end
  379. drawItems()
  380. end
  381. elseif e == "key" and a == 14 and b == false and source then
  382. url = source
  383. scroll = 1
  384. drawBar()
  385. drawItems()
  386. elseif e == "char" and a == "f" then
  387. local sort = answer("Find: ")
  388. if not origlist then
  389. origlist = list
  390. end
  391. if not origgoesto then
  392. origgoesto = goesto
  393. end
  394. if url ~= origurl or updateurl then
  395. if updateurl then
  396. drawItems()
  397. end
  398. origurl = url
  399. origgoesto = goesto
  400. end
  401. list = {}
  402. goesto = {}
  403. for i = 1,#origlist do
  404. if string.lower(sort) == string.lower(string.sub(origlist[i],1,string.len(sort))) then
  405. table.insert(list,origlist[i])
  406. table.insert(goesto,origgoesto[i])
  407. end
  408. end
  409. scroll = 1
  410. paintutils.drawFilledBox(1,3,x,y,colors.black)
  411. drawItems()
  412. drawBar()
  413. end
  414. end
Add Comment
Please, Sign In to add comment