Guest User

FileSystem[Lewisk3]

a guest
Jul 3rd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.02 KB | None | 0 0
  1. local selFile = ""
  2. local fPath = "/cozy/programs/"
  3. local fDefault = fPath
  4. local fPrePath = fPath
  5. local bgcolor = colors.cyan
  6. local tbcolor = colors.gray
  7. local tccolor = colors.white
  8. local printoff = 0
  9. local mfoff = 0
  10. local fs_ver = "v1.0 Alpha"
  11. inRun = true
  12. inContext=false
  13.  
  14. local contextMenu = {
  15.  
  16.     ["____________"] = {yoff = 1, bcol=colors.black,tcol=colors.white,fcmd = function() end};
  17.     ["Run File    "] = {yoff = 2, bcol=colors.black,tcol=colors.white,fcmd = function() os.run({},selFile) inContext = false; inRun = true; fileSystem() ;end};
  18.     ["Edit File   "] = {yoff = 3, bcol=colors.black,tcol=colors.white,fcmd = function() shell.run("edit",selFile);  inContext = false; inRun = true; fileSystem() end};
  19.     ["New File    "] = {yoff = 4, bcol=colors.black,tcol=colors.white,fcmd = function()
  20.  
  21.             f=fs.open(fPath.."/.temp","w")
  22.             f.writeLine(" ")
  23.             f.close()
  24.             selFile = fPath.."/.temp"
  25.             loadFS(fPath)
  26.             ea.scp(4,contextY);
  27.             ea.stc(colors.black)
  28.             ea.sbc(colors.white);
  29.             print("                     ")
  30.             ea.scp(5,contextY)
  31.             nFn = read()
  32.  
  33.             if(not fs.exists(fPath.."/"..nFn) and nFn ~= "")then
  34.                 fs.move(selFile,fPath..nFn);  
  35.                 inRun = true;
  36.                 inContext = false;
  37.                 fileSystem()
  38.             else
  39.                 inRun = true;
  40.                 inContext = false;
  41.                 fileSystem()
  42.                 fs.delete(fPath..".temp")
  43.                 loadFS(fPath)
  44.             end
  45.             end};
  46.     ["Rename File "] = {yoff = 5, bcol=colors.black,tcol=colors.white,fcmd = function() ea.scp(4,contextY); ea.stc(colors.black) ea.sbc(colors.white); print("                     ") ea.scp(5,contextY) nFn = read() if(not fs.exists(fPath..nFn) and nFn ~= "")then fs.move(selFile,fPath..nFn); end inRun = true; inContext = false; fileSystem() end};
  47.     ["Delete File "] = {yoff = 6, bcol=colors.black,tcol=colors.white,fcmd = function() if(fs.exists(selFile))then fs.delete(selFile); inContext = false; inRun = true; fileSystem(); end end};
  48.     ["------------"] = {yoff = 7, bcol=colors.black,tcol=colors.white,fcmd = function() end};
  49. }
  50.  
  51. local w,h = term.getSize()
  52.  
  53. os.loadAPI("ea")
  54. os.loadAPI("cozy_string")
  55.  
  56. function clear() ea.sbc(bgcolor); term.clear() end
  57. clear()
  58.  
  59. --]] FILE SYSTEM [[--
  60. local filetable = {}
  61.  
  62. function loadFS(fsl)
  63.  
  64.      mfoff = 0
  65.  
  66.  
  67.     if(filetable ~= {})then filetable = {}
  68.  
  69.     for k, v in pairs(fs.list(fsl)) do
  70.    
  71.         if(fs.isDir(fsl..v))then isdir=true; else isdir = false; end
  72.  
  73.         if(not filetable[v])then
  74.             filetable[v] = v
  75.             filetable[v] = {offset = k, dir = isdir};
  76.             mfoff = mfoff + 1
  77.         end
  78.     end
  79.  
  80. end
  81. end
  82.  
  83.  
  84. function drawMenu(x,y)
  85.  
  86.  
  87.     for k, v in pairs(contextMenu)do
  88.  
  89.         ea.sbc(v.bcol)
  90.         ea.stc(v.tcol)
  91.         ea.scp(x+2,y+v.yoff)
  92.         print(k)
  93.  
  94.     end
  95.        
  96. end
  97.  
  98. function fileSystem()
  99.  
  100.        
  101.  
  102.     while inRun do
  103.  
  104.         clear()
  105.  
  106.         ea.scp(1,2)
  107.  
  108.         moff = 14
  109.  
  110.  
  111.         loadFS(fPath)
  112.        
  113.  
  114.         ea.sbc(colors.white)
  115.         term.clear()
  116.         ea.sbc(tbcolor)
  117.         ea.scp(1,1)
  118.         term.clearLine()
  119.         ea.stc(colors.yellow)
  120.         ea.scp(1,1)
  121.         print("Cozy File Manager")
  122.         ea.scp(w-#fs_ver,1)
  123.         print(os_ver)
  124.         ea.stc(colors.lightGray)
  125.         ea.sbc(colors.white)
  126.         print(fPath)
  127.         print("[BACK]                                       [EXIT]")
  128.  
  129.  
  130.  
  131.         for k, v in pairs(filetable) do
  132.  
  133.             if(v.offset-printoff < moff and v.offset >= printoff)then
  134.  
  135.                 if( ((v.offset)-printoff)+3 < 4)then addl = 4 else addl = ( (v.offset) - printoff )+3 end
  136.  
  137.                 if(v.dir)then ea.stc(colors.green); ea.sbc(colors.gray); ea.scp(1,addl); write("[=]"); ea.stc(colors.black); ea.sbc(colors.white); print(" "..k) end
  138.                 if(not v.dir)then ea.stc(colors.orange); ea.sbc(colors.gray); ea.scp(1,addl); write("-~-"); ea.sbc(colors.white); ea.stc(colors.black); if(fPath..k == selFile)then ea.sbc(colors.lightBlue) else ea.sbc(colors.white) end; print(" "..k) end
  139.             end
  140.  
  141.             if(mfoff-printoff > moff)then
  142.  
  143.                     ea.stc(colors.lightGray)
  144.                     ea.scp(10,3)
  145.                     print("+")
  146.             end
  147.  
  148.             if(printoff+1 > 1)then
  149.  
  150.                     ea.stc(colors.lightGray)
  151.                     ea.scp(11,3)
  152.                     print("-")
  153.             end
  154.  
  155.         end
  156.  
  157.  
  158.         mi = {os.pullEvent()}
  159.  
  160.  
  161.         if(mi[1] == "mouse_click" and mi[2] == 1)then
  162.  
  163.             fBi = 1
  164.  
  165.             for k, v in pairs(filetable) do
  166.  
  167.                 if(mi[3] >= 1 and mi[3] <= 4+#k and mi[4] == (v.offset-printoff)+3)then
  168.  
  169.                     if(v.dir)then fPrePath = fPath; fPath = fPath..k.."/";  selFile = ""; loadFS(fPath); printoff = 0 end
  170.                     if(selFile == fPath..k)then ea.stc(colors.white); ea.sbc(colors.black); ea.scp(1,1); os.run({},fPath..k); else selFile = ""; selFile = fPath..k; end
  171.                 end
  172.  
  173.             end
  174.  
  175.                 if( mi[3] >= 1 and mi[3] <= 6 and mi[4] == 3)then
  176.                     fPrePath = cozy_string.split(fPrePath,"/",(cozy_string.pathLen(fPath)-fBi))
  177.                     fPath = fPrePath;  
  178.                     printoff = 0
  179.                     selFile = ""
  180.                     loadFS(fPath)
  181.                     fBi = 0
  182.                 end
  183.  
  184.                 if( mi[3] >= w-6 and mi[3] <= w and mi[4] == 3)then inRun=false; isRun=true; ea.sbc(colors.black) ea.stc(colors.white) ea.scp(1,1)  end
  185.  
  186.         else
  187.  
  188.             fBi = 1
  189.  
  190.             if(mi[1] == "key" or mi[1] == "mouse_scroll")then
  191.  
  192.                 ea.scp(1,1)
  193.                 pfc = mfoff
  194.                 if(mi[2] == keys.up or mi[2] == -1)then if(printoff > 0)then printoff = printoff - 1 end end
  195.                 if(mi[2] == keys.down or mi[2] == 1)then if(printoff < mfoff-moff)then printoff = printoff + 1 end end
  196.  
  197.             end
  198.  
  199.         end
  200.  
  201.         if(mi[1] == "mouse_click" and mi[2] == 2)then
  202.  
  203.             for k, v in pairs(filetable) do
  204.            
  205.                 if(mi[3] >= 1 and mi[3] <= #k+5 and mi[4] == (v.offset-printoff)+3 and not v.dir)then
  206.                
  207.                     if(selFile == fPath..k)then
  208.                        
  209.                         if(mi[4] >= h-9)then
  210.                             prntedY = ((v.offset-printoff)+3)-8
  211.                         else
  212.                             prntedY = (v.offset-printoff)+3
  213.                         end
  214.  
  215.                         drawMenu(1,prntedY)
  216.                         contextY = prntedY
  217.                         inContext = true
  218.                         inRun = false
  219.                         fileSystem()
  220.                     else
  221.                         selFile = fPath..k
  222.                     end
  223.                 end
  224.  
  225.             end
  226.  
  227.         end
  228.  
  229.     end
  230.  
  231.  
  232.     while inContext and not inRun do
  233.  
  234.         m = {os.pullEvent()}
  235.  
  236.         if(m[1] == "mouse_click")then
  237.  
  238.             if(m[2] == 2)then inContext = false; inRun = true; fileSystem() end
  239.  
  240.             for k, v in pairs(contextMenu) do
  241.  
  242.                 if(m[2] == 1 and m[3] >=3 and m[4] == v.yoff+contextY)then
  243.  
  244.                     v.fcmd()
  245.  
  246.                 end
  247.  
  248.             end
  249.  
  250.         end
  251.  
  252.     end
  253.  
  254. end
  255. fileSystem()
Advertisement
Add Comment
Please, Sign In to add comment