Advertisement
Redxone

[Special RedOS] - Kosy File Manager

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