Advertisement
Guest User

fb

a guest
Aug 16th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.57 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. 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.    
  96.     print(items[i])
  97.     end
  98.    
  99.   end
  100.   return items
  101. end
  102. local function drawMenu(path)
  103.   term.setBackgroundColor(colors.blue)
  104.   term.setTextColor(colors.white)
  105.   term.setCursorPos(1,h-1)
  106.   term.clearLine()
  107.   term.setCursorPos(1,h-1)
  108.   term.write(path)
  109.   term.setCursorPos(1,h)
  110.   term.clearLine()
  111.   term.setCursorPos(1,h)
  112.   term.write("| New | MkDir | More | Root | Home | Back | Exit |")
  113. end
  114. local function rPrint(txt,y)
  115.   if y == nil then _,y = term.getCursorPos() end
  116.   term.setCursorPos(w-#txt,y)
  117.   term.write(txt)
  118.   term.setCursorPos(1,y+1)
  119. end
  120. local function popupMenu(isDir)
  121.   paintutils.drawFilledBox(math.floor(w/2),1,w,h-1)
  122.   if (isDir) then
  123.     rPrint("Open",2)
  124.     rPrint("Unpack")
  125.   else
  126.     rPrint("Edit",2)
  127.     rPrint("Paint")
  128.     rPrint("Open with ...")
  129.     rPrint("Run")
  130.     rPrint("Run w/Args")
  131.   end
  132.     rPrint("Rename",7)
  133.     rPrint("Move")
  134.     rPrint("Copy")
  135.     rPrint("Copy to clipboard")
  136.     rPrint("Delete")
  137.     rPrint("Deselect")
  138.   end
  139. local function createNewName(name,type)
  140.   if type == nil then type = 1 end
  141.   if type == 1 then return "Unpacked_" .. name end
  142.   if type == 2 then return name .. math.random(1,10000) end
  143.   return name .. "_pasted"
  144. end
  145. local function drawLine(y,txt,col)
  146.   if type(col) == nil then col = colors.white end
  147.   term.setCursorPos(1,y)
  148.   term.clearLine()
  149.   term.setCursorPos(math.floor(w/2-#txt/2),y)
  150.   term.write(txt)
  151. end
  152. local function customShell()
  153. clear()
  154.   while true do
  155.     term.setCursorPos(1,h)
  156.     term.clearLine()
  157.     term.setCursorPos(1,h)
  158.     term.write("? ")
  159.     local command = read()
  160.     clear()
  161.     term.setCursorPos(1,1)
  162.     if command == "exit" then return end
  163.     if type(shellcom[command]) == "function" then
  164.       shellcom[command]()
  165.     else
  166.       print("Not a command")
  167.     end
  168.   end
  169. end
  170. local function downloadfile(path)
  171.   clear()
  172.   local url = readtextbox("      Please enter url      ")
  173.   clear()
  174.   print("Progress:")
  175.   print("Downloading...")
  176.   local httpgot = http.get(url)
  177.   print("Done")
  178.   local name = readtextbox("Please name the file")
  179.   while fs.exists(path..name) do name = createNewName(name,3) end
  180.   local file = fs.open(path..name,"w")
  181.   file.write(httpgot.readAll())
  182.   file.close()
  183.   httpgot.close()
  184.   clear()
  185.   buttonbox("File downloaded",{{"OK",colors.gray,colors.white}})
  186. end
  187. local function more(path)
  188. while true do
  189.   clear()
  190.   --draw ui
  191.   print("More actions:")
  192.   term.setTextColor(colors.black)
  193.   drawLine(3,"Shell")
  194.   drawLine(5,"Download file")
  195.   drawLine(7,"Paste from clipboard")
  196.   drawLine(9,"Paste from pastebin.com")
  197.   drawLine(11,"Open webpage")
  198.   drawLine(13,"New file...")
  199.   drawLine(15,"Go back")
  200.   --get event
  201.   local _,_,_,y = os.pullEvent()
  202.   if y == 3 then
  203.     customShell()
  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. local path = root .. home
  262. local scroll = 0
  263. local selected = 0
  264. local items
  265. local endprogram = false
  266. while not endprogram do
  267. repeat
  268.     items = list(path,scroll,selected)
  269.     drawMenu(path)
  270.    if items[selected] ~= nil then
  271.      popupMenu(fs.isDir(path..items[selected]))
  272.    end
  273.   local e,b,x,y = os.pullEvent()
  274.   if e == "mouse_click" then
  275.   if items[selected] ~= nil and x > math.floor(w/2) and y < h-1 then
  276.    if fs.isDir(path..items[selected]) then
  277.      --folder specific
  278.      if y == 2 then
  279.        path = path .. items[selected] .. "/"
  280.        selected = 0
  281.        scroll = 0
  282.        history[#history+1] = path
  283.      end
  284.      if y == 3 then
  285.        local con = fs.list(path..items[selected].."/")
  286.        for i=1,#con do
  287.          local altname = con[i]
  288.          while fs.exists(path..altname) do altname = createNewName(altname) end
  289.          fs.move(path..items[selected].."/"..con[i],path..altname)      
  290.        end
  291.        fs.delete(path..items[selected])
  292.        selected = 0
  293.      end
  294.    else
  295.      --file specific
  296.      if y == 2 then
  297.        --edit
  298.        shell.run("edit",path.. items[selected])
  299.      elseif y == 3 then
  300.        --open with paint
  301.        shell.run("paint",path..items[selected])
  302.      elseif y == 4 then
  303.       --open with...
  304.        local program = readtextbox("Please choose a program: "..root)
  305.         if program ~= nil or program ~= "" then
  306.           shell.run(root..program,path..items[selected])  
  307.         end      
  308.      elseif y == 5 then
  309.        term.setBackgroundColor(colors.black)
  310.        term.setTextColor(colors.white)
  311.        term.clear()
  312.        term.setCursorPos(1,1)
  313.        os.pullEvent()
  314.        shell.run(path..items[selected])
  315.        print("Press any key to continue")
  316.        os.pullEvent()
  317.      elseif y == 6 then
  318.        local args = readtextbox("Please enter all arguments to run with!")
  319.        term.setBackgroundColor(colors.black)
  320.        term.setTextColor(colors.white)
  321.        term.clear()
  322.        term.setCursorPos(1,1)
  323.        os.pullEvent()
  324.        shell.run(path..items[selected] .. " " .. args)
  325.        print("Press any key to continue")
  326.        os.pullEvent()
  327.      end
  328.  
  329.    end
  330.    --universal
  331.    if y == 7 then
  332.      local nname = readtextbox("Please enter the new name (empty = cancel)")
  333.      if nname ~= nil or nname ~= "" then
  334.        if not fs.exists(path..nname) then
  335.        fs.move(path..items[selected],path..nname)
  336.        clear()
  337.        buttonbox("Success",{{"OK",colors.gray,colors.white}})
  338.        selected = 0
  339.        else
  340.        clear()
  341.         buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  342.        end
  343.      end
  344.    end
  345.    if y == 8 then
  346.      --move
  347.      local dest = readtextbox("Move to (empty=cancel) "..root)    
  348.      if dest ~= nil or dest ~= "" then
  349.        if not fs.exists(root..dest) then
  350.        fs.move(path..items[selected],root..dest)
  351.        clear()
  352.        buttonbox("Success",{{"OK",colors.gray,colors.white}})
  353.        else
  354.        clear()
  355.        buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  356.        end
  357.      end
  358.    end
  359.    if y == 9 then
  360.      --copy
  361.      local destination = readtextbox("Copy to (emplty=cancel) "..root)
  362.      if destination ~= nil or destination ~= "" then
  363.        if not fs.exists(root..destination) then
  364.        fs.copy(path..items[selected],root..destination)
  365.        clear()
  366.        buttonbox("Success",{{"OK",colors.gray,colors.white}})
  367.        else
  368.        clear()
  369.          buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
  370.        end
  371.      end
  372.    end
  373.    if y == 10 then
  374.      --clipboard
  375.      clipboard = path .. items[selected]
  376.    end
  377.    if y == 11 then
  378.      --delete
  379.       local bclicked = buttonbox()
  380.       if bclicked == 1 then
  381.         fs.delete(path..items[selected])
  382.         clear()
  383.         buttonbox("File deleted",{{"OK",colors.gray,colors.white}})
  384.         selected = 0
  385.       end
  386.    end
  387.    if y == 12 then
  388.      selected = 0
  389.    end
  390.    break
  391.   end
  392.   if y == h-1 then
  393.     local npath = readtextbox("Enter new path: (empty=cancel) "..root)
  394.    
  395.     if npath ~= nil and npath ~= "" and fs.isDir(root..npath) then
  396.       path = root .. npath
  397.       selected = 0
  398.       scroll = 0
  399.     end
  400.     history[#history+1] = path
  401.   elseif y == h then
  402.     --new
  403.     if x > 1 and x < 7 then
  404.       local name = readtextbox("Filename: (empty=cancel)  ")
  405.       if name ~= nil or name ~= "" then
  406.         if not fs.exists(path..name) then
  407.         shell.run("edit",path..name)
  408.         end
  409.       end
  410.     end
  411.     if x > 7 and x < 15 then
  412.       local name = readtextbox("Directory's name: (empty=cancel)")    
  413.       if name ~= nil or name ~= "" then
  414.         if not fs.exists(path..name) then
  415.         fs.makeDir(path..name)
  416.         end
  417.       end
  418.     end
  419.     if x > 15 and x < 22 then
  420.     --more...
  421.     more(path)
  422.     end
  423.     if x > 22 and x < 29 then
  424.     path = root
  425.     history[#history+1] = path
  426.     selected = 0
  427.     scroll = 0
  428.     end
  429.     if x > 29 and x < 36 then
  430.     if fs.exists(root .. home) then
  431.     path = root .. home
  432.     history[#history+1] = path
  433.     selected = 0
  434.     scroll = 0
  435.     end
  436.     end
  437.     if x > 36 and x < 43 then
  438.       while true do
  439.         history[#history] = nil
  440.         if #history == 0 then
  441.           if fs.exists(root..home) then
  442.             path = root..home
  443.             history[1] = path
  444.           elseif fs.exists(root) then
  445.             path = root
  446.             history[1] = path
  447.           else
  448.             error("Root folder deleted",0)
  449.           end
  450.         end
  451.         if fs.exists(history[#history]) then
  452.           path = history[#history]
  453.           break
  454.         end
  455.       end
  456.       selected = 0
  457.     end
  458.     if x > 43 and x < 50 then
  459.     term.setBackgroundColor(colors.black)
  460.     term.setTextColor(colors.white)
  461.     term.setCursorPos(1,1)
  462.     term.clear()
  463.     return
  464.     end
  465.   else
  466.     if y + scroll == selected then
  467.        if items[selected] ~= nil then
  468.         if fs.isDir(path..items[selected]) then
  469.           path = path .. items[selected] .. "/"
  470.           history[#history+1] = path
  471.           selected = 0
  472.           scroll = 0
  473.         else
  474.           term.setBackgroundColor(colors.black)
  475.           term.setTextColor(colors.white)
  476.           term.setCursorPos(1,1)
  477.           term.clear()
  478.           shell.run(path..items[selected])
  479.         end
  480.        end
  481.     else
  482.       selected =  y + scroll
  483.     end
  484.   end
  485.   elseif e == "mouse_scroll" then
  486.     if b == 1 and #items - scroll > h-2 then
  487.       scroll = scroll + 1
  488.     end
  489.     if b == -1 and scroll > 0 then
  490.       scroll = scroll - 1
  491.     end
  492.   end
  493. until true
  494. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement