Guest User

file_browser

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