Advertisement
Guest User

FileBrowser

a guest
Aug 30th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.42 KB | None | 0 0
  1. local tArgs = { ... }
  2. local home
  3. local root
  4. local clipboard
  5. if #tArgs > 0 then
  6. home = tArgs[1]
  7. else
  8. home = ""
  9. end
  10. if #tArgs > 1 then
  11. root = tArgs[2]
  12. else
  13. root = "/"
  14. end
  15. history = {}
  16. history[1] = root .. home
  17. local w,h = term.getSize()
  18. local function split(inputstr, sep)
  19. if sep == nil then
  20. sep = "%s"
  21. end
  22. local t={} ; i=1
  23. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  24. t[i] = str
  25. i = i + 1
  26. end
  27. return t
  28. end
  29. local function diabox(txt)
  30. term.setBackgroundColor(colors.white)
  31. term.setTextColor(colors.black)
  32. paintutils.drawFilledBox(math.floor(w/2-(#txt+2)/2),math.floor(h/2)-2,math.floor(w/2+(#txt+2)/2),math.floor(h/2)+2,colors.white)
  33. term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)-1)
  34. term.write(txt)
  35. term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)+1)
  36. end
  37. function readtextbox(txt)
  38. diabox(txt)
  39. return read()
  40. end
  41. function buttonbox(txt,buttons)
  42. if type(buttons) ~= "table" then
  43. buttons = {{" Yes ",colors.red,colors.white},{" No ",colors.gray,colors.white}}
  44. end
  45. if txt == "" or txt == nil then
  46. txt = " Are you sure? "
  47. end
  48. local x,y
  49. diabox(txt)
  50. for i=1,#buttons do
  51. x,y = term.getCursorPos()
  52. x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
  53. term.setCursorPos(x,y)
  54. term.setBackgroundColor(buttons[i][2])
  55. term.setTextColor(buttons[i][3])
  56. term.write(buttons[i][1])
  57. end
  58. while true do
  59. local _,b,x2,y2 = os.pullEvent("mouse_click")
  60. if b == 1 and y == y2 then
  61. for i=1,#buttons do
  62. local x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
  63. if x2 > x - 1 and x2 < x + #buttons[i][1] then
  64. return i
  65. end
  66. end
  67. end
  68. end
  69. end
  70. local function clear()
  71. term.setBackgroundColor(colors.lightBlue)
  72. term.setTextColor(colors.black)
  73. term.clear()
  74. term.setCursorPos(1,1)
  75. end
  76. local function list(path,scroll,selected)
  77. clear()
  78. local items = fs.list(path)
  79. for i=scroll+1,scroll+h-2 do
  80. if type(items[i]) == "string" then
  81. if i ~= selected then
  82. if not fs.isDir(path..items[i]) then
  83. term.setTextColor(colors.black)
  84. else
  85. term.setTextColor(colors.yellow)
  86. end
  87. else
  88. term.setTextColor(colors.white)
  89. end
  90.  
  91. print(items[i])
  92. end
  93.  
  94. end
  95. return items
  96. end
  97. local function drawMenu(path)
  98. term.setBackgroundColor(colors.blue)
  99. term.setTextColor(colors.white)
  100. term.setCursorPos(1,h-1)
  101. term.clearLine()
  102. term.setCursorPos(1,h-1)
  103. term.write(path)
  104. term.setCursorPos(1,h)
  105. term.clearLine()
  106. term.setCursorPos(1,h)
  107. term.write("| New | MkDir | More | Root | Home | Back | Exit |")
  108. end
  109. local function rPrint(txt,y)
  110. if y == nil then _,y = term.getCursorPos() end
  111. term.setCursorPos(w-#txt,y)
  112. term.write(txt)
  113. term.setCursorPos(1,y+1)
  114. end
  115. local function popupMenu(isDir)
  116. paintutils.drawFilledBox(math.floor(w/2),1,w,h-1)
  117. if (isDir) then
  118. rPrint("Open",2)
  119. rPrint("Unpack")
  120. else
  121. rPrint("Edit",2)
  122. rPrint("Paint")
  123. rPrint("Open with ...")
  124. rPrint("Run")
  125. rPrint("Run w/Args")
  126. end
  127. rPrint("Rename",7)
  128. rPrint("Move")
  129. rPrint("Copy")
  130. rPrint("Copy to clipboard")
  131. rPrint("Delete")
  132. rPrint("Deselect")
  133. end
  134. local function createNewName(name,type)
  135. if type == nil then type = 1 end
  136. if type == 1 then return "Unpacked_" .. name end
  137. if type == 2 then return name .. math.random(1,10000) end
  138. return name .. "_pasted"
  139. end
  140. local function findFile(path)
  141. local name = readtextbox("Please enter a keyword")
  142. local results = lookForFile(path,name,{})
  143. return results
  144. end
  145. function lookForFile(path,name,results)
  146. local files = fs.list(path)
  147. for k,v in pairs(files) do
  148. local str = path..v
  149. if type(str:find(name)) == "number" then
  150. results[#results+1] = str
  151. if fs.isDir(str) then results = lookForFile(str.."/",name,results) end
  152. end
  153. end
  154. return results
  155. end
  156. local function drawLine(y,txt,col)
  157. if type(col) == nil then col = colors.white end
  158. term.setCursorPos(1,y)
  159. term.clearLine()
  160. term.setCursorPos(math.floor(w/2-#txt/2),y)
  161. term.write(txt)
  162. end
  163. local function downloadfile(path)
  164. clear()
  165. local url = readtextbox(" Please enter url ")
  166. clear()
  167. print("Progress:")
  168. print("Downloading...")
  169. local httpgot = http.get(url)
  170. print("Done")
  171. local name = readtextbox("Please name the file")
  172. while fs.exists(path..name) do name = createNewName(name,3) end
  173. local file = fs.open(path..name,"w")
  174. file.write(httpgot.readAll())
  175. file.close()
  176. httpgot.close()
  177. clear()
  178. buttonbox("File downloaded",{{"OK",colors.gray,colors.white}})
  179. end
  180. local function more(path)
  181. while true do
  182. clear()
  183. --draw ui
  184. print("More actions:")
  185. term.setTextColor(colors.black)
  186. drawLine(3,"Find file")
  187. drawLine(5,"Download file")
  188. drawLine(7,"Paste from clipboard")
  189. drawLine(9,"Paste from pastebin.com")
  190. drawLine(11,"Open webpage")
  191. drawLine(13,"New file...")
  192. drawLine(15,"Go back")
  193. --get event
  194. local _,_,_,y = os.pullEvent()
  195. if y == 3 then
  196. --find file
  197. local results = findFile(path)
  198. buttonbox(#results.." results saved in current path",{{"OK",colors.gray,colors.white}})
  199. local file = fs.open(path..createNewName("search_results",2),"w")
  200. for k,v in pairs(results) do
  201. file.writeLine(v)
  202. end
  203. file.close()
  204. elseif y == 5 then
  205. downloadfile(path)
  206. elseif y == 7 then
  207. if fs.exists(clipboard) then
  208. if clipboard:sub(#clipboard,#clipboard) == "/" then clipboard = clipboard:sub(1,#clipboard-1) end
  209. local splitted = split(clipboard,"/")
  210. local name = splitted[#splitted]
  211. while fs.exists(path..name) do name = createNewName(name,3) end
  212. fs.copy(clipboard,path..name)
  213. buttonbox("Pasted as "..name.." in directory "..path,{{"OK",colors.gray,colors.white}})
  214. else
  215. buttonbox("File not found: "..clipboard,{{"OK",colors.gray,colors.white}})
  216. end
  217. elseif y == 9 then
  218. --paste from pastebin
  219. local name
  220. repeat
  221. name = readtextbox("Please enter a filename")
  222. until not fs.exists(path..name)
  223. local url = readtextbox("Please enter the pastebin URL")
  224. term.setCursorPos(1,1)
  225. term.setBackgroundColor(colors.white)
  226. term.setTextColor(colors.gray)
  227. term.clear()
  228. print("Downloading file: ")
  229. shell.run("pastebin get "..url.." "..path..name)
  230. print("Ended. Press any key to continue")
  231. os.pullEvent()
  232. elseif y == 11 then
  233. --open webpage:
  234. local url = readtextbox(" Please enter the url ")
  235. local httpsth = http.get(url)
  236. local cont = httpsth.readAll()
  237. httpsth.close()
  238. clear()
  239. print(cont)
  240. os.pullEvent()
  241. elseif y == 13 then
  242. --new file options
  243. clear()
  244. local exc = buttonbox("Please choose a program",{{"Paint",colors.yellow,colors.black},{"Custom",colors.yellow,colors.black}})
  245. clear()
  246. local filename = readtextbox("Please enter a filename")
  247. if not fs.exists(path..filename) then
  248. if exc == 1 then
  249. shell.run("paint",path..filename)
  250. else
  251. clear()
  252. local prog = readtextbox("Please choose a program "..root)
  253. shell.run(prog,path..filename)
  254. end
  255. end
  256. elseif y == 15 then
  257. return
  258. end
  259. end
  260. end
  261. ------------------------------------------------------
  262. --actions:
  263. --folder:
  264. local function openFolder(path,items,selected,history,scroll)
  265. path = path .. items[selected] .. "/"
  266. history[#history+1] = path
  267. return path,history
  268. end
  269. local function unpackFolder(path,items,selected,history,scroll)
  270. if fs.isDir(path..items[selected]) then
  271. local con = fs.list(path..items[selected].."/")
  272. for i=1,#con do
  273. local altname = con[i]
  274. while fs.exists(path..altname) do altname = createNewName(altname) end
  275. fs.move(path..items[selected].."/"..con[i],path..altname)
  276. end
  277. fs.delete(path..items[selected])
  278. end
  279. end
  280. --file:
  281. local function runFile(path,items,selected)
  282. term.setBackgroundColor(colors.black)
  283. term.setTextColor(colors.white)
  284. term.clear()
  285. term.setCursorPos(1,1)
  286. os.pullEvent()
  287. shell.run(path..items[selected])
  288. print("Press any key to continue")
  289. os.pullEvent()
  290. end
  291. local function runFileWArgs(path,items,selected)
  292. local args = readtextbox("Please enter all arguments to run with!")
  293. term.setBackgroundColor(colors.black)
  294. term.setTextColor(colors.white)
  295. term.clear()
  296. term.setCursorPos(1,1)
  297. os.pullEvent()
  298. shell.run(path..items[selected] .. " " .. args)
  299. print("Press any key to continue")
  300. os.pullEvent()
  301. end
  302. local function renamefile(path,items,selected)
  303. local nname = readtextbox("Please enter the new name (empty = cancel)")
  304. if nname ~= nil or nname ~= "" then
  305. if not fs.exists(path..nname) then
  306. fs.move(path..items[selected],path..nname)
  307. clear()
  308. buttonbox("Success",{{"OK",colors.gray,colors.white}})
  309. selected = 0
  310. else
  311. clear()
  312. buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  313. end
  314. end
  315. end
  316. local function moveFile(path,items,selected)
  317. local dest = readtextbox("Move to (empty=cancel) "..root)
  318. if dest ~= nil or dest ~= "" then
  319. if dest:sub(#dest-#items[selected],#dest) ~= items[selected] then
  320. if dest:sub(#dest,#dest) == "/" then dest = dest .. items[selected] else
  321. dest = dest .. "/" .. items[selected]
  322. end
  323. end
  324. if not fs.exists(root..dest) then
  325. fs.move(path..items[selected],root..dest)
  326. clear()
  327. buttonbox("Success",{{"OK",colors.gray,colors.white}})
  328. else
  329. clear()
  330. buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  331. end
  332. end
  333. end
  334. local function copyFile(path,items,selected)
  335. local dest = readtextbox("Move to (empty=cancel) "..root)
  336. if dest ~= nil or dest ~= "" then
  337. if dest:sub(#dest-#items[selected],#dest) ~= items[selected] then
  338. if dest:sub(#dest,#dest) == "/" then dest = dest .. items[selected] else
  339. dest = dest .. "/" .. items[selected]
  340. end
  341. end
  342. if not fs.exists(root..dest) then
  343. fs.copy(path..items[selected],root..dest)
  344. clear()
  345. buttonbox("Success",{{"OK",colors.gray,colors.white}})
  346. else
  347. clear()
  348. buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  349. end
  350. end
  351. end
  352. local function deleteFile(path)
  353. local bclicked = buttonbox()
  354. if bclicked == 1 then
  355. fs.delete(path)
  356. clear()
  357. buttonbox("File deleted",{{"OK",colors.gray,colors.white}})
  358. selected = 0
  359. end
  360. end
  361. local function newFile(path)
  362. local name = readtextbox("Filename: (empty=cancel) ")
  363. if name ~= nil or name ~= "" then
  364. if not fs.exists(path..name) then
  365. shell.run("edit",path..name)
  366. end
  367. end
  368. end
  369. local function newDir(path)
  370. local name = readtextbox("Directory's name: (empty=cancel)")
  371. if name ~= nil or name ~= "" then
  372. if not fs.exists(path..name) then
  373. fs.makeDir(path..name)
  374. end
  375. end
  376. end
  377. ------------------------------------------------------
  378. local path = root .. home
  379. local scroll = 0
  380. local selected = 0
  381. local items
  382. local endprogram = false
  383. while not endprogram do
  384. repeat
  385. items = list(path,scroll,selected)
  386. drawMenu(path)
  387. if items[selected] ~= nil then
  388. popupMenu(fs.isDir(path..items[selected]))
  389. end
  390. local e,b,x,y = os.pullEvent()
  391. if e == "mouse_click" then
  392. if items[selected] ~= nil and x > math.floor(w/2) and y < h-1 then
  393. if fs.isDir(path..items[selected]) then
  394. --folder specific
  395. if y == 2 then
  396. path,history = openFolder(path,items,selected,history,scroll)
  397. selected = 0
  398. scroll = 0
  399. end
  400. if y == 3 then
  401. unpackFolder(path,items,selected,history,scroll)
  402. selected = 0
  403. end
  404. else
  405. --file specific
  406. if y == 2 then
  407. --edit
  408. shell.run("edit",path.. items[selected])
  409. elseif y == 3 then
  410. --open with paint
  411. shell.run("paint",path..items[selected])
  412. elseif y == 4 then
  413. --open with...
  414. local program = readtextbox("Please choose a program: "..root)
  415. if program ~= nil or program ~= "" then
  416. shell.run(root..program,path..items[selected])
  417. end
  418. elseif y == 5 then
  419. runFile(path,items,selected)
  420. elseif y == 6 then
  421. runFileWArgs(path,items,selected)
  422. end
  423.  
  424. end
  425. --universal
  426. if y == 7 then
  427. renamefile(path,items,selected)
  428. end
  429. if y == 8 then
  430. moveFile(path,items,selected)
  431. end
  432. if y == 9 then
  433. copyFile(path,items,selected)
  434. end
  435. if y == 10 then
  436. --clipboard
  437. clipboard = path .. items[selected]
  438. end
  439. if y == 11 then
  440. deleteFile(path..items[selected])
  441. end
  442. if y == 12 then
  443. selected = 0
  444. end
  445. break
  446. end
  447. if y == h-1 then
  448. local npath = readtextbox("Enter new path: (empty=cancel) "..root)
  449.  
  450. if npath ~= nil and npath ~= "" and fs.isDir(root..npath) then
  451. path = root .. npath
  452. selected = 0
  453. scroll = 0
  454. end
  455. history[#history+1] = path
  456. elseif y == h then
  457. --new
  458. if x > 1 and x < 7 then
  459. newFile(path)
  460. end
  461. if x > 7 and x < 15 then
  462. newDir(path)
  463. end
  464. if x > 15 and x < 22 then
  465. --more...
  466. more(path)
  467. end
  468. if x > 22 and x < 29 then
  469. path = root
  470. history[#history+1] = path
  471. selected = 0
  472. scroll = 0
  473. end
  474. if x > 29 and x < 36 then
  475. if fs.exists(root .. home) then
  476. path = root .. home
  477. history[#history+1] = path
  478. selected = 0
  479. scroll = 0
  480. end
  481. end
  482. if x > 36 and x < 43 then
  483. while true do
  484. history[#history] = nil
  485. if #history == 0 then
  486. if fs.exists(root..home) then
  487. path = root..home
  488. history[1] = path
  489. elseif fs.exists(root) then
  490. path = root
  491. history[1] = path
  492. else
  493. error("Root folder deleted",0)
  494. end
  495. end
  496. if fs.exists(history[#history]) then
  497. path = history[#history]
  498. break
  499. end
  500. end
  501. selected = 0
  502. end
  503. if x > 43 and x < 50 then
  504. term.setBackgroundColor(colors.black)
  505. term.setTextColor(colors.white)
  506. term.setCursorPos(1,1)
  507. term.clear()
  508. return
  509. end
  510. else
  511. if y + scroll == selected then
  512. if items[selected] ~= nil then
  513. if fs.isDir(path..items[selected]) then
  514. path = path .. items[selected] .. "/"
  515. history[#history+1] = path
  516. selected = 0
  517. scroll = 0
  518. else
  519. term.setBackgroundColor(colors.black)
  520. term.setTextColor(colors.white)
  521. term.setCursorPos(1,1)
  522. term.clear()
  523. shell.run(path..items[selected])
  524. end
  525. end
  526. else
  527. selected = y + scroll
  528. end
  529. end
  530. elseif e == "mouse_scroll" then
  531. if b == 1 and #items - scroll > h-2 then
  532. scroll = scroll + 1
  533. end
  534. if b == -1 and scroll > 0 then
  535. scroll = scroll - 1
  536. end
  537. ---------------------------------------------------
  538. --hotkeys:
  539. elseif e == "key" then
  540. if b == keys.f1 then
  541. clear()
  542. print("Hotkey help:")
  543. print([[Up/Down arrow keys + Page Up/Down = navigation
  544. Insert = new file
  545. Delete = delete file
  546. Backspace = exit
  547. Enter = Open/Run
  548. M = move
  549. C = copy
  550. R = rename
  551. 0 = deselect
  552. Press any key to go back]])
  553. os.pullEvent()
  554. end
  555. if b == keys.insert then
  556. newFile(path)
  557. end
  558. if b == keys.delete then
  559. if selected ~= 0 then
  560. deleteFile(path..items[selected])
  561. else
  562. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  563. end
  564. end
  565. if b == keys.backspace then
  566. term.setBackgroundColor(colors.black)
  567. term.setTextColor(colors.white)
  568. term.setCursorPos(1,1)
  569. term.clear()
  570. return
  571. end
  572. if b == keys.enter then
  573. if selected ~= 0 then
  574. if fs.isDir(path..items[selected]) then
  575. openFolder(path,items,selected,history,scroll)
  576. else
  577. runFile(path..items[selected])
  578. end
  579. else
  580. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  581. end
  582. end
  583. if b == keys.m then
  584. if selected ~= 0 then
  585. moveFile(path,items,selected)
  586. else
  587. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  588. end
  589. end
  590. if b == keys.c then
  591. if selected ~= 0 then
  592. copyFile(path,items,selected)
  593. else
  594. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  595. end
  596. end
  597. if b == keys.r then
  598. if selected ~= 0 then
  599. renamefile(path,items,selected)
  600. else
  601. buttonbox("Please select a file",{{"OK",colors.gray,colors.white}})
  602. end
  603. end
  604. if b == keys.zero then
  605. selected = 0
  606. end
  607. if b == keys.up and selected > 1 then
  608. selected = selected - 1
  609. if selected < scroll+1 then
  610. scroll = scroll - 1
  611. end
  612. end
  613. if b == keys.down and selected < #items then
  614. selected = selected + 1
  615. if selected > (scroll + h-3) then
  616. scroll = scroll + 1
  617. end
  618. end
  619. if b == keys.pageUp and selected then
  620. selected = selected - (h-3)
  621. scroll = selected-1
  622. if selected < 0 or scroll < 0 then scroll,selected = 0,1 end
  623. end
  624. if b == keys.pageDown and selected < #items-(h-2) then
  625. selected = selected + (h-3)
  626. scroll = selected-1
  627. if scroll > #items-(h-2) or selected > #items then scroll,selected = #items-(h-2),#items end
  628. if selected < 0 then scroll,selected = 0,0 end
  629. end
  630. end
  631. until true
  632. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement