Advertisement
Piorjade

fsExpose2.0 (CC filemanager)

Jan 6th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.60 KB | None | 0 0
  1. --[[
  2.          _         _            _     _      _              _          _            _            _      
  3.         /\ \      / /\         /\ \ /_/\    /\ \           /\ \       /\ \         / /\         /\ \    
  4.        /  \ \    / /  \       /  \ \\ \ \   \ \_\         /  \ \     /  \ \       / /  \       /  \ \  
  5.       / /\ \ \  / / /\ \__   / /\ \ \\ \ \__/ / /        / /\ \ \   / /\ \ \     / / /\ \__   / /\ \ \  
  6.      / / /\ \_\/ / /\ \___\ / / /\ \_\\ \__ \/_/        / / /\ \_\ / / /\ \ \   / / /\ \___\ / / /\ \_\
  7.     / /_/_ \/_/\ \ \ \/___// /_/_ \/_/ \/_/\__/\       / / /_/ / // / /  \ \_\  \ \ \ \/___// /_/_ \/_/
  8.    / /____/\    \ \ \     / /____/\     _/\/__\ \     / / /__\/ // / /   / / /   \ \ \     / /____/\    
  9.   / /\____\/_    \ \ \   / /\____\/    / _/_/\ \ \   / / /_____// / /   / / /_    \ \ \   / /\____\/    
  10.  / / /     /_/\__/ / /  / / /______   / / /   \ \ \ / / /      / / /___/ / //_/\__/ / /  / / /______    
  11. / / /      \ \/___/ /  / / /_______\ / / /    /_/ // / /      / / /____\/ / \ \/___/ /  / / /_______\  
  12. \/_/        \_____\/   \/__________/ \/_/     \_\/ \/_/       \/_________/   \_____\/   \/__________/  
  13.  
  14. v2.0
  15.  
  16.  Complete rewrite :)                                                                                                  
  17. ]]
  18. local oldTerm = term.current()
  19. local maxX, maxY = term.getSize()
  20. local mainWindow = window.create(oldTerm, 1, 1, maxX, maxY)
  21. --table containing buttons and textboxes, created by fsEAPI
  22. local items = {}
  23. local itemsInfo = {}
  24. local cFiles = {}
  25.  
  26. local cDir = "/"
  27. local history = {}
  28. local last = ""
  29. term.redirect(mainWindow)
  30.  
  31. os.loadAPI(shell.dir().."fsEAPI")
  32.  
  33. fsEAPI.addTextBox(items, "addressBar", cDir, maxX-6, 6, 1)
  34. fsEAPI.addButton(items, "backButton", "<", 1, 2, 1)
  35. fsEAPI.addButton(items, "forwardButton", ">", 1, 4, 1)
  36. fsEAPI.addScrollBox(items, "dir", maxX-2, maxY-3, 2, 3)
  37. local context = {
  38.   {
  39.     text = "Open",
  40.     bg = colors.white,
  41.     fg = colors.blue,
  42.    
  43.   },
  44.   {
  45.     text = "Delete",
  46.     bg = colors.white,
  47.     fg = colors.red,
  48.    
  49.   },
  50.   {
  51.     text = "Copy",
  52.     bg = colors.white,
  53.     fg = colors.black,
  54.     name = "",
  55.     typ = "File",
  56.     data = "",
  57.     folders = {},
  58.     files = {},
  59.    
  60.   },
  61.   {
  62.     text = "Paste",
  63.     bg = colors.white,
  64.     fg = colors.black,
  65.    
  66.   },
  67.   {
  68.     text = "Info",
  69.     bg = colors.white,
  70.     fg = colors.black,
  71.   },
  72. }
  73.  
  74. context[1]['func'] = function(path)
  75.   if fs.exists(path) and fs.isDir(path) then
  76.     table.insert(history, cDir)
  77.     cDir = path
  78.     if not string.find(cDir, "/", #cDir) then
  79.       cDir = cDir.."/"
  80.     end
  81.     items.addressBar.text = cDir
  82.     fsEAPI.update(items.addressBar)
  83.     fsEAPI.clearChilds(items.dir)
  84.     for each, file in ipairs(fs.list(cDir)) do
  85.       fsEAPI.addChild(items.dir, file)
  86.       if fs.isDir(cDir..file) then
  87.         items.dir.childs[#items.dir.childs].fg = colors.blue
  88.       end
  89.     end
  90.     fsEAPI.update(items.dir)
  91.   end
  92. end
  93.  
  94. context[2].func = function(path)
  95.       if fs.exists(path) and fs.isReadOnly(path) == false then
  96.         fs.delete(path)
  97.         items.dir.childs = {}
  98.         for each, file in ipairs(fs.list(cDir)) do
  99.           fsEAPI.addChild(items.dir, file)
  100.           if fs.isDir(cDir..file) then
  101.             items.dir.childs[#items.dir.childs].fg = colors.blue
  102.           end
  103.         end
  104.         fsEAPI.update(items.dir)
  105.       end
  106.     end
  107.  
  108. context[3].func = function(path)
  109.       context[3].folders = {}
  110.       context[3].files = {}
  111.       if fs.exists(path) and fs.isDir(path) == false then
  112.         local f = fs.open(path, "r")
  113.         context[3].data = f.readAll()
  114.         context[3].name = fs.getName(path)
  115.         context[3].typ = "File"
  116.         f.close()
  117.       elseif fs.exists(path) and fs.isDir(path) then
  118.         table.insert(context[3].folders, fs.getName(path))
  119.         context[3].data = {}
  120.         local function list(path)
  121.           local tab = fs.list(path)
  122.           local root = {}
  123.           local rootf = {}
  124.           local dat = {}
  125.           for k, v in ipairs(tab) do
  126.             if fs.isDir(path.."/"..v) then
  127.               table.insert(rootf, v)
  128.               local c, cf, cd = list(path.."/"..v)
  129.               for each, f in ipairs(c) do
  130.                 table.insert(root, v.."/"..f)
  131.                 table.insert(dat, cd[each])
  132.               end
  133.               for each, f in ipairs(cf) do
  134.                 table.insert(rootf, v.."/"..f)
  135.               end
  136.             else
  137.               table.insert(root, v)
  138.               local f = fs.open(path.."/"..v, "r")
  139.               local liss = f.readAll()
  140.               f.close()
  141.               table.insert(dat, liss)
  142.             end
  143.           end
  144.           return root, rootf, dat
  145.         end
  146.         local c, cf, cd  = list(path)
  147.         for k, v in ipairs(c) do
  148.           table.insert(context[3].files, context[3].folders[1].."/"..v)
  149.           table.insert(context[3].data, cd[k])
  150.         end
  151.         for k, v in ipairs(cf) do
  152.           table.insert(context[3].folders, context[3].folders[1].."/"..v)
  153.         end
  154.         context[3].typ = "Folder"
  155.       end
  156.     end
  157.  
  158. context[4].func = function(path)
  159.       if fs.isReadOnly(path) == false and #context[3].data > 0 and context[3].typ == "File" then
  160.         if fs.exists(path..context[3].name) then
  161.           local c = 1
  162.           repeat
  163.             local found = fs.exists(path..context[3].name.." - "..tostring(c))
  164.             if found then
  165.               c = c+1
  166.             end
  167.           until found == false
  168.           local f = fs.open(path..context[3].name.." - "..tostring(c), "w")
  169.           f.write(context[3].data)
  170.           f.close()
  171.         else
  172.           local f = fs.open(path..context[3].name, "w")
  173.           f.write(context[3].data)
  174.           f.close()
  175.         end
  176.       elseif fs.isReadOnly(path) == false and #context[3].folders > 0 and context[3].typ == "Folder" then
  177.         local folders = {}
  178.         local files = {}
  179.         local data = {}
  180.         for k, v in ipairs(context[3].files) do
  181.           table.insert(files, v)
  182.           table.insert(data, context[3].data[k])
  183.         end
  184.         for k, v in ipairs(context[3].folders) do
  185.           table.insert(folders, v)
  186.         end
  187.         if fs.exists(path..folders[1]) then
  188.           local oldName = folders[1]
  189.           local c = 1
  190.           repeat
  191.             local found = fs.exists(path..oldName.." - "..tostring(c))
  192.             if found then
  193.               c = c+1
  194.             end
  195.           until found == false
  196.           for k, v in ipairs(folders) do
  197.             folders[k] = string.gsub(folders[k], oldName, oldName.." - "..tostring(c), 1)
  198.           end
  199.           for k, v in ipairs(files) do
  200.             files[k] = string.gsub(files[k], oldName, oldName.." - "..tostring(c), 1)
  201.           end
  202.         end
  203.         local log = fs.open("/log", "w")
  204.         for k, v in ipairs(folders) do
  205.           log.writeLine(v)
  206.         end
  207.         log.close()
  208.         for k, v in ipairs(folders) do
  209.           fs.makeDir(path.."/"..v)
  210.         end
  211.         for k, v in ipairs(files) do
  212.           local f = fs.open(path.."/"..v, "w")
  213.           f.write(data[k])
  214.           f.close()
  215.         end
  216.       end
  217.     end
  218.  
  219. fsEAPI.addContextMenu(items.dir, context)
  220. local w = math.floor(maxX/2)
  221. local h = math.floor(maxY/2)
  222. local infoWindow = window.create(oldTerm, math.floor(maxX/2-(w/2)), math.floor(maxY/2-(h/2)), w, h, false)
  223. fsEAPI.addButton(itemsInfo, "closeButton", "X", 1, w, 1)
  224. fsEAPI.addScrollBox(itemsInfo, "textInfo", w-2, h-2, 2, 2)
  225. itemsInfo.closeButton.bg = colors.red
  226. itemsInfo.closeButton.fg = colors.white
  227. itemsInfo.textInfo.bg = colors.white
  228. term.setBackgroundColor(colors.gray)
  229. term.setTextColor(colors.black)
  230. term.clear()
  231.  
  232. fsEAPI.updateAll(items, colors.gray)
  233. for each, file in ipairs(fs.list(cDir)) do
  234.   fsEAPI.addChild(items.dir, file)
  235.   if fs.isDir(cDir..file) then
  236.     items.dir.childs[#items.dir.childs].fg = colors.blue
  237.   end
  238. end
  239. fsEAPI.update(items.dir)
  240.  
  241. local file = fs.open("/log", "w")
  242. local function log(str)
  243.   file.writeLine(str)
  244.   file.flush()
  245. end
  246.  
  247.  
  248. local c = coroutine.create(fsEAPI.run)
  249. local c2 = coroutine.create(fsEAPI.run)
  250. local ev = {}
  251. local inInfo = false
  252. coroutine.resume(c, items)
  253. coroutine.resume(c2, itemsInfo)
  254. while true do
  255.   coroutine.resume(c, unpack(ev))
  256.   ev = {os.pullEvent()}
  257.   if ev[1] == "context_select" and ev[2] == "dir" and ev[4] == "Info" then
  258.     local sel = false
  259.     for _, a in ipairs(items.dir.childs) do
  260.       if a.selected then
  261.         sel = true
  262.         break
  263.       end
  264.     end
  265.     if sel then
  266.       -- clear the previous info text and write a new one
  267.       itemsInfo.textInfo.childs = {}
  268.       local size = fs.getSize(cDir..items.dir.childs[ev[3]].text)
  269.       -- byte to kbyte
  270.       size = size/1000
  271.       local typ = "File"
  272.       if fs.isDir(cDir..items.dir.childs[ev[3]].text) then
  273.         typ = "Folder"
  274.       end
  275.       fsEAPI.addChild(itemsInfo.textInfo, "Type: "..typ)
  276.       itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].bg = colors.white
  277.       itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].fg = colors.black
  278.       fsEAPI.addChild(itemsInfo.textInfo, "Size: "..tostring(size).." KB")
  279.       itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].bg = colors.white
  280.       itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].fg = colors.black
  281.       fsEAPI.addChild(itemsInfo.textInfo, "ReadOnly: "..tostring(fs.isReadOnly(cDir..items.dir.childs[ev[3]].text)))
  282.       itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].bg = colors.white
  283.       itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].fg = colors.black
  284.       infoWindow.setVisible(true)
  285.       term.redirect(infoWindow)
  286.       fsEAPI.updateAll(itemsInfo, colors.gray)
  287.       local evt = {}
  288.       while true do
  289.         local v = {}
  290.         if evt[1] == "mouse_click" or evt[1] == "mouse_drag" or evt[1] == "mouse_up" or evt[1] == "mouse_down" then
  291.           for each, k in ipairs(evt) do
  292.             table.insert(v, k)
  293.           end
  294.           v[3] = math.floor(v[3]-(maxX/2-(w/2))+1)
  295.           v[4] = math.floor(v[4]-(maxY/2-(h/2))+1)
  296.         else
  297.           for each, k in ipairs(evt) do
  298.             table.insert(v, k)
  299.           end
  300.         end
  301.         coroutine.resume(c2, unpack(v))
  302.         evt = {os.pullEvent()}
  303.         if evt[1] == "button_press" then
  304.           break
  305.         end
  306.       end
  307.       infoWindow.setVisible(false)
  308.       mainWindow.redraw()
  309.       term.redirect(mainWindow)
  310.     end
  311.   elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[1].text then
  312.     local sel = false
  313.     for _, a in ipairs(items.dir.childs) do
  314.       if a.selected then
  315.         sel = true
  316.         break
  317.       end
  318.     end
  319.     if sel then
  320.       context[1].func(cDir..items.dir.childs[ev[3]].text)
  321.     end
  322.   elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[2].text then
  323.     local sel = false
  324.     for _, a in ipairs(items.dir.childs) do
  325.       if a.selected then
  326.         sel = true
  327.         break
  328.       end
  329.     end
  330.     if sel then
  331.      context[2].func(cDir..items.dir.childs[ev[3]].text)
  332.     end
  333.   elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[3].text then
  334.     local sel = false
  335.     for _, a in ipairs(items.dir.childs) do
  336.       if a.selected then
  337.         sel = true
  338.         break
  339.       end
  340.     end
  341.     if sel then
  342.       context[3].func(cDir..items.dir.childs[ev[3]].text)
  343.     end
  344.   elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[4].text then
  345.     context[4].func(cDir)
  346.     fsEAPI.clearChilds(items.dir)
  347.     for each, file in ipairs(fs.list(cDir)) do
  348.       fsEAPI.addChild(items.dir, file)
  349.       if fs.isDir(cDir..file) then
  350.         items.dir.childs[#items.dir.childs].fg = colors.blue
  351.       end
  352.     end
  353.     fsEAPI.update(items.dir)
  354.   elseif ev[1] == "button_press" and ev[3] == "backButton" and #history > 0 then
  355.     last = cDir
  356.     cDir = history[#history]
  357.     table.remove(history, #history)
  358.     fsEAPI.clearChilds(items.dir)
  359.     for each, file in ipairs(fs.list(cDir)) do
  360.       fsEAPI.addChild(items.dir, file)
  361.       if fs.isDir(cDir..file) then
  362.         items.dir.childs[#items.dir.childs].fg = colors.blue
  363.       end
  364.     end
  365.     fsEAPI.update(items.dir)
  366.     items.addressBar.text = cDir
  367.     fsEAPI.update(items.addressBar)
  368.   elseif ev[1] == "button_press" and ev[3] == "forwardButton" and #last > 0 then
  369.     table.insert(history, cDir)
  370.     cDir = last
  371.     last = ""
  372.     fsEAPI.clearChilds(items.dir)
  373.     for each, file in ipairs(fs.list(cDir)) do
  374.       fsEAPI.addChild(items.dir, file)
  375.       if fs.isDir(cDir..file) then
  376.         items.dir.childs[#items.dir.childs].fg = colors.blue
  377.       end
  378.     end
  379.     fsEAPI.update(items.dir)
  380.     items.addressBar.text = cDir
  381.     fsEAPI.update(items.addressBar)
  382.   elseif ev[1] == "textBox_input" then
  383.     -- check if entered string is a folder, if not change the text, sleep and show the old text
  384.     if not fs.isDir(items[ev[3]].text) then
  385.       items[ev[3]].text = "No such dir."
  386.       items[ev[3]].fg = colors.red
  387.       fsEAPI.update(items[ev[3]])
  388.       sleep(2)
  389.       items[ev[3]].text = cDir
  390.       items[ev[3]].fg = colors.lime
  391.       fsEAPI.update(items[ev[3]])
  392.     else
  393.       table.insert(history, cDir)
  394.       if not string.find(items[ev[3]].text, "/", #items[ev[3]].text) then
  395.         items[ev[3]].text = items[ev[3]].text.."/"
  396.         fsEAPI.update(items[ev[3]])
  397.       end
  398.       cDir = items[ev[3]].text
  399.       fsEAPI.clearChilds(items.dir)
  400.       for each, file in ipairs(fs.list(cDir)) do
  401.         fsEAPI.addChild(items.dir, file)
  402.         if fs.isDir(cDir..file) then
  403.           items.dir.childs[#items.dir.childs].fg = colors.blue
  404.         end
  405.       end
  406.       fsEAPI.update(items.dir)
  407.     end
  408.   end
  409. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement