Advertisement
Guest User

explorer

a guest
Jul 24th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.54 KB | None | 0 0
  1. local fol
  2. local prog
  3. local dd = false
  4. local dx, dy = term.getSize()
  5. local directory = "/"
  6. local folders = {}
  7. local programs = {}
  8. local i
  9. local selected
  10. local scroll = 0
  11. local scrollt = 0
  12. local var = 0
  13. local varI = 1
  14. local srch = false
  15. local button = {}
  16. local btns
  17. local rlib = {}
  18.  
  19. button.clear = function()
  20.     btns = {}
  21.     return true
  22. end
  23. button.add = function(par2, par3, par4 ,par5 ,par6)
  24.     if par3 == nil or par4 == nil or par5 == nil or par6 == nil then--there is a more efficient way of doing this but I don't know it
  25.         return false
  26.     else
  27.         btns[#btns+1] = {par2, math.ceil(par3), math.ceil(par4), math.ceil(par5), math.ceil(par6)}
  28.     end
  29.     return true, #btns
  30. end
  31. button.wait = function()
  32.     if #btns == 0 then
  33.         return false
  34.     end
  35.     while true do
  36.         eve, but, x, y = os.pullEvent("mouse_click")
  37.         for i = 1, #btns do
  38.             local sel = btns[i]
  39.             if sel[1]==but or sel[1] == nil then
  40.                 if eve == "mouse_click" and x >= sel[2] and y >= sel[3] and x <= sel[4] and y <= sel[5] then
  41.                     return i, but
  42.                 end
  43.             end
  44.         end
  45.     end
  46. end
  47. button.list = function()
  48.     return btns
  49. end
  50. rlib.search = function(search, dir)
  51.   if search == nil then
  52.     search = textBox('Search Item')
  53.   end
  54.   local directory = dir or directory
  55.   local fls = fs.list(directory)
  56.   local all2 = {}
  57.   for i = 1, #fls do
  58.     if fs.isDir(directory..fls[i]) then
  59.       local ind3, ind4 = string.find(fls[i], search)
  60.       if ind[3] ~= nil then
  61.         table.insert(all2, {directory, fls[i], ind[3], ind[4]})
  62.       end      
  63.       local ret = rlib.search(search, directory..all[i].."/")
  64.       for i = 1, #ret do      
  65.         table.insert(all2, ret[i])
  66.       end
  67.     else
  68.       local ind3, ind4 = string.find(fls[i], search)
  69.       if ind[3] ~= nil then
  70.         table.insert(all2, {directory, fls[i], ind[3], ind[4]})
  71.       end
  72.     end
  73.   end
  74. end
  75. rlib.hilite = function(str, ind1, ind2)
  76.   local sub = string.gmatch(str, ".")
  77.   for i = 1, string.len(str) do
  78.     if i >= ind1 and i <= ind2 then
  79.       term.setBackgroundColor(colors.yellow)
  80.       write(sub())
  81.     else
  82.       term.setBackgroundColor(colors.white)
  83.       write(sub())
  84.     end
  85.   end
  86. end
  87.  
  88. rlib.read = function(lmt, start, cvr)
  89.     local col = term.getBackgroundColor()
  90.     local x, y = term.getCursorPos()
  91.     if lmt == nil then
  92.         local xc = term.getCursorPos()
  93.         local x = term.getSize()
  94.         lmt = x - xc
  95.     end
  96.     term.setCursorBlink(true)
  97.     local outstr = {}
  98.     local function Rupdate()
  99.         local x3, y3 = term.getCursorPos()
  100.         paintutils.drawLine(x, y, x+lmt, y, col)
  101.         term.setCursorPos(x+lmt, y)
  102.         for i = 1, lmt do
  103.             term.setCursorPos(x+lmt-i, y)
  104.             if outstr[i] ~= nil then
  105.                 if cvr == nil then
  106.                     write(outstr[#outstr-i+1])
  107.                 else
  108.                     write(cvr)
  109.                 end
  110.             end
  111.         end
  112.         term.setCursorPos(x+lmt, y)
  113.     end
  114.     if start ~= nil then
  115.         for i in string.gmatch(start, ".") do
  116.             table.insert(outstr, i)
  117.         end
  118.         term.setCursorPos(x, y)
  119.         if #outstr < lmt then
  120.             for i = 1, #outstr do
  121.                 if cvr == nil then
  122.                     write(outstr[i])
  123.                 else
  124.                     write(cvr)
  125.                 end
  126.             end
  127.         else
  128.             Rupdate()
  129.         end
  130.     end
  131.     while true do
  132.         if lmt == nil then
  133.             return false
  134.         end
  135.         eve, let = os.pullEvent()
  136.         if eve == "char" then
  137.             outstr[#outstr+1] = let
  138.             if #outstr <= lmt then
  139.                 if cvr == nil then
  140.                     write(let)
  141.                 else
  142.                     write(cvr)
  143.                 end
  144.             else
  145.                 Rupdate()
  146.             end
  147.         elseif eve == "key" then
  148.             if let == 14 and outstr ~= {} then
  149.                 outstr[#outstr] = nil
  150.                 if #outstr <= lmt then
  151.                     paintutils.drawLine(x, y, x+lmt, y, col)
  152.                     term.setCursorPos(x, y)
  153.                     for i = 1, #outstr do
  154.                         if cvr == nil then
  155.                             write(outstr[i])
  156.                         else
  157.                             write(cvr)
  158.                         end
  159.                     end
  160.                 else
  161.                     Rupdate()
  162.                 end
  163.             elseif let == 28 then
  164.                 local restr = ""
  165.                 for i = 1, #outstr do
  166.                     restr = restr..outstr[i]
  167.                 end
  168.                 return restr
  169.             elseif let == 15 then
  170.                 local restr = ""
  171.                 for i = 1, #outstr do
  172.                     restr = restr..outstr[i]
  173.                 end
  174.                 return restr, "tab"
  175.             end
  176.         end
  177.     end
  178.     term.setCursorBlink(false)
  179. end
  180. local function draw()
  181.   button.clear()
  182.   term.setBackgroundColor(colors.white)
  183.   term.clear()
  184.   paintutils.drawLine(dx, 1, dx, dy, colors.cyan)
  185.   term.setTextColor(colors.white)
  186.   term.setBackgroundColor(colors.red)
  187.   term.setTextColor(colors.black)
  188.   local x, y = term.getSize()
  189.   term.setCursorPos(x, 1)
  190.   term.write("X")
  191.   button.add(1, x, 1, x, 1)
  192.   term.setCursorPos(x, 2)
  193.   term.setBackgroundColor(colors.blue)
  194.   term.write("+")
  195.   term.setBackgroundColor(colors.yellow)
  196.   term.setCursorPos(51, 3)
  197.   write("/")
  198.   term.setBackgroundColor(colors.white)
  199. end
  200. local scroll2 = 0
  201. local function updateTbl()
  202.   local all
  203.   if srch == false then
  204.     all = fs.list(directory)
  205.   elseif srch == true then
  206.     all = rlib.search()
  207.   end
  208.   folders = {}
  209.   programs = {}
  210.   for i = 1, #all do
  211.     if fs.isDir(directory..all[i]) then
  212.       table.insert(folders, all[i])
  213.     else
  214.       table.insert(programs, all[i])
  215.     end
  216.   end
  217. end
  218. local function list()
  219.   term.setTextColor(colors.lime)
  220.   term.setCursorPos(1, 0)
  221.   if scroll == 0 then
  222.    var, varI = 1, 0
  223.    term.setCursorPos(1, 1)
  224.     write("..")
  225.     button.add(1, 1, 1, 51, 1)
  226.   else
  227.     varI, var = 1, 0
  228.   end
  229.   fol = 0
  230.   prog = 0
  231.   for i = 1, #folders do
  232.     if folders[i+scroll-varI] == nil then
  233.       break
  234.     end
  235.     _, y = term.getCursorPos()
  236.     if y >= dy-1 then
  237.       break
  238.     end
  239.     fol = fol+1
  240.     term.setCursorPos(1, y+1)
  241.     button.add(nil, 1, y+1, 50, y+1)
  242.     write(folders[i+scroll-varI])
  243.   end
  244.   term.setTextColor(colors.blue)
  245.   for i = 1, #programs do
  246.     if programs[i+scroll2] == nil then
  247.       break
  248.     end
  249.     _, y = term.getCursorPos()
  250.     if y <= dy-1 then
  251.       prog = prog+1
  252.          term.setCursorPos(1, y+1)
  253.          button.add(nil, 1, y+1, 50, y+1)
  254.          write(programs[i+scroll2])
  255.     end
  256.   end
  257.   button.add(1, 51, 2, 51, 2)
  258.   button.add(1, 51, 3, 51, 3)
  259. end
  260. local function update()
  261.   draw()
  262.   updateTbl()
  263.   list()
  264. end
  265.  
  266. local function textbox(top, strt)
  267.   while true do
  268.     paintutils.drawBox(1, 1, 51, 3, colors.cyan)
  269.     term.setCursorPos(1, 1)
  270.     term.setTextColor(colors.black)
  271.     write(top)
  272.     paintutils.drawLine(2, 2, 50, 2, colors.white)
  273.     term.setCursorPos(2, 2)
  274.     term.setTextColor(colors.black)
  275.     local text = rlib.read(48, strt)
  276.     if text ~= nil and text ~= "" then
  277.       term.setCursorBlink(false)
  278.       return text
  279.     else
  280.       term.setCursorBlink(false)
  281.       return false
  282.     end
  283.   end
  284. end
  285. local function setDir()
  286.   local dir = textbox("Enter Directory Path", directory)
  287.   if fs.exists(dir) then
  288.     directory = dir
  289.     scroll = 0
  290.     scroll2 = 0
  291.     update()
  292.   end
  293. end
  294. local function menu(y, pord, tf)
  295.   if y > 14 then
  296.      y = 14
  297.   end
  298.   if tf == true then
  299.     while true do
  300.     dd = true
  301.     paintutils.drawFilledBox(1, y, 11, y+3, colors.white)
  302.     paintutils.drawLine(12, y+1, 12, y+4, colors.gray)
  303.     paintutils.drawLine(2, y+4, 12, y+4, colors.gray)
  304.     button.clear()
  305.     term.setBackgroundColor(colors.red)
  306.     term.setTextColor(colors.black)
  307.     term.setCursorPos(1, y)
  308.     write("<")
  309.     button.add(1, 1, y, 1, y)
  310.     term.setBackgroundColor(colors.white)
  311.     term.setCursorPos(1, y+1)
  312.     write("remove")
  313.     button.add(1, 1, y+1, 10, y+1)
  314.     term.setCursorPos(1, y+2)
  315.     write("rename/move")
  316.     button.add(1, 1, y+2, 10, y+2)
  317.     term.setCursorPos(1, y+3)
  318.     write("copy")
  319.     button.add(1, 1, y+3, 10, y+3)
  320.     local click = button.wait()
  321.     if click == 1 then
  322.       dd = false
  323.       update()
  324.       break
  325.     elseif click == 2 then
  326.       fs.delete(pord)
  327.       dd = false
  328.       update()
  329.       scroll = 0
  330.       scroll2 = 0
  331.       break
  332.     elseif click == 3 then
  333.       local text = textbox("Path/New Name", pord)
  334.       if text then
  335.         fs.move(pord, text)
  336.       end
  337.       dd = false
  338.       scroll = 0
  339.       scroll2 = 0
  340.       update()
  341.       break
  342.     elseif click == 4 then
  343.       local text = textbox("destination", directory)
  344.       if text then
  345.         fs.copy(pord, text)
  346.       end
  347.       dd = false
  348.       scroll = 0
  349.       scroll2 = 0
  350.       update()
  351.       break
  352.     end
  353.     end
  354.   end
  355. if tf == false then
  356.   while true do
  357.     update()
  358.     dd = true
  359.     paintutils.drawFilledBox(1, y, 11, y+5, colors.white)
  360.     paintutils.drawLine(12, y+1, 12, y+6, colors.gray)
  361.     paintutils.drawLine(2, y+6, 12, y+6, colors.gray)
  362.     button.clear()
  363.     term.setBackgroundColor(colors.red)
  364.     term.setTextColor(colors.black)
  365.     term.setCursorPos(1, y)
  366.     write("<")
  367.     button.add(1, 1, y, 1, y)
  368.     term.setBackgroundColor(colors.white)
  369.     term.setCursorPos(1, y+1)
  370.     write("edit")
  371.     button.add(1, 1, y+1, 11, y+1)
  372.     term.setCursorPos(1, y+2)
  373.     write("rename/move")
  374.     button.add(1, 1, y+2, 11, y+2)
  375.     term.setCursorPos(1, y+3)
  376.     write("delete")
  377.     button.add(1, 1, y+3, 11, y+3)
  378.     term.setCursorPos(1, y+4)
  379.     write("copy")
  380.     button.add(1, 1, y+4, 11, y+4)
  381.     term.setCursorPos(1, y+5)
  382.     write("Run w/ args")
  383.     button.add(1, 1, y+5, 11, y+5)
  384.     local click = button.wait()
  385.     if click == 1 then
  386.       dd = false
  387.       update()
  388.       break
  389.     elseif click == 2 then
  390.       term.setBackgroundColor(colors.black)
  391.       shell.run("edit "..pord)
  392.       term.setCursorBlink(false)
  393.     elseif click == 3 then
  394.       local text = textbox("Path/New Name", pord)
  395.       if text ~= false then
  396.         fs.move(pord, text)
  397.       end
  398.       dd = false
  399.       update()
  400.       scroll = 0
  401.       scroll2 = 0
  402.       break
  403.     elseif click == 4 then
  404.       fs.delete(pord)
  405.       dd = false
  406.       update()
  407.       scroll = 0
  408.       scroll2 = 0
  409.       break
  410.     elseif click == 5 then
  411.       local text = textbox("Copy Path")
  412.       fs.copy(pord, text)
  413.     elseif click == 6 then
  414.       local text = textbox("Args")
  415.       term.setBackgroundColor(colors.black)
  416.       term.clear()
  417.       term.setCursorPos(1, 1)
  418.       term.setTextColor(colors.white)
  419.       shell.run(pord.." "..text)
  420.       term.setBackgroundColor(colors.black)
  421.       term.setTextColor(colors.white)
  422.       write("Program finished click anywhere to go back to explorer.")
  423.       os.pullEvent("mouse_click")
  424.     end
  425.   end
  426. end
  427. end
  428. local function scrollBar()
  429.   while true do
  430.     local eve, ud = os.pullEvent("mouse_scroll")
  431.     if eve == "mouse_scroll" and dd == false and scroll+ud >= 0 and scroll+scroll2+ud+19 <= #folders+#programs+1 and #folders+#programs > 19 then
  432.       if scroll-1+ud <= #folders and scroll2 == 0 then      
  433.         scroll = scroll+ud
  434.         update()
  435.       elseif scroll2+ud >= 0 then
  436.         scroll2 = scroll2+ud
  437.         update()
  438.       end
  439.     end
  440.   end
  441. end
  442. local function main()
  443.     while true do
  444.   update()
  445.         local click, but = button.wait()
  446.         if click == 1 then
  447.             term.setBackgroundColor(colors.black)
  448.             term.clear()
  449.             term.setCursorPos(1, 1)
  450.             break
  451.         elseif click == 2 and directory ~= "/" and scroll == 0 then
  452.             if but == 1 then
  453.     if fs.getDir(directory) ~= nil then
  454.                   directory = fs.getDir(directory).."/"
  455.                   update()
  456.     end
  457.             end
  458.         elseif click >= 1+var and click <= fol+1+var then
  459.     if but == 1 then
  460.       if folders[click-2+scroll] ~= nil then
  461.                  directory = directory..folders[click-2+scroll].."/"
  462.         scroll, scroll2 = 0, 0        
  463.                  update()
  464.       end  
  465.     elseif but == 2 then
  466.       menu(click, directory..folders[click-2+scroll], true)
  467.     end
  468.   elseif click > fol+var+1 and  click <= fol+var+prog+1 then
  469.     if but == 1 then
  470.       term.setBackgroundColor(colors.black)
  471.       term.clear()
  472.       term.setCursorPos(1, 1)
  473.       term.setTextColor(colors.white)
  474.       dd = true
  475.       shell.run(directory..programs[(click-fol)-var-1+scroll2])
  476.       term.setBackgroundColor(colors.black)
  477.       term.setTextColor(colors.white)
  478.       local _, y = term.getCursorPos()
  479.       term.setCursorPos(1, y+1)
  480.       write("Program finished click anywhere to go back to explorer.")
  481.       os.pullEvent("mouse_click")
  482.       dd = false
  483.     elseif but == 2 then
  484.       menu(click, directory..programs[(click-fol)-var-1+scroll2], false)
  485.     end
  486.         elseif click == prog+fol+var+2 then
  487.     dd = true
  488.     while true do
  489.       update()
  490.       paintutils.drawFilledBox(43, 1, 51, 19, colors.white)
  491.       paintutils.drawLine(42, 1, 42, 19, colors.cyan)
  492.       term.setTextColor(colors.black)
  493.       term.setCursorPos(42, 1)
  494.       term.setBackgroundColor(colors.red)
  495.       write("<")      
  496.       button.clear()
  497.       term.setBackgroundColor(colors.white)
  498.       term.setCursorPos(43, 1)  
  499.       button.add(1, 42, 1, 42, 1)
  500.       term.setTextColor(colors.green)
  501.       term.setCursorPos(43, 1)
  502.       write(" Folder")
  503.       button.add(1, 43, 1, 51, 1)
  504.       term.setTextColor(colors.blue)
  505.       term.setCursorPos(43, 2)
  506.       write(" Program")
  507.       button.add(1, 43, 2, 51, 2)
  508.       local click = button.wait()
  509.       if click ~= 1 then
  510.         if click == 2 then
  511.           local text = textbox("Folder Name")
  512.           if fs.exists(directory..text) == false then
  513.             fs.makeDir(directory..text)
  514.           end
  515.         elseif click == 3 then
  516.           local text = textbox("Program Name")
  517.           if fs.exists(directory..text) == false then
  518.             local file = fs.open(directory..text, "w")
  519.             file.write(" ")
  520.             file.close()
  521.           end
  522.         end
  523.       else
  524.         break
  525.       end
  526.     end
  527.     scroll = 0
  528.     scroll2 = 0
  529.     dd = false
  530.     update()
  531.   elseif click == prog+fol+var+3 then
  532.     setDir()
  533.     update()
  534.   end
  535.      end
  536. end
  537. update()
  538. parallel.waitForAny(main, scrollBar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement