Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- fsExpose (name by supernicejohn - he is indeed nice)
- Made by Piorjade
- [INFO(offtopic):]
- I did this while I couldn't access my home computer on my MacBook,
- so I had many problems with the keyboard and maybe there are some
- awkward spaces between functions here :P
- [INFO(on this program):]
- -Right window shows every file in the current directory
- -Left window shows the code of a file (click on a file in the right window)
- -Right click to:
- -create a new folder
- -create a new file
- -delete a file/folder
- -copy a file/a whole folder
- -paste a file/a whole folder with the stuff inside
- -edit a file
- -To add in new entries in the right-click menu OR edited the way an entry works:
- -scroll down a bit to the function drawRightClick(), to add in new entries, or edit the text
- -scroll down to _set = {}, and edit the functions or add new ones
- -scroll down to drawScreen(), and down to the comment "Commands" and edit how to handle the return of drawRightClick()
- (or just go to line 571, for the lazy peeps out there)
- -Click on the searchbar to get to a path directly
- -Click on the left arrow to go back to the folder you were before
- -Click on the right arrow to go to the folder, where you were, before you pressed back (much like a normal filebrowser, basically)
- [Current state:]
- -Works quite decent.
- -No bugs currently found.
- -(Yes, cat does not go to the next line if a line in a file is too long to display, this is intended (mainly to prevent glitches I had))
- -I'm going to implement this (with edited commands) into doorX, after I tested it out with the resizeable windows (I didn't test the resize handle yet)
- ]]--
- --[[
- listDir(Path[, true (if you want to get the full path)])
- lists the whole tree in the given directory
- Example:
- listDir("/rom", true)
- Returns:
- /rom/autorun
- /rom/programs/shell
- /rom/programs/ls
- [...]
- ]]
- local function listDir(path, withP)
- local t = {}
- local files = fs.list(path)
- for _, a in ipairs(files) do
- if fs.isDir(path.."/"..a) then
- local tab = listDir(path.."/"..a, true)
- for _, a in ipairs(tab) do
- table.insert(t, a)
- end
- else
- if withP then
- table.insert(t, path.."/"..a)
- else
- table.insert(t, a)
- end
- end
- end
- return t
- end
- --[[
- Preset settings!
- If you want to (e.g. open a file with a different command)
- edit these commands, set the commands :)
- ]]
- _set = {}
- function _set.open(f)
- --runs the file or opens the folder
- if not fs.isDir(f) then
- return shell.run(f)
- else
- table.insert(_history, currentPath)
- currentPath = f
- end
- end
- function _set.copy(f)
- --inserts the file as fdat[FILENAME] = [TEXT]
- fdat = {}
- if fs.isDir(f) then
- nf = string.sub(f, #currentPath)
- _filename = nf
- for _, a in ipairs(listDir(f, true)) do
- local _, file = pcall(fs.open, a, "r")
- if _ then
- local inhalt = file.readAll()
- file.close()
- if string.find(a, currentPath, 1, #currentPath) then
- a = string.sub(a, #currentPath)
- end
- fdat[a] = inhalt
- else
- printErr(file)
- end
- end
- else
- local file = fs.open(f, "r")
- local inhalt = file.readAll()
- file.close()
- f = string.sub(f, #currentPath)
- fdat[f] = inhalt
- end
- end
- function _set.delete(f)
- --deletes the file
- local _, ok, err = pcall(fs.delete, f)
- if not _ then
- printErr(ok)
- end
- end
- function _set.edit(f)
- --edit a file with 'edit <path>'
- shell.run("edit", f)
- end
- function _set.paste(f)
- --paste everything in the fdat table to the current directory
- if #_filename > 0 then
- if string.find(_filename, "/", 1, 1) then _filename = string.sub(_filename, 2) end
- local c = 1
- local str = _filename
- repeat
- if fs.exists(currentPath..str) then
- str = _filename.."-"..tostring(c)
- c = c+1
- else
- break
- end
- until not fs.exists(currentPath..str)
- oldfname = _filename
- _filename = str
- fs.makeDir(currentPath.._filename)
- table.insert(_history, currentPath)
- currentPath = currentPath.._filename
- end
- for a, b in pairs(fdat) do
- local c = 1
- local str = a
- if string.find(str, oldfname, 1, #oldfname) then
- str = string.sub(str, #oldfname+2)
- end
- repeat
- if fs.exists(currentPath..str) then
- str = a.."-"..tostring(c)
- c = c+1
- end
- until not fs.exists(currentPath..str)
- a = str
- local _, file = pcall(fs.open, currentPath..a, "w")
- if _ then
- file.write(b)
- file.close()
- else
- printErr(file)
- end
- end
- fdat = {}
- oldfname = ""
- _filename = ""
- end
- local function clear(bg, fg)
- --[[
- This function clears the screen in the
- desired Background (bg) and the desired
- Foreground (fg) color. (This is very very
- simple :D)
- ]]--
- term.setCursorPos(1,1)
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- term.clear()
- end
- --[[
- Here is the right-click menu
- If you want to add stuff,
- then write your new entry
- and increase "height".
- ]]
- function drawRightClick(x, y)
- --increase (or decrease) this variable to make the menu smaller/bigger (mainly for removing/adding entries)
- local height = 7
- local w = window.create(oldTerm, x, y, 15, height)
- term.redirect(w)
- clear(colors.white, colors.black)
- term.write(" Open ")
- term.setCursorPos(1, 2)
- term.setTextColor(colors.blue)
- term.write(" Copy ")
- term.setCursorPos(1, 3)
- term.setTextColor(colors.red)
- term.write(" Delete ")
- term.setCursorPos(1, 4)
- term.setTextColor(colors.lime)
- term.write(" Paste ")
- term.setCursorPos(1, 5)
- term.setTextColor(colors.black)
- term.write(" New Folder ")
- term.setCursorPos(1, 6)
- term.write(" New File ")
- term.setCursorPos(1, 7)
- term.write(" Edit ")
- --[[
- EXAMPLE NEW ENTRY:
- term.setCursorPos(1,8)
- term.setTextColor(colors.yellow)
- term.write("Secret function")
- ]]
- term.redirect(oldTerm)
- while true do
- local _, button, a, b = os.pullEvent("mouse_click")
- if _ == "mouse_click" and button == 1 and a >= x and a <= x+15-1 and b >= y and b <= y+6 then
- if b == y then
- return "open"
- elseif b == y+1 then
- return "copy"
- elseif b == y+2 then
- return "delete"
- elseif b == y+3 then
- return "paste"
- elseif b == y+4 then
- return "newfolder"
- elseif b == y+5 then
- return "newfile"
- elseif b == y+6 then
- return "edit"
- end
- --[[
- CONTINUEATION OF THE EXAMPLE
- elseif b == y+7 then
- return "secretfunction"
- ]]
- elseif _ == "mouse_click" then
- w.setVisible(false)
- term.redirect(oldTerm)
- w = nil
- break
- end
- end
- end
- --variables
- _ver = 2.1
- _name = "doorX file manager"
- currentPath = "/" --self explaining
- selected = "" --stores the path of the selected file
- _cFiles = {} --stores the files in the current directory
- _history = {} --Stores every path the user is heading
- _last = nil --Stores the path that the user was in, before he pressed back
- _filename = "" --stores the original filename
- _fScroll = 0 --stores the number of times _files did scroll
- _cScroll = 0 --stores the number of times _tree did scroll
- _cMax = 0 --stores the maximum lines of cat
- _cX, _cY = 0, 0 --stores the size of the cat window
- oldfname = "" --saving purposes
- fdat = {} --stores all the listed files and their data
- catTab = {} --stores the cat'ed file in a table (one entry per line)
- maxFiles = 0
- missingFiles = 0
- leftFiles = 0
- --functions
- local function drawButton(text)
- --[[
- Function to draw a blue button with
- the given text.
- (This is basically to prevent from me
- writing that code over and over again)
- ]]--
- local c = term.getBackgroundColor()
- term.setBackgroundColor(colors.blue)
- term.write(text)
- term.setBackgroundColor(c)
- end
- local function redrawScreens()
- local oMaxFiles = maxFiles
- --[[
- Put every file/folder in _cFiles and then
- print them to the right panel and additionally
- every folder to the left panel
- ]]--
- clear(colors.lightGray, colors.white)
- local oldX, oldY = _files.getCursorPos()
- _cFiles = {}
- maxFiles = 0
- missingFiles = 0
- leftFiles = 0
- --list files and clear screen
- _cFiles = fs.list(currentPath)
- term.redirect(_files)
- clear(colors.gray, colors.lime)
- _tree.redraw()
- --print files and folders
- for _, a in ipairs(_cFiles) do
- if fs.isDir(currentPath..a) then
- term.setTextColor(colors.blue)
- end
- term.write(a)
- term.setTextColor(colors.lime)
- local cx, cy = term.getCursorPos()
- term.setCursorPos(1, cy+1)
- end
- _files.setCursorPos(1, 1)
- --store new variables
- term.redirect(oldTerm)
- maxFiles = #_cFiles
- local x, y = _tree.getSize()
- x, y = _files.getSize()
- leftFiles = maxFiles-y
- if leftFiles < 0 then leftFiles = 0 end
- term.redirect(searchBar)
- clear(colors.gray, colors.lime)
- if not string.find(currentPath, "/", 1, 1) then
- currentPath = "/"..currentPath
- end
- if not string.find(currentPath, "/", #currentPath, #currentPath) then currentPath = currentPath.."/" end
- term.write(currentPath)
- local cScroll = 0
- if _fScroll > cScroll and maxFiles >= oMaxFiles then
- repeat
- _files.scroll(1)
- _files.setCursorPos(1, y)
- _files.setTextColor(colors.lime)
- if fs.isDir(currentPath.._cFiles[missingFiles+y+1]) then
- _files.setTextColor(colors.blue)
- end
- _files.write(_cFiles[missingFiles+y+1])
- missingFiles = missingFiles+1
- leftFiles = leftFiles-1
- cScroll = cScroll+1
- until cScroll == _fScroll
- else
- _fScroll = 0
- end
- if leftFiles < 0 then leftFiles = 0 end
- end
- local function rewriteCat()
- _cScroll = 0
- _cMax = 0
- catTab = {}
- _cX, _cY = _tree.getSize()
- if #selected > 0 then
- term.redirect(_tree)
- clear(colors.gray, colors.lime)
- local file, err = fs.open(selected, "r")
- if not file then
- printErr(err)
- return
- end
- repeat
- local str = file.readLine()
- if str then
- table.insert(catTab, str)
- _cMax = _cMax+1
- end
- until str == nil
- file.close()
- term.setCursorPos(1,1)
- clear(colors.gray, colors.lime)
- for _, a in ipairs(catTab) do
- term.write(a)
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y+1)
- end
- term.setCursorPos(1,1)
- term.redirect(oldTerm)
- end
- end
- local function drawScreen()
- --[[
- This is basically the main function that
- controls everything.
- ]]--
- clear(colors.lightBlue, colors.black)
- --create the goback- and goforthbutton + searchbar
- local maxX, maxY = term.getSize()
- sBar = window.create(oldTerm, 1, 1, tonumber(maxX), 1)
- term.redirect(sBar)
- --draw the back and forth buttons
- clear(colors.lightGray, colors.black)
- term.setCursorPos(1,1)
- drawButton("<")
- term.write(" ")
- drawButton(">")
- term.write(" ")
- --draw the searchbar, as a new window (and leave the last pixel again)
- searchBar = window.create(sBar, 5, 1, maxX-5, 1)
- term.redirect(searchBar)
- clear(colors.gray, colors.lime)
- term.write("Enter a path.")
- --draw the left panel, to see the folders in the current path (to move to the folders)
- term.redirect(oldTerm)
- leftPanel = window.create(oldTerm, 1, 2, maxX/2-7, maxY-1)
- term.redirect(leftPanel)
- clear(colors.lightGray, colors.black)
- local lx, ly = term.getSize()
- _tree = window.create(leftPanel, 2, 2, lx-2, ly-2)
- term.redirect(_tree)
- clear(colors.gray, colors.lime)
- term.redirect(oldTerm)
- --draw the right panel, to manage files
- rightPanel = window.create(oldTerm, lx+1, 2, maxX-lx+1, maxY-1)
- term.redirect(rightPanel)
- clear(colors.lightGray, colors.black)
- local rx, ry = term.getSize()
- _files = window.create(rightPanel, 2, 2, rx-2, ry-2)
- term.redirect(_files)
- clear(colors.gray, colors.lime)
- --start the main loop, managing everything
- while true do
- term.redirect(oldTerm)
- redrawScreens()
- local _, button, x, y = os.pullEvent()
- if _ == "term_resize" then
- oMaxX, oMaxY = term.getSize()
- --resize every window again
- --complicated math, kappa
- local x, y = term.getSize()
- sBar.reposition(1, 1, x, 1)
- sBar.redraw()
- term.redirect(sBar)
- x = term.getSize()
- searchBar.reposition(5, 1, x-5, 1)
- searchBar.redraw()
- term.redirect(leftPanel)
- leftPanel.reposition(1, 2, x/2-7, y-1)
- leftPanel.redraw()
- _tree.reposition(2, 2, x/2-9, y-3)
- _tree.redraw()
- term.redirect(rightPanel)
- rightPanel.reposition((x/2-7)+1, 2, x-(x/2-7)+1, y-1)
- rightPanel.redraw()
- _files.reposition(2, 2, x-(x/2-7)-2, y-3)
- _files.redraw()
- term.redirect(oldTerm)
- elseif _ == "mouse_click" and button == 1 and x >= 5 and x < oMaxX and y == 1 then
- term.redirect(searchBar)
- clear(colors.gray, colors.lime)
- local oprint = print
- _G.print = function(t)
- return term.write(t)
- end
- local e = read()
- _G.print = oprint
- if fs.exists(e) and fs.isDir(e) then
- table.insert(_history, currentPath)
- currentPath = e
- term.redirect(oldTerm)
- elseif e == "exit" then
- term.redirect(oldTerm)
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- break
- else
- clear(colors.gray, colors.red)
- term.write("Invalid path.")
- term.redirect(oldTerm)
- sleep(1)
- end
- elseif _ == "mouse_click" and button == 1 and x == 1 and y == 1 then
- if #_history > 0 then
- _last = currentPath
- currentPath = _history[#_history]
- table.remove(_history, #_history)
- end
- elseif _ == "mouse_click" and button == 1 and x == 3 and y == 1 then
- if _last ~= nil then
- table.insert(_history, currentPath)
- currentPath = _last
- _last = nil
- end
- elseif _ == "mouse_scroll" and button == 1 and x >= 2 and x <= oMaxX/2-8 and y >= 3 and y <= oMaxY-2 and _cScroll+_cY <= _cMax then
- _tree.scroll(1)
- _tree.setCursorPos(1, _cY)
- _tree.write(tostring(catTab[_cScroll+_cY-2]))
- _cScroll = _cScroll+1
- elseif _ == "mouse_scroll" and button == -1 and x >= 2 and x <= oMaxX/2-8 and y >= 3 and y <= oMaxY-2 and _cScroll > 0 then
- _tree.scroll(-1)
- _tree.setCursorPos(1, 1)
- _tree.write(tostring(catTab[_cScroll]))
- _cScroll = _cScroll-1
- elseif _ == "mouse_scroll" and button == -1 and x >= (oMaxX/2-8)+2 and x <= oMaxX-2 and y >= 3 and y <= oMaxY-1 and missingFiles > 0 then
- _files.scroll(-1)
- local x, y = _files.getSize()
- _files.setCursorPos(1, y)
- if fs.isDir(currentPath.._cFiles[missingFiles]) then
- _files.setTextColor(colors.blue)
- end
- _files.write(_cFiles[missingFiles])
- _files.setTextColor(colors.lime)
- missingFiles = missingFiles-1
- leftFiles = leftFiles+1
- _fScroll = _fScroll-1
- elseif _ == "mouse_scroll" and button == 1 and x >= (oMaxX/2-8)+2 and x <= oMaxX-2 and y >= 3 and y <= oMaxY-1 and leftFiles > 0 then
- _files.scroll(1)
- local x, y = _files.getSize()
- _files.setCursorPos(1, y)
- if fs.isDir(currentPath.._cFiles[missingFiles+y+1]) then
- _files.setTextColor(colors.blue)
- end
- _files.write(_cFiles[missingFiles+y+1])
- _files.setTextColor(colors.lime)
- missingFiles = missingFiles+1
- leftFiles = leftFiles-1
- _fScroll = _fScroll+1
- elseif _ == "mouse_click" and button == 1 and x >= (oMaxX/2-8)+2 and x <= oMaxX-2 and y >= 3 and y <= oMaxY-1 then
- local realY = y
- local y = y-2
- if _cFiles[missingFiles+y] then
- selected = currentPath.._cFiles[missingFiles+y]
- rewriteCat()
- end
- elseif _ == "mouse_click" and button == 2 and x >= (oMaxX/2-8)+2 and x <= oMaxX-2 and y >= 3 and y <= oMaxY-1 then
- --[[
- Commands
- If you added new entries,
- Write your way of handling the click.
- ]]
- local realY = y
- local y = y-2
- local invert = false
- if 19-realY < 7 then invert = true end
- local command = ""
- if not invert then
- command = drawRightClick(x, realY, false)
- else
- command = drawRightClick(x, 12, false)
- end
- if command == "open" and _cFiles[missingFiles+y] then
- _set.open(currentPath.._cFiles[missingFiles+y])
- elseif command == "copy" and _cFiles[missingFiles+y] then
- fileName = _cFiles[missingFiles+y]
- local byteinhalt = _set.copy(currentPath.._cFiles[missingFiles+y])
- fileData = byteinhalt
- elseif command == "delete" and _cFiles[missingFiles+y] then
- _set.delete(currentPath.._cFiles[missingFiles+y])
- elseif command == "newfolder" then
- term.redirect(searchBar)
- clear(colors.gray, colors.lime)
- term.write(currentPath)
- local e = read()
- if string.find(e, "%.%.") then
- printErr("Invalid path.")
- sleep(2)
- elseif string.find(e, "//") then
- printErr("Invalid path.")
- sleep(2)
- elseif fs.exists(currentPath..e) then
- printErr("Already exists.")
- sleep(2)
- else
- fs.makeDir(currentPath..e)
- end
- term.redirect(oldTerm)
- elseif command == "paste" then
- _set.paste()
- elseif command == "edit" and _cFiles[missingFiles+y] and not fs.isDir(_cFiles[missingFiles+y]) then
- _set.edit(currentPath.._cFiles[missingFiles+y])
- elseif command == "newfile" then
- term.redirect(searchBar)
- clear(colors.gray, colors.lime)
- term.write(currentPath)
- local oprint = _G.print
- _G.print = function(f)
- term.write(f)
- end
- _G.print = oprint
- local e = read()
- if string.find(e, "%.%.") then
- printErr("Invalid path.")
- sleep(2)
- elseif string.find(e, "//") then
- printErr("Invalid path.")
- sleep(2)
- elseif fs.exists(currentPath..e) then
- printErr("Already exists.")
- sleep(2)
- else
- local ff = fs.open(currentPath..e, "w")
- ff.close()
- end
- end
- end
- end
- end
- --code
- --catch the original terminal
- oldTerm = term.current()
- oMaxX, oMaxY = term.getSize()
- function printErr(str)
- term.redirect(searchBar)
- clear(colors.gray, colors.red)
- term.write(str)
- term.redirect(oldTerm)
- end
- drawScreen()
Add Comment
Please, Sign In to add comment