Advertisement
PaymentOption

fileBrowser

Apr 30th, 2012
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.55 KB | None | 0 0
  1. -- Client Side File Browser by PaymentOption for NeXuS --
  2. VERSION = "1.0"
  3. ---------------------------------------------------------
  4.  
  5. -- VARS --
  6. selection = 1
  7. directory = "ROOT"
  8. subSelection = 1
  9. objectType = 1 -- To tell what type of file is in the selection; 1 = directory, 2 = file
  10. bDisplayMore = false
  11. bCreating = false
  12. ----------
  13.  
  14. -- Helper Functions --
  15. function list(sDir) -- Written by dan200; the creator of ComputerCraft
  16.     local tAll = {}
  17.     if directory == "ROOT" then tAll = fs.list( "" )
  18.     else tAll = fs.list( directory) end
  19.     local tFiles = {}
  20.     local tDirs = {}
  21.  
  22.     for n, sItem in pairs( tAll ) do
  23.         if string.sub( sItem, 1, 1 ) ~= "." then
  24.             local sPath = fs.combine( sDir, sItem )
  25.             if fs.isDir( sPath ) then
  26.                 table.insert( tDirs, sItem )
  27.             else
  28.                 table.insert( tFiles, sItem )
  29.             end
  30.         end
  31.     end
  32.     table.sort( tDirs )
  33.     table.sort( tFiles )
  34.    
  35.     local y = 5 --For the printing of the files/dirs
  36.     local stopNum = 9
  37.    
  38.     term.setCursorPos(4, 4); print("Directories: ")
  39.     term.setCursorPos(24, 4); print("Files: "); term.setCursorPos(4, 5)
  40.    
  41.     if bDisplayMore == true then
  42.         clear(); local index = 10; local x = 5
  43.         objectType = 2
  44.         printBorder()
  45.         if shell.dir() == "" then cPrint(2, "Directory: ROOT")
  46.         else cPrint(2, "Directory: "..shell.dir()) end
  47.         rPrint(17, "Version: "..VERSION.." *")
  48.         y = 6
  49.         term.setCursorPos(4, 4); write("Files: "); y = 5; term.setCursorPos(x, y)
  50.         repeat
  51.             if objectType == 2 and selection == index-9 then write("["..tostring(tFiles[index]).."]"); term.setCursorPos(x, y+1); y = y+1
  52.             else write(" "..tostring(tFiles[index]).." "); term.setCursorPos(x, y+1); y = y+1 end
  53.            
  54.             if index == 18 then x = 16; y = 4 end
  55.             if index == 28 then x = 26; y = 4 end
  56.             if index == 38 then x = 36; y = 4 end
  57.             if index >= 49 then break end
  58.             index = index+1
  59.         until index == #tFiles+1
  60.         return
  61.     end
  62.    
  63.     for i,v in pairs(tDirs) do
  64.         if objectType == 1 and selection == i then write("["..v.."]"); term.setCursorPos(4, y+1); y = y+1
  65.         else write(" "..v.." "); term.setCursorPos(4, y+1); y = y+1 end
  66.         if i == stopNum then
  67.             bMore = true
  68.             term.setCursorPos(38, 15)
  69.             if selection == 300 then write("[More Dirs]")
  70.             else write(" More Dirs ") end
  71.             break
  72.         end
  73.     end
  74.     y = 5; term.setCursorPos(24, 5)
  75.     for i,v in pairs(tFiles) do
  76.         if objectType == 2 and selection == i then write("["..v.."]"); term.setCursorPos(24, y+1); y = y+1
  77.         else write(" "..v.." "); term.setCursorPos(24, y+1); y = y+1 end
  78.         if i == stopNum then
  79.             bMore = true
  80.             term.setCursorPos(36, 15)
  81.             if selection == 300 then write("[More Files]") -- 300 Because that is an unreachable selection otherwise
  82.             else write(" More Files ") end
  83.             break
  84.         end
  85.     end
  86. end
  87.  
  88. function cPrint(height, string)
  89.     local w,h = term.getSize()
  90.     local xPos = w/2 - string.len(string)/2
  91.    
  92.     term.setCursorPos(xPos, height)
  93.     term.write(string)
  94. end
  95.  
  96. function rPrint(height, string)
  97.     local w,h = term.getSize()
  98.     local xPos = w - string.len(string)
  99.    
  100.     term.setCursorPos(xPos, height)
  101.     term.write(string)
  102. end
  103.  
  104. function printBorder()
  105.     local w,h = term.getSize()
  106.     write(" ".. string.rep('*', w-2).."\n")
  107.     for i=1, h-2 do write(" "..'*'..string.rep(" ", w-4)..'*'.."\n") end
  108.     write(" ".. string.rep('*', w-2))
  109. end
  110.  
  111. function printCreateMenu(sPath)
  112.     local filename = ""; local w,h = term.getSize()
  113.     term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  114.     term.setCursorPos(4, 15); write("Enter new file name: "); filename = read()
  115.    
  116.     if fs.exists(tostring(sPath).."/"..tostring(filename)) == false then
  117.         if not shell.dir() == "" then shell.run("edit", tostring(sPath).."/"..tostring(filename))
  118.         else shell.run("edit", tostring(filename)) end
  119.     else
  120.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  121.         term.setCursorPos(4, 15); write(tostring(filename).." already exists"); sleep(1.3); clear()
  122.     end
  123. end
  124.  
  125. function printCtrlMenu()
  126.     if selection == 41 then      term.setCursorPos(3, 17); write("[Create] Exit")
  127.     elseif selection == 42  then term.setCursorPos(3, 17); write(" Create [Exit]")
  128.     else                         term.setCursorPos(3, 17); write("Ctrl to create file")
  129.     end
  130. end
  131.  
  132. function clear() term.clear(); term.setCursorPos(1,1) end
  133. ----------------------
  134.  
  135. -- Menues and other screens --
  136. function printScreen()
  137.     clear()
  138.     printBorder()
  139.     if shell.dir() == "" then cPrint(2, "Directory: ROOT")
  140.     else cPrint(2, "Directory: "..shell.dir()) end
  141.     rPrint(17, "Version: "..VERSION.." *")
  142.     if bDisplayMore == false then printCtrlMenu() end
  143.     listFiles()
  144. end
  145.  
  146. function printLogo()
  147.     clear()
  148.     print("     _______          ____  ___      _________")
  149.     print("     \\      \\   ____  \\   \\/  /__ __/   _____/")
  150.     print("     /   |   \\_/ __ \\  \\     /|  |  \\_____  \\ ")
  151.     print("    /    |    \\  ___/  /     \\|  |  /        \\")
  152.     print("    \\____|__  /\\___  >/___/\\  \\____/_______  /")
  153.     print("            \\/     \\/       \\_/            \\/ ")
  154.     term.setCursorPos(4, 11); write("Controls: Enter = Change Dir/File Options,")
  155.     term.setCursorPos(4, 12); write("Arrows = Selection Change, Back = Return to")
  156.     term.setCursorPos(4, 13); write("ROOT, X = Exit Program")
  157.     cPrint(15, "File Browser by PaymentOption")
  158.     rPrint(18, "Spaghetti Coded!")
  159. end
  160.  
  161. function printMenu()
  162.     if subSelection == 1 then term.setCursorPos(4, 15); write("[Edit]")-- +6
  163.     else                   term.setCursorPos(4, 15); write(" Edit ") end
  164.    
  165.     if subSelection == 2 then term.setCursorPos(10, 15); write("[Rename]") -- +9
  166.     else                  term.setCursorPos(10, 15); write(" Rename ") end
  167.    
  168.     if subSelection == 3 then term.setCursorPos(19, 15); write("[Delete]") -- +8
  169.     else                   term.setCursorPos(19, 15); write(" Delete ") end
  170.    
  171.     if subSelection == 4 then term.setCursorPos(27, 15); write("[Run]")
  172.     else                      term.setCursorPos(27, 15); write(" Run ") end -- +5
  173.    
  174.     if subSelection == 5 then term.setCursorPos(32, 15); write("[Exit]") -- +6
  175.     else                   term.setCursorPos(32, 15); write(" Exit ") end
  176. end
  177.  
  178. function subMenu(fileNum)
  179.     local tAll = {}; local w,h = term.getSize(); local sDir = shell.dir()
  180.     if directory == "ROOT" then tAll = fs.list( "" )
  181.     else tAll = fs.list( directory) end
  182.     local tFiles = {}
  183.     local tDirs = {}
  184.  
  185.     for n, sItem in pairs( tAll ) do
  186.         if string.sub( sItem, 1, 1 ) ~= "." then
  187.             local sPath = fs.combine( sDir, sItem )
  188.             if fs.isDir( sPath ) then
  189.                 table.insert( tDirs, sItem )
  190.             else
  191.                 table.insert( tFiles, sItem )
  192.             end
  193.         end
  194.     end
  195.     table.sort( tDirs )
  196.     table.sort( tFiles )
  197.    
  198.     for i,v in pairs(tFiles) do
  199.         if fileNum == i then
  200.             while true do
  201.                 term.setCursorPos(1, 15); term.clearLine();
  202.                 term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* "); printMenu()
  203.                
  204.                 local event, key = os.pullEvent("key")
  205.                
  206.                 if key == 205 and subSelection < 5 then subSelection = subSelection+1
  207.                 elseif key == 203 and subSelection > 1 then subSelection = subSelection-1
  208.                
  209.                 elseif key == 28 and subSelection == 1 then edit(tostring(shell.dir()).."/"..v); break
  210.                 elseif key == 28 and subSelection == 2 then renameFile(tostring(shell.dir()).."/"..v); break
  211.                 elseif key == 28 and subSelection == 3 then delete(tostring(shell.dir()).."/"..v); selection = selection-1; break
  212.                 elseif key == 28 and subSelection == 4 then shell.run(tFiles[i]); return
  213.                 elseif key == 28 and subSelection == 5 then break
  214.                 end
  215.             end
  216.         end
  217.     end
  218. end
  219. ------------------------------
  220.  
  221. -- File related Functions --
  222. function listFiles()
  223.     local sDir = shell.dir()
  224.     if sDir ~= "" and sDir ~= "/" then directory = tostring(sDir)
  225.     else directory = "ROOT" end
  226.     list(sDir)
  227. end
  228.  
  229. function navigateDir(DirNum)
  230.     local sDir = shell.dir(); local w,h = term.getSize()
  231.     local tAll = fs.list( sDir )
  232.         local tFiles = {}
  233.         local tDirs = {}
  234.  
  235.         for n, sItem in pairs( tAll ) do
  236.             if string.sub( sItem, 1, 1 ) ~= "." then
  237.                 local sPath = fs.combine( sDir, sItem )
  238.                 if fs.isDir( sPath ) then
  239.                     table.insert( tDirs, sItem )
  240.                 else
  241.                     table.insert( tFiles, sItem )
  242.                 end
  243.             end
  244.         end
  245.         table.sort( tDirs )
  246.         table.sort( tFiles )
  247.        
  248.         for i,v in pairs(tDirs) do
  249.             if DirNum == i and directory ~= "ROOT" then shell.setDir(directory.."/"..v); return
  250.             elseif DirNum == i and directory == "ROOT" then shell.setDir(v); return end
  251.         end
  252.        
  253.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  254.         term.setCursorPos(4, 15); write("Cannot navigate to that Directory"); sleep(1.3); clear()
  255. end
  256.  
  257. function edit(sPath)
  258.     local w,h = term.getSize()
  259.     term.setCursorPos(1, 15); term.clearLine()
  260.     write(" *"..string.rep(" ", w-4).."* ")
  261.    
  262.     if fs.exists(tostring(sPath)) == false then
  263.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  264.         term.setCursorPos(4, 15); write("Cannot edit a non existant file"); sleep(1.3); clear()
  265.     elseif fs.isDir(tostring(sPath)) then
  266.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  267.         term.setCursorPos(4, 15); write("Cannot edit a directory"); sleep(1.3); clear()
  268.     elseif string.find(tostring(sPath), "rom/") then
  269.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  270.         term.setCursorPos(4, 15); write("Cannot edit read only files"); sleep(1.3); clear()
  271.     elseif shell.dir() ~= "" or shell.dir() == "/" then
  272.         shell.setDir(""); shell.run("edit", tostring(sPath))
  273.     else shell.run("edit", tostring(sPath)) end
  274. end
  275.  
  276. function renameFile(sPath)
  277.     local w,h = term.getSize(); local newname = ""
  278.     term.setCursorPos(1, 15); term.clearLine()
  279.     write(" *"..string.rep(" ", w-4).."* ")
  280.    
  281.     if fs.exists(tostring(sPath)) == false then
  282.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  283.         term.setCursorPos(4, 15); write("Cannot rename a nonexistant file"); sleep(1.3); clear()
  284.     elseif fs.isDir(tostring(sPath)) then
  285.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  286.         term.setCursorPos(4, 15); write("Cannot rename a directory"); sleep(1.3); clear()
  287.     elseif string.find(tostring(sPath), "rom/") then
  288.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  289.         term.setCursorPos(4, 15); write("Cannot rename a read only file"); sleep(1.3); clear()
  290.     else
  291.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  292.         term.setCursorPos(4, 15); write("Rename to: "); newname = read(); clear()
  293.         if directory == "ROOT" then
  294.             local file = fs.open(tostring(sPath), "r"); local fileContents = file.readAll(); file.close()
  295.             fs.delete(tostring(sPath)); file = fs.open(tostring(newname), "w"); file.write(fileContents); file.close()
  296.         else
  297.             local file = fs.open(directory..tostring(sPath), "r"); local fileContents = file.readAll(); file.close()
  298.             fs.delete(tostring(sPath)); file = fs.open(shell.dir()..tostring(newname), "w"); file.write(fileContents); file.close()
  299.         end
  300.     end
  301. end
  302.  
  303. function delete(sPath)
  304.     local w,h = term.getSize()
  305.     term.setCursorPos(1, 15); term.clearLine()
  306.     write(" *"..string.rep(" ", w-4).."* ")
  307.    
  308.     if string.find(tostring(sPath), "rom/") then
  309.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  310.         term.setCursorPos(4, 15); write("Cannot delete read only files"); sleep(1.3); clear()
  311.     else
  312.         fs.delete(tostring(sPath))
  313.         term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
  314.         term.setCursorPos(4, 15); write("Deleted"); sleep(1.3); clear()
  315.     end
  316. end
  317. ----------------------------
  318.  
  319. printLogo(); sleep(3)
  320. while true do
  321.     -- Conditionals to keep variables from changing when they shouldn't --
  322.     if selection == 300 then selection = 300
  323.     elseif selection == 41 and bDisplayMore == false then selection = 41
  324.     elseif selection == 42 and bDisplayMore == false then selection = 42
  325.     elseif selection > 9 and bDisplayMore == false then selection = 1
  326.     elseif selection == 0 and bDisplayMore == false then selection = 1
  327.     elseif objectType == 0 then objectType = 1
  328.     elseif selection == 41 or selection == 42 and bDisplayMore == true then selection = 1
  329.     end
  330.     ------------------------------------------------------------------
  331.  
  332.     -- File code for the amount of selections available --
  333.     local sDir = shell.dir()
  334.     local tAll = {}
  335.     if directory == "ROOT" then tAll = fs.list( sDir )
  336.     else tAll = fs.list(directory); directory = directory.."/"..sDir end
  337.     local tFiles = {}
  338.     local tDirs = {}
  339.  
  340.     for n, sItem in pairs( tAll ) do
  341.         if string.sub( sItem, 1, 1 ) ~= "." then
  342.             local sPath = fs.combine( sDir, sItem )
  343.             if fs.isDir( sPath ) then
  344.                 table.insert( tDirs, sItem )
  345.             else
  346.                 table.insert( tFiles, sItem )
  347.             end
  348.         end
  349.     end
  350.     table.sort( tDirs )
  351.     table.sort( tFiles )
  352.     local amntDirs = #tDirs
  353.     local amntFiles = #tFiles
  354.     -------------------------------------------------------
  355.     if selection > #tFiles+1 and bDisplayMore == true then selection = 1
  356.     elseif selection == 0 and bDisplayMore == true then selection = 1 end
  357.    
  358.     clear()
  359.     printScreen()
  360.     event, key = os.pullEvent("key")
  361.    
  362.     if key == 28 and selection == 300 then bDisplayMore = true
  363.     elseif key == 14 and bDisplayMore == true then bDisplayMore = false; shell.setDir("")
  364.     elseif key == 208 and objectType == 2 and bDisplayMore == true and selection == 300 then selection = selection-1
  365.     elseif key == 208 and objectType == 2 and bDisplayMore == true and selection+10 > #tFiles then selection = selection-1
  366.     end
  367.    
  368.     if bCreating == true and key == 29 or key == 157 then bCreating = false; selection = 1; objectType = 0
  369.     elseif key == 29 or key == 157 and bCreating == false then bCreating = true; selection = 41
  370.     elseif key == 205 and selection == 41 then selection = 42
  371.     elseif key == 203 and selection == 42 then selection = 41
  372.     elseif key == 28 and selection == 41 then printCreateMenu(shell.dir())
  373.     elseif key == 28 and selection == 42 then bCreating = false; selection = 1; objectType = 0 -- 0 so the navigateDir does not jump to the first dir
  374.     end
  375.    
  376.     if key == 205 and selection ~= 42 and objectType == 1 then objectType = objectType+1; selection = 1
  377.     elseif key == 205 and bDisplayMore == true and selection+10 < 50 then selection = selection+10
  378.     elseif key == 205 and selection ~= 42 and objectType == 2 then selection = 300
  379.     elseif key == 205 and selection ~= 42 and objectType == 2 then objectType = 2
  380.     elseif key == 203 and selection ~= 42 and objectType == 2 and bDisplayMore == false then objectType = objectType-1; selection = 1
  381.     elseif key == 205 and selection ~= 42 and selection == 1 and bDisplayMore == true then selection = 11
  382.     elseif key == 205 and selection ~= 42 and objectType == 2 and bDisplayMore == true and selection < 21 then selection = selection+10
  383.     elseif key == 203 and selection ~= 42 and objectType > 1 and bDisplayMore == true and selection > 10 then selection = selection-10
  384.    
  385.     elseif key == 200 and objectType == 1 and selection > #tDirs-(#tDirs+1) then selection = selection-1
  386.     elseif key == 208 and objectType == 1 and bDisplayMore == false and selection < #tDirs then selection = selection+1
  387.    
  388.     elseif key == 200 and objectType == 2  and selection > #tFiles-(#tFiles+1) then selection = selection-1
  389.     elseif key == 208 and objectType == 2 and selection < #tFiles then selection = selection+1
  390.    
  391.     elseif key == 28 and objectType == 1 and selection ~= 41 and selection ~= 42 then navigateDir(selection)
  392.     elseif key == 14 then shell.run("cd", ".."); selection = 1
  393.     elseif key == 45 then break
  394.    
  395.     elseif key == 28 and objectType == 2 and bDisplayMore == false and selection ~= 41 and selection ~= 42 then subMenu(selection)
  396.     elseif key == 28 and objectType == 2 and bDisplayMore == true and selection ~= 41 and selection ~= 42 then subMenu(selection+9)
  397.     end
  398. end
  399. clear() -- When loop is broken --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement