Advertisement
LDDestroier

filebrowser whitespaced

Aug 16th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. historyclear = function()
  30.     history = {}
  31.     history[1] = root..home
  32. end
  33. shellcom = {historyclear}
  34. local function diabox(txt)
  35.     term.setBackgroundColor(colors.white)
  36.     term.setTextColor(colors.black)
  37.     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)
  38.     term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)-1)
  39.     term.write(txt)
  40.     term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)+1)
  41. end
  42. function readtextbox(txt)
  43.     diabox(txt)
  44.     return read()
  45. end
  46. function buttonbox(txt,buttons)
  47.     if type(buttons) ~= "table" then
  48.         buttons = {{" Yes ",colors.red,colors.white},{" No ",colors.gray,colors.white}}
  49.     end
  50.     if txt == "" or txt == nil then
  51.         txt = " Are you sure? "
  52.     end
  53.     local x,y
  54.     diabox(txt)
  55.     for i=1,#buttons do
  56.         x,y = term.getCursorPos()
  57.         x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
  58.         term.setCursorPos(x,y)
  59.         term.setBackgroundColor(buttons[i][2])
  60.         term.setTextColor(buttons[i][3])
  61.         term.write(buttons[i][1])    
  62.     end
  63.     while true do
  64.         local _,b,x2,y2 = os.pullEvent("mouse_click")
  65.         if b == 1 and y == y2 then
  66.             for i=1,#buttons do
  67.                 local x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
  68.                 if x2 > x - 1 and x2 < x + #buttons[i][1] then
  69.                     return i
  70.                 end
  71.             end
  72.         end
  73.     end
  74. end
  75. local function clear()
  76.     term.setBackgroundColor(colors.lightBlue)
  77.     term.setTextColor(colors.black)
  78.     term.clear()
  79.     term.setCursorPos(1,1)
  80. end
  81. local function list(path,scroll,selected)
  82.     clear()
  83.     local items = fs.list(path)
  84.     for i=scroll+1,scroll+h-2 do
  85.         if type(items[i]) == "string" then
  86.             if i ~= selected then
  87.                 if not fs.isDir(path..items[i]) then
  88.                     term.setTextColor(colors.black)
  89.                 else
  90.                     term.setTextColor(colors.yellow)
  91.                 end
  92.             else
  93.                 term.setTextColor(colors.white)
  94.             end
  95.             print(items[i])
  96.         end
  97.     end
  98.     return items
  99. end
  100. local function drawMenu(path)
  101.     term.setBackgroundColor(colors.blue)
  102.     term.setTextColor(colors.white)
  103.     term.setCursorPos(1,h-1)
  104.     term.clearLine()
  105.     term.setCursorPos(1,h-1)
  106.     term.write(path)
  107.     term.setCursorPos(1,h)
  108.     term.clearLine()
  109.     term.setCursorPos(1,h)
  110.     term.write("| New | MkDir | More | Root | Home | Back | Exit |")
  111. end
  112. local function rPrint(txt,y)
  113.     if y == nil then _,y = term.getCursorPos() end
  114.     term.setCursorPos(w-#txt,y)
  115.     term.write(txt)
  116.     term.setCursorPos(1,y+1)
  117. end
  118. local function popupMenu(isDir)
  119.     paintutils.drawFilledBox(math.floor(w/2),1,w,h-1)
  120.     if (isDir) then
  121.         rPrint("Open",2)
  122.         rPrint("Unpack")
  123.     else
  124.         rPrint("Edit",2)
  125.         rPrint("Paint")
  126.         rPrint("Open with ...")
  127.         rPrint("Run")
  128.         rPrint("Run w/Args")
  129.     end
  130.     rPrint("Rename",7)
  131.     rPrint("Move")
  132.     rPrint("Copy")
  133.     rPrint("Copy to clipboard")
  134.     rPrint("Delete")
  135.     rPrint("Deselect")
  136. end
  137. local function createNewName(name,type)
  138.     if type == nil then type = 1 end
  139.     if type == 1 then return "Unpacked_" .. name end
  140.     if type == 2 then return name .. math.random(1,10000) end
  141.     return name .. "_pasted"
  142. end
  143. local function drawLine(y,txt,col)
  144.     if type(col) == nil then col = colors.white end
  145.     term.setCursorPos(1,y)
  146.     term.clearLine()
  147.     term.setCursorPos(math.floor(w/2-#txt/2),y)
  148.     term.write(txt)
  149. end
  150. local function customShell()
  151.     clear()
  152.     while true do
  153.         term.setCursorPos(1,h)
  154.         term.clearLine()
  155.         term.setCursorPos(1,h)
  156.         term.write("? ")
  157.         local command = read()
  158.         clear()
  159.         term.setCursorPos(1,1)
  160.         if command == "exit" then return end
  161.         if type(shellcom[command]) == "function" then
  162.             shellcom[command]()
  163.         else
  164.             print("Not a command")
  165.         end
  166.     end
  167. end
  168. local function downloadfile(path)
  169.     clear()
  170.     local url = readtextbox("      Please enter url      ")
  171.     clear()
  172.     print("Progress:")
  173.     print("Downloading...")
  174.     local httpgot = http.get(url)
  175.     print("Done")
  176.     local name = readtextbox("Please name the file")
  177.     while fs.exists(path..name) do name = createNewName(name,3) end
  178.     local file = fs.open(path..name,"w")
  179.     file.write(httpgot.readAll())
  180.     file.close()
  181.     httpgot.close()
  182.     clear()
  183.     buttonbox("File downloaded",{{"OK",colors.gray,colors.white}})
  184. end
  185. local function more(path) --draw ui
  186.     while true do
  187.         clear()
  188.         print("More actions:")
  189.         term.setTextColor(colors.black)
  190.         drawLine(3,"Shell")
  191.         drawLine(5,"Download file")
  192.         drawLine(7,"Paste from clipboard")
  193.         drawLine(9,"Paste from pastebin.com")
  194.         drawLine(11,"Open webpage")
  195.         drawLine(13,"New file...")
  196.         drawLine(15,"Go back")
  197.         --get event
  198.         local _,_,_,y = os.pullEvent()
  199.         if y == 3 then
  200.             customShell()
  201.         elseif y == 5 then
  202.             downloadfile(path)
  203.         elseif y == 7 then
  204.             if fs.exists(clipboard) then
  205.                 if clipboard:sub(#clipboard,#clipboard) == "/" then clipboard = clipboard:sub(1,#clipboard-1) end
  206.                 local splitted = split(clipboard,"/")
  207.                 local name = splitted[#splitted]
  208.                 while fs.exists(path..name) do name = createNewName(name,3) end
  209.                 fs.copy(clipboard,path..name)
  210.                 buttonbox("Pasted as "..name.." in directory "..path,{{"OK",colors.gray,colors.white}})
  211.             else
  212.                 buttonbox("File not found: "..clipboard,{{"OK",colors.gray,colors.white}})
  213.             end
  214.         elseif y == 9 then
  215.             --paste from pastebin
  216.             local name
  217.             repeat
  218.                 name = readtextbox("Please enter a filename")
  219.             until not fs.exists(path..name)
  220.             local url = readtextbox("Please enter the pastebin URL")
  221.             term.setCursorPos(1,1)
  222.             term.setBackgroundColor(colors.white)
  223.             term.setTextColor(colors.gray)
  224.             term.clear()
  225.             print("Downloading file: ")
  226.             shell.run("pastebin get "..url.." "..path..name)
  227.             print("Ended. Press any key to continue")
  228.             os.pullEvent()
  229.             elseif y == 11 then
  230.             --open webpage:
  231.             local url = readtextbox("   Please enter the url   ")
  232.             local httpsth = http.get(url)
  233.             local cont = httpsth.readAll()
  234.             httpsth.close()
  235.             clear()
  236.             print(cont)
  237.             os.pullEvent()
  238.         elseif y == 13 then
  239.             --new file options
  240.             clear()
  241.             local exc = buttonbox("Please choose a program",{{"Paint",colors.yellow,colors.black},{"Custom",colors.yellow,colors.black}})
  242.             clear()
  243.             local filename = readtextbox("Please enter a filename")
  244.             if not fs.exists(path..filename) then
  245.                 if exc == 1 then
  246.                     shell.run("paint",path..filename)
  247.                 else
  248.                     clear()
  249.                     local prog = readtextbox("Please choose a program "..root)
  250.                     shell.run(prog,path..filename)
  251.                 end
  252.             end
  253.         elseif y == 15 then
  254.             return
  255.         end
  256.     end
  257. end
  258. local path = root .. home
  259. local scroll = 0
  260. local selected = 0
  261. local items
  262. local endprogram = false
  263. while not endprogram do
  264.     repeat
  265.         items = list(path,scroll,selected)
  266.         drawMenu(path)
  267.         if items[selected] ~= nil then popupMenu(fs.isDir(path..items[selected])) end
  268.         local e,b,x,y = os.pullEvent()
  269.         if e == "mouse_click" then
  270.             if items[selected] ~= nil and x > math.floor(w/2) and y < h-1 then
  271.                 if fs.isDir(path..items[selected]) then
  272.                     --folder specific
  273.                     if y == 2 then
  274.                         path = path .. items[selected] .. "/"
  275.                         selected = 0
  276.                         scroll = 0
  277.                         history[#history+1] = path
  278.                     elseif y == 3 then
  279.                         local con = fs.list(path..items[selected].."/")
  280.                         for i=1,#con do
  281.                             local altname = con[i]
  282.                             while fs.exists(path..altname) do
  283.                                 altname = createNewName(altname)
  284.                             end
  285.                             fs.move(path..items[selected].."/"..con[i],path..altname)      
  286.                         end
  287.                         fs.delete(path..items[selected])
  288.                         selected = 0
  289.                     end
  290.                 else
  291.                     --file specific
  292.                     if y == 2 then
  293.                         --edit
  294.                         shell.run("edit",path.. items[selected])
  295.                     elseif y == 3 then
  296.                         --open with paint
  297.                         shell.run("paint",path..items[selected])
  298.                     elseif y == 4 then
  299.                         --open with...
  300.                         local program = readtextbox("Please choose a program: "..root)
  301.                         if program ~= nil or program ~= "" then
  302.                             shell.run(root..program,path..items[selected])  
  303.                         end      
  304.                     elseif y == 5 then
  305.                         term.setBackgroundColor(colors.black)
  306.                         term.setTextColor(colors.white)
  307.                         term.clear()
  308.                         term.setCursorPos(1,1)
  309.                         os.pullEvent()
  310.                         shell.run(path..items[selected])
  311.                         sleep(0)
  312.                         print("Press any key to continue")
  313.                         os.pullEvent("key")
  314.                     elseif y == 6 then
  315.                         local args = readtextbox("Please enter all arguments to run with!")
  316.                         term.setBackgroundColor(colors.black)
  317.                         term.setTextColor(colors.white)
  318.                         term.clear()
  319.                         term.setCursorPos(1,1)
  320.                         os.pullEvent()
  321.                         shell.run(path..items[selected] .. " " .. args)
  322.                         sleep(0)
  323.                         print("Press any key to continue")
  324.                         os.pullEvent("key")
  325.                     end
  326.                 end
  327.                 --universal
  328.                 if y == 7 then
  329.                     local nname = readtextbox("Please enter the new name (empty = cancel)")
  330.                     if nname ~= nil or nname ~= "" then
  331.                         if not fs.exists(path..nname) then
  332.                             fs.move(path..items[selected],path..nname)
  333.                             clear()
  334.                             buttonbox("Success",{{"OK",colors.gray,colors.white}})
  335.                             selected = 0
  336.                         else
  337.                             clear()
  338.                             buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  339.                         end
  340.                     end
  341.                 elseif y == 8 then
  342.                     --move
  343.                     local dest = readtextbox("Move to (empty=cancel) "..root)    
  344.                     if dest ~= nil or dest ~= "" then
  345.                         if not fs.exists(root..dest) then
  346.                             fs.move(path..items[selected],root..dest)
  347.                             clear()
  348.                             buttonbox("Success",{{"OK",colors.gray,colors.white}})
  349.                         else
  350.                             clear()
  351.                             buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  352.                         end
  353.                     end
  354.                 elseif y == 9 then
  355.                     --copy
  356.                     local destination = readtextbox("Copy to (emplty=cancel) "..root)
  357.                     if destination ~= nil or destination ~= "" then
  358.                         if not fs.exists(root..destination) then
  359.                             fs.copy(path..items[selected],root..destination)
  360.                             clear()
  361.                             buttonbox("Success",{{"OK",colors.gray,colors.white}})
  362.                         else
  363.                             clear()
  364.                             buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  365.                         end
  366.                     end
  367.                 elseif y == 10 then
  368.                     --clipboard
  369.                     clipboard = path .. items[selected]
  370.                 elseif y == 11 then
  371.                     --delete
  372.                     local bclicked = buttonbox()
  373.                     if bclicked == 1 then
  374.                         fs.delete(path..items[selected])
  375.                         clear()
  376.                         buttonbox("File deleted",{{"OK",colors.gray,colors.white}})
  377.                         selected = 0
  378.                     end
  379.                 elseif y == 12 then
  380.                     selected = 0
  381.                 end
  382.                 break
  383.             end
  384.             if y == h-1 then
  385.                 local npath = readtextbox("Enter new path: (empty=cancel) "..root)
  386.                 if npath ~= nil and npath ~= "" and fs.isDir(root..npath) then
  387.                     path = root .. npath
  388.                     selected = 0
  389.                     scroll = 0
  390.                 end
  391.                 history[#history+1] = path
  392.             elseif y == h then
  393.                 --new
  394.                 if x > 1 and x < 7 then
  395.                     local name = readtextbox("Filename: (empty=cancel)  ")
  396.                     if name ~= nil or name ~= "" then
  397.                         if not fs.exists(path..name) then
  398.                             shell.run("edit",path..name)
  399.                         end
  400.                     end
  401.                 end
  402.                 if x > 7 and x < 15 then
  403.                     local name = readtextbox("Directory's name: (empty=cancel)")    
  404.                     if name ~= nil or name ~= "" then
  405.                         if not fs.exists(path..name) then
  406.                             fs.makeDir(path..name)
  407.                         end
  408.                     end
  409.                 end
  410.                 if x > 15 and x < 22 then
  411.                     --more...
  412.                     more(path)
  413.                 end
  414.                 if x > 22 and x < 29 then
  415.                     path = root
  416.                     history[#history+1] = path
  417.                     selected = 0
  418.                     scroll = 0
  419.                 end
  420.                 if (x > 29 and x < 36) and fs.exists(root .. home) then
  421.                     path = root .. home
  422.                     history[#history+1] = path
  423.                     selected = 0
  424.                     scroll = 0
  425.                 end
  426.                 if x > 36 and x < 43 then
  427.                     while true do
  428.                         history[#history] = nil
  429.                         if #history == 0 then
  430.                             if fs.exists(root..home) then
  431.                                 path = root..home
  432.                                 history[1] = path
  433.                             elseif fs.exists(root) then
  434.                                 path = root
  435.                                 history[1] = path
  436.                             else
  437.                                 error("Root folder deleted",0)
  438.                             end
  439.                         end
  440.                         if fs.exists(history[#history]) then
  441.                             path = history[#history]
  442.                             break
  443.                         end
  444.                     end
  445.                     selected = 0
  446.                 end
  447.                 if x > 43 and x < 50 then
  448.                     term.setBackgroundColor(colors.black)
  449.                     term.setTextColor(colors.white)
  450.                     term.setCursorPos(1,1)
  451.                     term.clear()
  452.                     return
  453.                 end
  454.             elseif e == "mouse_scroll" then
  455.                 if b == 1 and #items - scroll > h-2 then
  456.                     scroll = scroll + 1
  457.                 end
  458.                 if b == -1 and scroll > 0 then
  459.                     scroll = scroll - 1
  460.                 end
  461.             else
  462.                 if y + scroll == selected then
  463.                     if items[selected] ~= nil then
  464.                         if fs.isDir(path..items[selected]) then
  465.                             path = path .. items[selected] .. "/"
  466.                             history[#history+1] = path
  467.                             selected = 0
  468.                             scroll = 0
  469.                         else
  470.                             term.setBackgroundColor(colors.black)
  471.                             term.setTextColor(colors.white)
  472.                             term.setCursorPos(1,1)
  473.                             term.clear()
  474.                             shell.run(path..items[selected])
  475.                         end
  476.                     end
  477.                 else
  478.                     selected = y + scroll
  479.                 end
  480.             end
  481.         end
  482.     until true
  483. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement