JustDoesGames

File Explorer

Feb 16th, 2020
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.19 KB | None | 0 0
  1.  
  2. local mainDir = "fe/" -- settings directory for saving and loading colors and settings
  3. local dir = mainDir.."engine/e.lua" -- change this to the file location of the engine
  4. if e == nil then if not os.loadAPI(dir) then error("Engine failed to load from "..dir..". Make sure you change this in the file to the appropriate location.") end end -- loads the engine if not already loaded
  5.  
  6.  
  7. local files = {}
  8. local dir = {"/"}
  9. local currentDir = ""
  10. local paste = "!"
  11. local cutting = false
  12. scroll = 0
  13. maxScroll = 0
  14. local w,h = e.gs()
  15. if w < 20 or h < 10 then error("Screen is to small.") end
  16. if not term.isColor() then error("Advanced computer is required.") end
  17. local cols = {}
  18.  
  19. cols["file"] = colors.white -- handling text color
  20. cols["directory"] = colors.yellow
  21. cols["rom"] = colors.orange
  22. cols["..."] = colors.white
  23.  
  24. cols["col1"] = colors.black -- handling background color
  25. cols["col2"] = colors.gray
  26.  
  27. cols["info"] = colors.white -- handling info screen text color
  28.  
  29. local settings = {}
  30. settings.run = true
  31. settings.edit = true
  32. settings.paint = true
  33. settings.delete = true
  34. settings.copy = false
  35. settings.move = false
  36.  
  37. if fs.exists(mainDir.."cols.lua") then
  38.     local file = fs.open(mainDir.."cols.lua", "r")
  39.     local tmp = file.readAll()
  40.     file.close()
  41.     cols = textutils.unserialize(tmp)
  42. end
  43. if fs.exists(mainDir.."settings.lua") then
  44.     local file = fs.open(mainDir.."settings.lua", "r")
  45.     local tmp = file.readAll()
  46.     file.close()
  47.     settings = textutils.unserialize(tmp)
  48. end
  49.  
  50. running = true
  51.  
  52. local function reloadFiles()
  53.     if #dir == 0 then running = false return false end
  54.     files, scroll, currentDir = {}, 0, ""
  55.     for i=1, #dir do
  56.         currentDir = currentDir..dir[i]
  57.     end
  58.     local fl = fs.list(currentDir)
  59.     local tmp = {{},{},{}}
  60.     for i=1, #fl do
  61.         if fs.isReadOnly(currentDir..fl[i]) then tmp[1][#tmp[1]+1] = fl[i] elseif fs.isDir(currentDir..fl[i]) then tmp[2][#tmp[2]+1] = fl[i] else tmp[3][#tmp[3]+1] = fl[i] end
  62.     end
  63.     fl = {} fl[1] = "..."
  64.     for i=1, #tmp[1] do
  65.         fl[#fl+1] = tmp[1][i]
  66.     end
  67.     for i=1, #tmp[2] do
  68.         fl[#fl+1] = tmp[2][i]
  69.     end
  70.     for i=1, #tmp[3] do
  71.         fl[#fl+1] = tmp[3][i]
  72.     end
  73.     for i=1, #fl do
  74.         if fl[i] ~= "..." then
  75.             local tmp = fl[i]
  76.             if string.len(fl[i]) > math.ceil(w/1.2)-3 then
  77.                 tmp = string.sub(fl[i], 1, math.ceil(w/1.2)-3).."..."
  78.             end
  79.             files[i] = {}
  80.             files[i].name = tmp
  81.             files[i].rom = fs.isReadOnly(currentDir..fl[i])
  82.             if fs.isDir(currentDir..fl[i]) then files[i].type = "directory" else files[i].type = "file" end
  83.             files[i].size = fs.getSize(currentDir..fl[i])
  84.             files[i].dir = currentDir..fl[i]
  85.         else
  86.             local tmp = "( "..currentDir.." )"
  87.             if string.len(currentDir) > math.ceil(w/1.2)-3 then
  88.                 tmp = "(..."..string.sub(currentDir, math.ceil(w/1.2)-3, -1)..")"
  89.             end
  90.             files[i] = {}
  91.             files[i].name = "... "..tmp
  92.             files[i].rom = false
  93.             files[i].type = "..."
  94.             files[i].size = 0
  95.             files[i].dir = "/"
  96.         end
  97.     end
  98.     if h > #files then maxScroll = 0 else maxScroll = #files-h end
  99.     e.clr() -- for display to draw fresh
  100. end
  101.  
  102. local function display()
  103.     e.cp(1,1) local tmp = 1
  104.     for i=1, math.min(#files, h) do
  105.         term.setBackgroundColor(cols["col"..tmp]) term.clearLine() e.cp(1,i)
  106.         if files[i+scroll].rom then
  107.             term.setTextColor(cols["rom"])
  108.             write("* ")
  109.         end
  110.         term.setTextColor(cols[files[i+scroll].type])
  111.         write(files[i+scroll].name)
  112.         if i ~= math.min(#files, h) then print("") end
  113.         if tmp == 1 then tmp = 2 else tmp = 1 end
  114.     end
  115.     e.setBack("black")
  116.     if scroll == 0 then sleep(.1) end
  117. end
  118.  
  119. local function displayInfo(info)
  120.     if fs.exists(info.dir) then
  121.         e.clr() e.cp(1,1)
  122.         term.setTextColor(cols["info"])
  123.         print(info.name)
  124.         print("Type: "..info.type)
  125.         print("ReadOnly (rom): "..tostring(info.rom))
  126.         print("Dir: "..info.dir)
  127.         print("Size: "..info.size.." byte(s)")
  128.         print("")
  129.         local run, update, menu, sel, s = true, true, {}, 1, false
  130.         if info.type == "file" and settings.run then menu[#menu+1] = "Run" end
  131.         if info.type == "file" and settings.edit then menu[#menu+1] = "Edit" end
  132.         if info.type == "file" and e.notnil(string.find(info.name, ".nfp")) and settings.paint then menu[#menu+1] = "Paint" end
  133.         if info.type ~= "..." and settings.copy then menu[#menu+1] = "Copy" end
  134.         if info.type ~= "..." and settings.move and not info.rom then menu[#menu+1] = "Cut" end
  135.         if info.type ~= "..." and not info.rom and settings.delete then menu[#menu+1] = "Delete" end
  136.         if info.type ~= "file" and settings.copy and paste ~= "!" then menu[#menu+1] = "Paste" end
  137.         menu[#menu+1] = "Exit"
  138.         while run do
  139.             if update then
  140.                 for i=1, #menu do
  141.                     e.cp(1,h-#menu-1+i) term.clearLine() e.cp(1,h-#menu-1+i)
  142.                     if i == sel then
  143.                         print("> "..menu[i])
  144.                     else
  145.                         print(menu[i].."  ")
  146.                     end
  147.                 end
  148.                 sleep(.0000001)
  149.                 a,i,x,y = os.pullEvent()
  150.                 if a == "mouse_click" then
  151.                     if i == 1 and y >= h-#menu and y <= h-1 then -- left click
  152.                         sel = y-(h-#menu-1) s = true run = false
  153.                     end
  154.                 elseif a == "key" then
  155.                     if i == keys.q then
  156.                         run = false
  157.                     elseif i == keys.w or i == keys.up then
  158.                         if sel == 1 then sel = #menu else sel = sel - 1 end update = true
  159.                     elseif i == keys.s or i == keys.down then
  160.                         if sel == #menu then sel = 1 else sel = sel + 1 end update = true
  161.                     elseif i == keys.e or i == keys.enter then
  162.                         s = true run = false
  163.                     end
  164.                 end
  165.             end
  166.         end
  167.         if s then
  168.             sel = menu[sel]
  169.             if sel == "Run" then
  170.                 e.clr() e.cp(1,1) shell.run(info.dir) sleep(2)
  171.             elseif sel == "Edit" then
  172.                 shell.run("edit "..info.dir)
  173.             elseif sel == "Paint" then
  174.                 shell.run("paint "..info.dir)
  175.             elseif sel == "Delete" then
  176.                 fs.delete(info.dir)
  177.             elseif sel == "Copy" then
  178.                 paste = info.dir
  179.             elseif sel == "Cut" then
  180.                 cutting = true
  181.                 paste = info.dir
  182.             elseif sel == "Paste" then
  183.                 if cutting then
  184.                     cutting = false
  185.                     fs.copy(paste, info.dir..fs.getName(paste))
  186.                     fs.delete(paste)
  187.                     paste = "!"
  188.                 else
  189.                     print(paste.."  "..currentDir) sleep(2)
  190.                     --if fs.isDir(paste) then fs.copy("/testing1", currentDir..paste)
  191.                     if fs.exists(paste) and fs.exists(currentDir) then
  192.                         if fs.isDir(paste) then
  193.                             fs.copy(paste, currentDir..paste)
  194.                         else
  195.                             fs.copy(paste, currentDir..paste)
  196.                         end
  197.                     end
  198.                 end
  199.             elseif sel == "Exit" then
  200.                 run = false
  201.             end
  202.         end
  203.     end
  204. end
  205.  
  206. local function manageSettings()
  207.     --
  208. end
  209.  
  210. local function runtime() -- main running script
  211.     reloadFiles()
  212.     local updat = true
  213.     while running do
  214.         if updat then
  215.             updat = false
  216.             display()
  217.         end
  218.         --sleep(.000001) -- to avoid lag and crashes
  219.         local a,b,x,y = os.pullEvent()
  220.         if a == "mouse_scroll" then
  221.             if b == 1 then -- down
  222.                 if scroll < maxScroll then
  223.                     scroll = scroll + 1
  224.                 else
  225.                     scroll = 0
  226.                 end
  227.                 updat = true
  228.             else -- up
  229.                 if scroll > 0 then
  230.                     scroll = scroll - 1
  231.                 else
  232.                     scroll = maxScroll
  233.                 end
  234.                 updat = true
  235.             end
  236.         elseif a == "mouse_click" then
  237.             if b == 1 then
  238.                 if e.notnil(files[y+scroll]) then
  239.                     if files[y+scroll].type == "directory" then
  240.                         dir[#dir+1] = files[y+scroll].name.."/" reloadFiles() updat = true
  241.                     elseif files[y+scroll].type == "..." then
  242.                         --table.remove(dir, #dir)
  243.                         dir[#dir] = nil
  244.                         reloadFiles() updat = true
  245.                     else
  246.                         displayInfo(files[y+scroll]) reloadFiles() updat = true
  247.                     end
  248.                 end
  249.             elseif b == 2 then
  250.                 if e.notnil(files[y+scroll]) then
  251.                     if files[y+scroll].type == "..." then
  252.                         displayInfo(files[y+scroll]) reloadFiles() updat = true
  253.                     else
  254.                         displayInfo(files[y+scroll]) reloadFiles() updat = true
  255.                     end
  256.                 end
  257.             end
  258.         elseif a == "key" then
  259.             if b == keys.q then
  260.                 table.remove(dir, #dir) reloadFiles() updat = true
  261.             elseif b == keys.w or b == keys.up then
  262.                 if scroll ~= 0 then
  263.                     scroll = scroll - 1 updat = true
  264.                 end
  265.             elseif b == keys.s or b == keys.down then
  266.                 if scroll ~= maxScroll then
  267.                     scroll = scroll + 1 updat = true
  268.                 end
  269.             elseif b == keys.r then
  270.                 e.clr() sleep(.1) reloadFiles() updat = true
  271.             elseif b == keys.f then
  272.                 --manageSettings() updat = true
  273.             end
  274.         end
  275.     end
  276. end
  277.  
  278. runtime() -- not run inside pcall because the os is responsible for handling errors
  279. e.clr() e.cp(1,1)
  280. sleep(.001)
Advertisement
Add Comment
Please, Sign In to add comment