Advertisement
Redxone

OFFICAL - CozyFile File Manager

Jul 4th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.28 KB | None | 0 0
  1. -- COPY RIGHT @Redxone or Lewisk3
  2. -- MC Ign: trainerred2000
  3. local arg = { ... }
  4.  
  5. local selFile = ""
  6. local fPath = "rom/"
  7. local fDefault = fPath
  8. local fPrePath = fPath
  9. local printoff = 0
  10. local mfoff = 0
  11. local fs_ver = "v1.0 Alpha"
  12. local copy_path = ""
  13. local copy_file = ""
  14. local fName = ""
  15.  
  16. local theme = {
  17.    
  18.      foldercolor = colors.lime,
  19.      filecolor = colors.orange,
  20.      iconbgcolor = colors.gray,
  21.      folderIcon = "[=]",
  22.      fileIcon = "-~-",
  23.      bgcolor = colors.white,
  24.      tbcolor = colors.gray,
  25.      tbtcolor = colors.yellow,
  26.      textcolor = colors.black,
  27.      dimtextcolor = colors.gray,
  28.      selectedcolor = colors.blue,
  29.      mbcolor = colors.black,
  30.      mtcolor = colors.white,
  31.      infocolor = colors.lightGray,
  32. }
  33.  
  34.  
  35. if(#arg > 0)then
  36.     f = fs.open(arg[1],"r")
  37.     filetheme = textutils.unserialize(f.readAll())
  38.     f.close()
  39.  
  40.     theme = setmetatable(filetheme, {__index = theme})
  41. end
  42.  
  43.  
  44. inRun = true
  45. inContext=false
  46.  
  47.  
  48. local contextEmpty = {
  49.     ["____________"] = {x=0,y=0,yoff = 1, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() end};
  50.     [" New Folder "] = {x=0,y=0,yoff = 2, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() ea.scp(1,1) ea.sbc(theme.tbcolor) ea.stc(theme.mtcolor); term.clearLine() write("New Folder: ") nFn = read(); if(not fs.isDir(nFn) and nFn ~= "")then fs.makeDir(fPath.."/"..nFn) inContext = false; inRun = true; cEmpty = false; fileSystem() end end};
  51.     [" New  File  "] = {x=0,y=0,yoff = 3, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() ea.scp(1,1) ea.sbc(theme.tbcolor) ea.stc(theme.mtcolor); term.clearLine() write("New File: ") nFn = read(); if(not fs.exists(fPath.."/"..nFn) and nFn ~= "")then f=fs.open(fPath.."/"..nFn,"w") f.writeLine(" ") f.close(); cEmpty = false; inContext = false; inRun = true; fileSystem() end end};
  52.     ["   Paste    "] = {x=0,y=0,yoff = 4, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() if(fs.exists(fPath.."/"..copy_file))then cut =false inContext = false inRun = true selFile = fPath..copy_file; copy_path = "" copy_file = "" fileSystem() end if(copy_path ~= "" and not fs.exists(fPath.."/"..copy_file))then if(not cut)then fs.copy(copy_path,fPath.."/"..copy_file); else fs.move(copy_path,fPath.."/"..copy_file); cut =false end inContext = false inRun = true selFile = fPath..copy_file; copy_path = "" copy_file = "" fileSystem() end end};
  53.     ["------------"] = {x=0,y=0,yoff = 5, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() end};
  54. }
  55.  
  56.  
  57. local folderMenu = {
  58.    
  59.         ["_____________"] = {yoff = 1, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() end};
  60.         [" Open Folder "] = {yoff = 2, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() fPrePath = fPath; fPath = fPath..fName.."/";  selFile = ""; loadFS(fPath); inContext=false inRun = true fileSystem(); end};
  61.         [" New Folder  "] = {yoff = 3, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() ea.scp(1,1) ea.sbc(theme.tbcolor) ea.stc(theme.textcolor); term.clearLine() write("New Folder: ") nFn = read(); if(not fs.isDir(nFn) and nFn ~= "")then fs.makeDir(fPath.."/"..nFn) inContext = false; inRun = true; fileSystem() end end};
  62.         [" Cut         "] = {yoff=  4, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() cut = true; copy_path = selFile; copy_file = fName inContext = false inRun = true fileSystem() end};
  63.         [" Copy        "] = {yoff=  5, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() cut = false; copy_path = selFile; copy_file = fName inContext = false inRun = true fileSystem() end};
  64.         ["Rename Folder"] = {yoff = 6, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() ea.sbc(theme.bgcolor) ea.stc(theme.textcolor); ea.scp(4,renameY) write(string.rep(" ",15)) ea.scp(4,renameY); nFn = read(); if(not fs.isDir(fPath.."/"..nFn))then fs.move(selFile, fPath.."/"..nFn) inContext = false inRun = true fileSystem(); end end};
  65.         ["Delete Folder"] = {yoff = 7, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() fs.delete(selFile); selFile = ""; inContext = false; inRun = true fileSystem(); end};
  66.         ["-------------"] = {yoff = 8, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() end};
  67. }
  68.  
  69.  
  70.  
  71. local contextMenu = {
  72.  
  73.     --["____________"] = {yoff = 1, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() end};
  74.     --["Run File    "] = {yoff = 2, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() os.run({},selFile) inContext = false; inRun = true; fileSystem() ;end};
  75.     ["Edit File   "] = {yoff = 1, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() shell.run("edit",selFile);  inContext = false; inRun = true; fileSystem() end};
  76.     ["New File    "] = {yoff = 2, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function()
  77.         ea.scp(1,1)
  78.         ea.sbc(theme.tbcolor)
  79.         ea.stc(theme.mtcolor);
  80.         term.clearLine()
  81.         write("New File: ")
  82.         nFn = read();
  83.         if(not fs.exists(nFn) and nFn ~= "")then
  84.             f = fs.open(fPath.."/"..nFn,"w")
  85.             f.writeLine(" ")
  86.             f.close();
  87.  
  88.             inContext = false;
  89.             inRun = true;
  90.             fileSystem()
  91.         end end};
  92.     ["New Folder  "] = {yoff = 3, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() ea.scp(1,1) ea.sbc(theme.tbcolor) ea.stc(theme.mtcolor); term.clearLine() write("New Folder: ") nFn = read(); if(not fs.isDir(nFn) and nFn ~= "")then fs.makeDir(fPath.."/"..nFn) inContext = false; inRun = true; fileSystem() end end};    
  93.     ["Cut         "] = {yoff=  4, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() cut = true; copy_path = selFile; copy_file = fName inContext = false inRun = true fileSystem() end};
  94.     ["Copy        "] = {yoff=  5, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() cut = false; copy_path = selFile; copy_file = fName inContext = false inRun = true fileSystem() end};
  95.     ["Rename File "] = {yoff = 6, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() ea.scp(4,renameY); ea.stc(theme.textcolor) ea.sbc(theme.bgcolor); print("                     ") ea.scp(5,renameY) nFn = read() if(not fs.exists(fPath..nFn) and nFn ~= "")then fs.move(selFile,fPath..nFn); end inRun = true; inContext = false; fileSystem() end};
  96.     ["Delete File "] = {yoff = 7, bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() if(fs.exists(selFile))then fs.delete(selFile); inContext = false; inRun = true; fileSystem(); end end};
  97.     ["------------"] = {yoff = 8,bcol=theme.mbcolor,tcol=theme.mtcolor,fcmd = function() end};
  98. }
  99.  
  100. local w,h = term.getSize()
  101.  
  102. os.loadAPI("ea")
  103. os.loadAPI("cozy_string")
  104.  
  105. function clear() ea.sbc(theme.bgcolor); term.clear() end
  106. clear()
  107.  
  108. --]] FILE SYSTEM [[--
  109. local filetable = {}
  110.  
  111. function loadFS(fsl)
  112.  
  113.      mfoff = 0
  114.  
  115.  
  116.     if(filetable ~= {})then filetable = {}
  117.  
  118.     for k, v in pairs(fs.list(fsl)) do
  119.    
  120.         if(fs.isDir(fsl..v))then isdir=true; else isdir = false; end
  121.  
  122.         if(not filetable[v])then
  123.             filetable[v] = v
  124.             filetable[v] = {offset = k, dir = isdir};
  125.             mfoff = mfoff + 1
  126.         end
  127.  
  128.     end
  129.  
  130. end
  131. end
  132.  
  133.  
  134. function drawMenu(x,y)
  135.  
  136.  
  137.     for k, v in pairs(contextMenu)do
  138.  
  139.         ea.sbc(v.bcol)
  140.         ea.stc(v.tcol)
  141.         ea.scp(x+2,y+v.yoff)
  142.         print(k)
  143.  
  144.     end
  145.        
  146. end
  147.  
  148. function drawEmptyContext(x,y)
  149.  
  150.     for k, v in pairs(contextEmpty)do
  151.  
  152.         v.x=x
  153.         v.y=y+v.yoff
  154.         ea.sbc(v.bcol)
  155.         ea.stc(v.tcol)
  156.         ea.scp(x+2,y+v.yoff)
  157.         print(k)
  158.  
  159.     end
  160.  
  161. end
  162.  
  163.  
  164. function drawFolderMenu(x,y)
  165.  
  166.  
  167.     for k, v in pairs(folderMenu)do
  168.  
  169.         ea.sbc(v.bcol)
  170.         ea.stc(v.tcol)
  171.         ea.scp(x+2,y+v.yoff)
  172.         print(k)
  173.  
  174.     end
  175.        
  176. end
  177.  
  178.  
  179. function fileSystem()
  180.  
  181.        
  182.  
  183.     while inRun do
  184.  
  185.         clear()
  186.  
  187.         ea.scp(1,2)
  188.  
  189.         moff = 14
  190.  
  191.  
  192.         loadFS(fPath)
  193.        
  194.  
  195.         ea.sbc(theme.bgcolor)
  196.         term.clear()
  197.         ea.sbc(theme.tbcolor)
  198.         ea.scp(1,1)
  199.         term.clearLine()
  200.         ea.stc(theme.tbtcolor)
  201.         ea.scp(1,1)
  202.         print("Cozy File Manager ")
  203.         ea.scp(w-#fs_ver,1)
  204.         print(fs_ver)
  205.         ea.stc(theme.infocolor)
  206.         ea.sbc(theme.bgcolor)
  207.         print(fPath)
  208.         print("[BACK]                                       [EXIT]")
  209.  
  210.  
  211.  
  212.         for k, v in pairs(filetable) do
  213.  
  214.             if(v.offset-printoff < moff+1 and v.offset >= printoff)then
  215.  
  216.                 if( ((v.offset)-printoff)+3 < 4)then addl = 4 else addl = ( (v.offset) - printoff )+3 end
  217.                 if(v.dir)then ea.stc(theme.foldercolor); ea.sbc(theme.iconbgcolor); ea.scp(1,addl); write(theme.folderIcon); if(cut and copy_path == fPath..k)then ea.stc(theme.dimtextcolor) else ea.stc(theme.textcolor); end ea.sbc(theme.bgcolor); if(fPath..k == selFile)then ea.sbc(theme.selectedcolor) else ea.sbc(theme.bgcolor) end; print(" "..k) end
  218.                 if(not v.dir)then ea.stc(theme.filecolor); ea.sbc(theme.iconbgcolor); ea.scp(1,addl); write(theme.fileIcon); ea.sbc(theme.bgcolor); if(cut and copy_path == fPath..k)then ea.stc(theme.dimtextcolor) else ea.stc(theme.textcolor); end if(fPath..k == selFile)then ea.sbc(theme.selectedcolor) else ea.sbc(theme.bgcolor) end; print(" "..k) end
  219.             end
  220.  
  221.             if(mfoff-printoff > moff)then
  222.  
  223.                     ea.stc(theme.infocolor)
  224.                     ea.scp(10,3)
  225.                     print("+")
  226.             end
  227.  
  228.             if(printoff+1 > 1)then
  229.  
  230.                     ea.stc(theme.infocolor)
  231.                     ea.scp(11,3)
  232.                     print("-")
  233.             end
  234.  
  235.         end
  236.  
  237.  
  238.         mi = {os.pullEvent()}
  239.  
  240.  
  241.         if(mi[1] == "mouse_click" and mi[2] == 1)then
  242.  
  243.             fBi = 1
  244.             clkF = 0
  245.  
  246.             for k, v in pairs(filetable) do
  247.  
  248.                 if(mi[3] >= 1 and mi[3] <= 4+#k and mi[4] == (v.offset-printoff)+3)then
  249.  
  250.                     clkF = 1
  251.                     if(v.dir and selFile == fPath..k)then fPrePath = fPath; fPath = fPath..k.."/"; loadFS(fPath); selFile = ""; printoff = 0 end
  252.                     if(selFile == fPath..k)then ea.stc(colors.white); ea.sbc(colors.black); ea.scp(1,1); os.run({},fPath..k); term.setCursorBlink(false) ea.sbc(colors.black) ea.stc(colors.white) term.clear() print("Press Any Key") os.pullEvent("key")
  253.                         else selFile = ""; selFile = fPath..k; fName = k end
  254.                
  255.                 end
  256.  
  257.             end
  258.                 if(clkF == 0)then selFile = "" end
  259.  
  260.                 if( mi[3] >= 1 and mi[3] <= 6 and mi[4] == 3)then
  261.                     fPrePath = cozy_string.split(fPrePath,"/",(cozy_string.pathLen(fPath)-fBi))
  262.                     fPath = fPrePath;  
  263.                     printoff = 0
  264.                     selFile = ""
  265.                     loadFS(fPath)
  266.                     fBi = 0
  267.                 end
  268.  
  269.                 if( mi[3] >= w-6 and mi[3] <= w and mi[4] == 3)then inRun=false; ea.sbc(colors.black) ea.stc(colors.white) ea.scp(1,1)  end
  270.  
  271.         else
  272.  
  273.             fBi = 1
  274.  
  275.             if(mi[1] == "key" or mi[1] == "mouse_scroll")then
  276.  
  277.                 ea.scp(1,1)
  278.                 pfc = mfoff
  279.                 if(mi[2] == keys.up or mi[2] == -1)then if(printoff > 0)then printoff = printoff - 1 end end
  280.                 if(mi[2] == keys.down or mi[2] == 1)then if(printoff < mfoff-moff)then printoff = printoff + 1 end end
  281.  
  282.             end
  283.  
  284.         end
  285.  
  286.         if(mi[1] == "mouse_click" and mi[2] == 2)then
  287.  
  288.             i = 0
  289.  
  290.             for k, v in pairs(filetable) do
  291.                
  292.                 i = i + 1
  293.  
  294.                 if(mi[3] >= 1 and mi[3] <= #k+5 and mi[4] == (v.offset-printoff)+3 and not inContext)then
  295.                    
  296.                     if(selFile == fPath..k)then
  297.                        
  298.                         if(mi[4] >= h-9)then
  299.                             prntedY = ((v.offset-printoff)+3)-9
  300.                         else
  301.                             prntedY = (v.offset-printoff)+3
  302.                         end
  303.  
  304.                         renameY = (v.offset-printoff)+3
  305.  
  306.                         if(not fs.isDir(selFile))then drawMenu(1,prntedY) else if(mi[4] >= h-10)then end drawFolderMenu(1,prntedY) end
  307.                         contextY = prntedY
  308.                         cEmpty=false
  309.                         inContext = true
  310.                         inRun = false
  311.                         fileSystem()
  312.  
  313.                        
  314.                     end
  315.                 end
  316.  
  317.                 if(selFile == "" and not (mi[3] >= w-6 and mi[3] <= w and mi[4] == 3) )then
  318.  
  319.                         if(mi[4] >= h-5)then pY = mi[4]-10 else pY = mi[4] end
  320.                         if(mi[3] >= w-12)then pX = w-22 else pX = mi[3] end
  321.                         drawEmptyContext(pX,pY)
  322.                         cEmpty = true
  323.                         inContext=true
  324.                         inRun=false
  325.  
  326.                 end
  327.  
  328.             end
  329.  
  330.         if(i == 0 )then
  331.  
  332.             if(mi[4] >= h-5)then
  333.                 pY = mi[4]-10
  334.             else
  335.                 pY = mi[4]
  336.             end
  337.  
  338.             if(mi[3] >= w-12)then
  339.                 pX = w-22
  340.             else
  341.                 pX = mi[3]
  342.             end
  343.  
  344.             drawEmptyContext(pX,pY)
  345.             cEmpty = true
  346.             inContext=true
  347.             inRun=false
  348.  
  349.         end
  350.  
  351.     end
  352. end
  353.  
  354.  
  355.     while inContext and not inRun do
  356.  
  357.         m = {os.pullEvent()}
  358.  
  359.         if(m[1] == "mouse_click")then
  360.  
  361.             if(m[2] == 2)then inContext = false; inRun = true; fileSystem() end
  362.  
  363.             if(cEmpty)then
  364.                 for k, v in pairs(contextEmpty) do
  365.  
  366.                     if(m[2] == 1 and m[3] >= v.x and m[3] <= (#k)+v.x and m[4] == v.y)then
  367.  
  368.                         v.fcmd()
  369.  
  370.                     end
  371.  
  372.                 end
  373.             end
  374.  
  375.  
  376.             if(not fs.isDir(selFile) and not cEmpty)then
  377.  
  378.                 for k, v in pairs(contextMenu) do
  379.  
  380.                     if(m[2] == 1 and m[3] >=3 and m[3] <= #k+3 and m[4] == v.yoff+contextY)then
  381.  
  382.                         v.fcmd()
  383.  
  384.                     end
  385.  
  386.                 end
  387.             end
  388.  
  389.             if(not cEmpty and fs.isDir(selFile))then
  390.                 for k, v in pairs(folderMenu) do
  391.  
  392.                     if(m[2] == 1 and m[3] >=3 and m[3] <= #k+3 and m[4] == v.yoff+contextY)then
  393.  
  394.                         v.fcmd()
  395.  
  396.                     end
  397.  
  398.                 end
  399.             end            
  400.  
  401.         end
  402.  
  403.     end
  404.  
  405. end
  406. fileSystem()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement