Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Client Side File Browser by PaymentOption for NeXuS --
- VERSION = "1.0"
- ---------------------------------------------------------
- -- VARS --
- selection = 1
- directory = "ROOT"
- subSelection = 1
- objectType = 1 -- To tell what type of file is in the selection; 1 = directory, 2 = file
- bDisplayMore = false
- bCreating = false
- ----------
- -- Helper Functions --
- function list(sDir) -- Written by dan200; the creator of ComputerCraft
- local tAll = {}
- if directory == "ROOT" then tAll = fs.list( "" )
- else tAll = fs.list( directory) end
- local tFiles = {}
- local tDirs = {}
- for n, sItem in pairs( tAll ) do
- if string.sub( sItem, 1, 1 ) ~= "." then
- local sPath = fs.combine( sDir, sItem )
- if fs.isDir( sPath ) then
- table.insert( tDirs, sItem )
- else
- table.insert( tFiles, sItem )
- end
- end
- end
- table.sort( tDirs )
- table.sort( tFiles )
- local y = 5 --For the printing of the files/dirs
- local stopNum = 9
- term.setCursorPos(4, 4); print("Directories: ")
- term.setCursorPos(24, 4); print("Files: "); term.setCursorPos(4, 5)
- if bDisplayMore == true then
- clear(); local index = 10; local x = 5
- objectType = 2
- printBorder()
- if shell.dir() == "" then cPrint(2, "Directory: ROOT")
- else cPrint(2, "Directory: "..shell.dir()) end
- rPrint(17, "Version: "..VERSION.." *")
- y = 6
- term.setCursorPos(4, 4); write("Files: "); y = 5; term.setCursorPos(x, y)
- repeat
- if objectType == 2 and selection == index-9 then write("["..tostring(tFiles[index]).."]"); term.setCursorPos(x, y+1); y = y+1
- else write(" "..tostring(tFiles[index]).." "); term.setCursorPos(x, y+1); y = y+1 end
- if index == 18 then x = 16; y = 4 end
- if index == 28 then x = 26; y = 4 end
- if index == 38 then x = 36; y = 4 end
- if index >= 49 then break end
- index = index+1
- until index == #tFiles+1
- return
- end
- for i,v in pairs(tDirs) do
- if objectType == 1 and selection == i then write("["..v.."]"); term.setCursorPos(4, y+1); y = y+1
- else write(" "..v.." "); term.setCursorPos(4, y+1); y = y+1 end
- if i == stopNum then
- bMore = true
- term.setCursorPos(38, 15)
- if selection == 300 then write("[More Dirs]")
- else write(" More Dirs ") end
- break
- end
- end
- y = 5; term.setCursorPos(24, 5)
- for i,v in pairs(tFiles) do
- if objectType == 2 and selection == i then write("["..v.."]"); term.setCursorPos(24, y+1); y = y+1
- else write(" "..v.." "); term.setCursorPos(24, y+1); y = y+1 end
- if i == stopNum then
- bMore = true
- term.setCursorPos(36, 15)
- if selection == 300 then write("[More Files]") -- 300 Because that is an unreachable selection otherwise
- else write(" More Files ") end
- break
- end
- end
- end
- function cPrint(height, string)
- local w,h = term.getSize()
- local xPos = w/2 - string.len(string)/2
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- function rPrint(height, string)
- local w,h = term.getSize()
- local xPos = w - string.len(string)
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- function printBorder()
- local w,h = term.getSize()
- write(" ".. string.rep('*', w-2).."\n")
- for i=1, h-2 do write(" "..'*'..string.rep(" ", w-4)..'*'.."\n") end
- write(" ".. string.rep('*', w-2))
- end
- function printCreateMenu(sPath)
- local filename = ""; local w,h = term.getSize()
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Enter new file name: "); filename = read()
- if fs.exists(tostring(sPath).."/"..tostring(filename)) == false then
- if not shell.dir() == "" then shell.run("edit", tostring(sPath).."/"..tostring(filename))
- else shell.run("edit", tostring(filename)) end
- else
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write(tostring(filename).." already exists"); sleep(1.3); clear()
- end
- end
- function printCtrlMenu()
- if selection == 41 then term.setCursorPos(3, 17); write("[Create] Exit")
- elseif selection == 42 then term.setCursorPos(3, 17); write(" Create [Exit]")
- else term.setCursorPos(3, 17); write("Ctrl to create file")
- end
- end
- function clear() term.clear(); term.setCursorPos(1,1) end
- ----------------------
- -- Menues and other screens --
- function printScreen()
- clear()
- printBorder()
- if shell.dir() == "" then cPrint(2, "Directory: ROOT")
- else cPrint(2, "Directory: "..shell.dir()) end
- rPrint(17, "Version: "..VERSION.." *")
- if bDisplayMore == false then printCtrlMenu() end
- listFiles()
- end
- function printLogo()
- clear()
- print(" _______ ____ ___ _________")
- print(" \\ \\ ____ \\ \\/ /__ __/ _____/")
- print(" / | \\_/ __ \\ \\ /| | \\_____ \\ ")
- print(" / | \\ ___/ / \\| | / \\")
- print(" \\____|__ /\\___ >/___/\\ \\____/_______ /")
- print(" \\/ \\/ \\_/ \\/ ")
- term.setCursorPos(4, 11); write("Controls: Enter = Change Dir/File Options,")
- term.setCursorPos(4, 12); write("Arrows = Selection Change, Back = Return to")
- term.setCursorPos(4, 13); write("ROOT, X = Exit Program")
- cPrint(15, "File Browser by PaymentOption")
- rPrint(18, "Spaghetti Coded!")
- end
- function printMenu()
- if subSelection == 1 then term.setCursorPos(4, 15); write("[Edit]")-- +6
- else term.setCursorPos(4, 15); write(" Edit ") end
- if subSelection == 2 then term.setCursorPos(10, 15); write("[Rename]") -- +9
- else term.setCursorPos(10, 15); write(" Rename ") end
- if subSelection == 3 then term.setCursorPos(19, 15); write("[Delete]") -- +8
- else term.setCursorPos(19, 15); write(" Delete ") end
- if subSelection == 4 then term.setCursorPos(27, 15); write("[Run]")
- else term.setCursorPos(27, 15); write(" Run ") end -- +5
- if subSelection == 5 then term.setCursorPos(32, 15); write("[Exit]") -- +6
- else term.setCursorPos(32, 15); write(" Exit ") end
- end
- function subMenu(fileNum)
- local tAll = {}; local w,h = term.getSize(); local sDir = shell.dir()
- if directory == "ROOT" then tAll = fs.list( "" )
- else tAll = fs.list( directory) end
- local tFiles = {}
- local tDirs = {}
- for n, sItem in pairs( tAll ) do
- if string.sub( sItem, 1, 1 ) ~= "." then
- local sPath = fs.combine( sDir, sItem )
- if fs.isDir( sPath ) then
- table.insert( tDirs, sItem )
- else
- table.insert( tFiles, sItem )
- end
- end
- end
- table.sort( tDirs )
- table.sort( tFiles )
- for i,v in pairs(tFiles) do
- if fileNum == i then
- while true do
- term.setCursorPos(1, 15); term.clearLine();
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* "); printMenu()
- local event, key = os.pullEvent("key")
- if key == 205 and subSelection < 5 then subSelection = subSelection+1
- elseif key == 203 and subSelection > 1 then subSelection = subSelection-1
- elseif key == 28 and subSelection == 1 then edit(tostring(shell.dir()).."/"..v); break
- elseif key == 28 and subSelection == 2 then renameFile(tostring(shell.dir()).."/"..v); break
- elseif key == 28 and subSelection == 3 then delete(tostring(shell.dir()).."/"..v); selection = selection-1; break
- elseif key == 28 and subSelection == 4 then shell.run(tFiles[i]); return
- elseif key == 28 and subSelection == 5 then break
- end
- end
- end
- end
- end
- ------------------------------
- -- File related Functions --
- function listFiles()
- local sDir = shell.dir()
- if sDir ~= "" and sDir ~= "/" then directory = tostring(sDir)
- else directory = "ROOT" end
- list(sDir)
- end
- function navigateDir(DirNum)
- local sDir = shell.dir(); local w,h = term.getSize()
- local tAll = fs.list( sDir )
- local tFiles = {}
- local tDirs = {}
- for n, sItem in pairs( tAll ) do
- if string.sub( sItem, 1, 1 ) ~= "." then
- local sPath = fs.combine( sDir, sItem )
- if fs.isDir( sPath ) then
- table.insert( tDirs, sItem )
- else
- table.insert( tFiles, sItem )
- end
- end
- end
- table.sort( tDirs )
- table.sort( tFiles )
- for i,v in pairs(tDirs) do
- if DirNum == i and directory ~= "ROOT" then shell.setDir(directory.."/"..v); return
- elseif DirNum == i and directory == "ROOT" then shell.setDir(v); return end
- end
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot navigate to that Directory"); sleep(1.3); clear()
- end
- function edit(sPath)
- local w,h = term.getSize()
- term.setCursorPos(1, 15); term.clearLine()
- write(" *"..string.rep(" ", w-4).."* ")
- if fs.exists(tostring(sPath)) == false then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot edit a non existant file"); sleep(1.3); clear()
- elseif fs.isDir(tostring(sPath)) then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot edit a directory"); sleep(1.3); clear()
- elseif string.find(tostring(sPath), "rom/") then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot edit read only files"); sleep(1.3); clear()
- elseif shell.dir() ~= "" or shell.dir() == "/" then
- shell.setDir(""); shell.run("edit", tostring(sPath))
- else shell.run("edit", tostring(sPath)) end
- end
- function renameFile(sPath)
- local w,h = term.getSize(); local newname = ""
- term.setCursorPos(1, 15); term.clearLine()
- write(" *"..string.rep(" ", w-4).."* ")
- if fs.exists(tostring(sPath)) == false then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot rename a nonexistant file"); sleep(1.3); clear()
- elseif fs.isDir(tostring(sPath)) then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot rename a directory"); sleep(1.3); clear()
- elseif string.find(tostring(sPath), "rom/") then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot rename a read only file"); sleep(1.3); clear()
- else
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Rename to: "); newname = read(); clear()
- if directory == "ROOT" then
- local file = fs.open(tostring(sPath), "r"); local fileContents = file.readAll(); file.close()
- fs.delete(tostring(sPath)); file = fs.open(tostring(newname), "w"); file.write(fileContents); file.close()
- else
- local file = fs.open(directory..tostring(sPath), "r"); local fileContents = file.readAll(); file.close()
- fs.delete(tostring(sPath)); file = fs.open(shell.dir()..tostring(newname), "w"); file.write(fileContents); file.close()
- end
- end
- end
- function delete(sPath)
- local w,h = term.getSize()
- term.setCursorPos(1, 15); term.clearLine()
- write(" *"..string.rep(" ", w-4).."* ")
- if string.find(tostring(sPath), "rom/") then
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Cannot delete read only files"); sleep(1.3); clear()
- else
- fs.delete(tostring(sPath))
- term.setCursorPos(1, 15); term.clearLine(); write(" *"..string.rep(" ", w-4).."* ")
- term.setCursorPos(4, 15); write("Deleted"); sleep(1.3); clear()
- end
- end
- ----------------------------
- printLogo(); sleep(3)
- while true do
- -- Conditionals to keep variables from changing when they shouldn't --
- if selection == 300 then selection = 300
- elseif selection == 41 and bDisplayMore == false then selection = 41
- elseif selection == 42 and bDisplayMore == false then selection = 42
- elseif selection > 9 and bDisplayMore == false then selection = 1
- elseif selection == 0 and bDisplayMore == false then selection = 1
- elseif objectType == 0 then objectType = 1
- elseif selection == 41 or selection == 42 and bDisplayMore == true then selection = 1
- end
- ------------------------------------------------------------------
- -- File code for the amount of selections available --
- local sDir = shell.dir()
- local tAll = {}
- if directory == "ROOT" then tAll = fs.list( sDir )
- else tAll = fs.list(directory); directory = directory.."/"..sDir end
- local tFiles = {}
- local tDirs = {}
- for n, sItem in pairs( tAll ) do
- if string.sub( sItem, 1, 1 ) ~= "." then
- local sPath = fs.combine( sDir, sItem )
- if fs.isDir( sPath ) then
- table.insert( tDirs, sItem )
- else
- table.insert( tFiles, sItem )
- end
- end
- end
- table.sort( tDirs )
- table.sort( tFiles )
- local amntDirs = #tDirs
- local amntFiles = #tFiles
- -------------------------------------------------------
- if selection > #tFiles+1 and bDisplayMore == true then selection = 1
- elseif selection == 0 and bDisplayMore == true then selection = 1 end
- clear()
- printScreen()
- event, key = os.pullEvent("key")
- if key == 28 and selection == 300 then bDisplayMore = true
- elseif key == 14 and bDisplayMore == true then bDisplayMore = false; shell.setDir("")
- elseif key == 208 and objectType == 2 and bDisplayMore == true and selection == 300 then selection = selection-1
- elseif key == 208 and objectType == 2 and bDisplayMore == true and selection+10 > #tFiles then selection = selection-1
- end
- if bCreating == true and key == 29 or key == 157 then bCreating = false; selection = 1; objectType = 0
- elseif key == 29 or key == 157 and bCreating == false then bCreating = true; selection = 41
- elseif key == 205 and selection == 41 then selection = 42
- elseif key == 203 and selection == 42 then selection = 41
- elseif key == 28 and selection == 41 then printCreateMenu(shell.dir())
- elseif key == 28 and selection == 42 then bCreating = false; selection = 1; objectType = 0 -- 0 so the navigateDir does not jump to the first dir
- end
- if key == 205 and selection ~= 42 and objectType == 1 then objectType = objectType+1; selection = 1
- elseif key == 205 and bDisplayMore == true and selection+10 < 50 then selection = selection+10
- elseif key == 205 and selection ~= 42 and objectType == 2 then selection = 300
- elseif key == 205 and selection ~= 42 and objectType == 2 then objectType = 2
- elseif key == 203 and selection ~= 42 and objectType == 2 and bDisplayMore == false then objectType = objectType-1; selection = 1
- elseif key == 205 and selection ~= 42 and selection == 1 and bDisplayMore == true then selection = 11
- elseif key == 205 and selection ~= 42 and objectType == 2 and bDisplayMore == true and selection < 21 then selection = selection+10
- elseif key == 203 and selection ~= 42 and objectType > 1 and bDisplayMore == true and selection > 10 then selection = selection-10
- elseif key == 200 and objectType == 1 and selection > #tDirs-(#tDirs+1) then selection = selection-1
- elseif key == 208 and objectType == 1 and bDisplayMore == false and selection < #tDirs then selection = selection+1
- elseif key == 200 and objectType == 2 and selection > #tFiles-(#tFiles+1) then selection = selection-1
- elseif key == 208 and objectType == 2 and selection < #tFiles then selection = selection+1
- elseif key == 28 and objectType == 1 and selection ~= 41 and selection ~= 42 then navigateDir(selection)
- elseif key == 14 then shell.run("cd", ".."); selection = 1
- elseif key == 45 then break
- elseif key == 28 and objectType == 2 and bDisplayMore == false and selection ~= 41 and selection ~= 42 then subMenu(selection)
- elseif key == 28 and objectType == 2 and bDisplayMore == true and selection ~= 41 and selection ~= 42 then subMenu(selection+9)
- end
- end
- clear() -- When loop is broken --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement