Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Name: Blaze File Browser (w/ LuaIDE)
- Version: 1.0.0
- Creator: DannySMc (dannysmc95)
- Description: A simplistic file browser
- License:
- This work is licensed under the Creative Commons
- Attribution-NonCommercial-NoDerivatives 4.0 International
- License. To view a copy of this license, visit
- http://creativecommons.org/licenses/by-nc-nd/4.0/.
- You are free to:
- Share — copy and redistribute the material in any medium or format
- (The licensor cannot revoke these freedoms as long as you follow the license terms.)
- Under the following terms:
- Attribution — You must give appropriate credit, provide a link to
- the license, and indicate if changes were made. You may do so in
- any reasonable manner, but not in any way that suggests the licensor
- endorses you or your use.
- NonCommercial — You may not use the material for commercial purposes.
- NoDerivatives — If you remix, transform, or build upon the material,
- you may not distribute the modified material.
- No additional restrictions — You may not apply legal terms or technological
- measures that legally restrict others from doing anything the license permits.
- ]]
- -- Set Vars:
- scroll = 1
- histFold = {}
- table.insert(histFold, "/")
- function fb_getClick(sButton, nX, nY, scroll, sDir)
- if sButton == 1 then
- local tFilesClean = tFilesClean
- tFilesCount = #tFilesClean
- local count = nY - 2 - scroll
- oldDir = sDir
- if sDir == "/" then
- newDir = sDir..""..(tFilesClean[count])
- else
- newDir = sDir.."/"..tFilesClean[count]
- end
- sDir = newDir
- if fs.isDir(newDir) then
- table.insert(histFold, sDir)
- fb_finder(newDir)
- else
- fb_runFile(newDir, sDir)
- end
- elseif sButton == 2 then
- -- Do delete or edit
- if sOption == "delete" then
- local tFilesClean = tFilesClean
- tFilesCount = #tFilesClean
- local count = nY - 2 - scroll
- oldDir = sDir
- if sDir == "/" then
- newDir = sDir..""..(tFilesClean[count])
- else
- newDir = sDir.."/"..tFilesClean[count]
- end
- fs.delete(newDir)
- fb_finder(sDir)
- elseif sOption == "edit" then
- -- Empty
- else
- fb_drawOptions(nX, nY, sDir)
- end
- end
- end
- function fb_drawOptions(tempX, tempY, sDir)
- drawBox(tempX, 8, tempY, 2, " ", "black", "lightGrey")
- printA("Delete", tempX, tempY, false, "black", "lightGrey")
- printA("Edit", tempX, tempY + 1, false, "black", "lightGrey")
- while true do
- local args = { os.pullEvent() }
- if args[1] == "mouse_click" then
- if args[2] == 1 then
- if args[3] >= tempX and args[3] <= tempX + 8 then
- if args[4] == tempY then
- local tFilesClean = tFilesClean
- tFilesCount = #tFilesClean
- local count = tempY - 2 - scroll
- oldDir = sDir
- if sDir == "/" then
- newDir = sDir..""..(tFilesClean[count])
- else
- newDir = sDir.."/"..tFilesClean[count]
- end
- fs.delete(newDir)
- fb_finder(sDir)
- elseif args[4] == tempY + 1 then
- local tFilesClean = tFilesClean
- tFilesCount = #tFilesClean
- local count = tempY - 2 - scroll
- oldDir = sDir
- if sDir == "/" then
- newDir = sDir..""..(tFilesClean[count])
- else
- newDir = sDir.."/"..tFilesClean[count]
- end
- shell.run("edit "..newDir)
- fb_finder(sDir)
- else
- fb_finder(sDir)
- end
- else
- fb_finder(sDir)
- end
- else
- fb_finder(sDir)
- end
- end
- end
- end
- function fb_runFile(path, sDir)
- cs()
- setCol("black", "white")
- shell.run(path)
- fb_finder(sDir)
- end
- function fb_drawMenu(sDir)
- -- Get files
- maintc = "blue"
- mainbc = "grey"
- mainbc2 = "lightGrey"
- texttc = "black"
- textbc = "white"
- dirtc = "red"
- xtc = "lime"
- sidetc = "white"
- sidebc = "cyan"
- sidetc1 = "lime"
- setCol("white", "white")
- cs()
- drawBox(1, 51, 1, 1, " ", maintc, mainbc)
- printA(" "..time().." ", 44, 1, false, maintc, mainbc2)
- printA("X", 1, 1, false, xtc, dirtc)
- printA(" Path: ", 3, 1, false, texttc, mainbc2)
- printA(sDir.." ", 10, 1, false, dirtc, mainbc2)
- drawBox(40, 12, 2, 18, " ", sidetc, sidebc)
- drawBox(41, 10, 3, 16, " ", sidetc, sidebc)
- drawBox(42, 8, 4, 14, " ", sidetc, sidebc)
- drawBox(43, 6, 5, 12, " ", sidetc, sidebc)
- drawBox(44, 4, 6, 10, " ", sidetc, sidebc)
- drawBox(45, 2, 7, 8, " ", sidetc, sidebc)
- printA("< ..", 2, 3, false, "blue", "white")
- printA("Create", 41, 3, false, sidetc1, sidebc)
- printA("File", 43, 4, false, sidetc, sidebc)
- printA("Folder", 43, 5, false, sidetc, sidebc)
- setCol(texttc, textbc)
- end
- function fb_finder(sDir)
- -- Get files
- fb_drawMenu(sDir)
- fb_drawDir(sDir, scroll)
- while true do
- local args = { os.pullEvent() }
- if args[1] == "timer" then
- fb_drawMenu(sDir)
- fb_drawDir(sDir, scroll)
- elseif args[1] == "mouse_scroll" then
- if args[2] == 1 then
- scroll = scroll + 1
- fb_drawMenu(sDir)
- fb_drawDir(sDir, scroll)
- elseif args[2] == -1 then
- scroll = scroll - 1
- fb_drawMenu(sDir)
- fb_drawDir(sDir, scroll)
- end
- elseif args[1] == "mouse_click" then
- if args[3] == 1 and args[4] == 1 then
- if RunningUnder == "Verinian" then
- home()
- else
- setCol("white", "black")
- break
- end
- elseif (args[3] >= 2 and args[3] <= 40) and (args[4] == 3) then
- table.remove(histFold)
- historyCount = #histFold
- sDir = histFold[historyCount]
- fb_finder(sDir)
- elseif (args[3] >= 2 and args[3] <= 40) and (args[4] >= 4 and args[4] <= 18) then
- fb_getClick(args[2], args[3], args[4], scroll, sDir)
- elseif (args[3] >= 43 and args[3] <= 50) then
- if args[4] == 4 then
- fb_drawInput("file", sDir)
- elseif args[4] == 5 then
- fb_drawInput("folder", sDir)
- end
- end
- end
- end
- end
- function fb_drawInput(object, sDir)
- poptc1 = "white"
- popbc1 = "red"
- poptc2 = "black"
- popbc2 = "white"
- if object == "file" then
- drawBox(6, 39, 6, 3, " ", poptc1, popbc1)
- drawBox(6, 39, 7, 1, " ", poptc1, popbc1)
- drawBox(7, 37, 7, 1, " ", poptc2, popbc2)
- printC("Name For File:", 6, false, poptc1, popbc1)
- setCol(poptc2, popbc2)
- term.setCursorPos(7,7)
- write(": ")
- local newFileName = tostring(read())
- if sDir == "/" then
- fileDirName = "/"..newFileName
- else
- fileDirName = sDir.."/"..newFileName
- end
- shell.run("edit "..fileDirName)
- fb_finder(sDir)
- elseif object == "folder" then
- drawBox(6, 39, 6, 3, " ", poptc1, popbc1)
- drawBox(6, 39, 7, 1, " ", poptc1, popbc1)
- drawBox(7, 37, 7, 1, " ", poptc2, popbc2)
- printC("Name For Folder:", 6, false, poptc1, popbc1)
- setCol(poptc2, popbc2)
- term.setCursorPos(7,7)
- write(": ")
- local newFolderName = tostring(read())
- if sDir == "/" then
- fileDirName = "/"..newFolderName
- else
- fileDirName = sDir.."/"..newFolderName
- end
- fs.makeDir(fileDirName)
- fb_finder(sDir)
- end
- end
- -- Code by: wieselkatze (Modified by dannysmc95)
- local function fb_getDir( sDir )
- sDir = ( "/" .. sDir .. "/" ):gsub( "\\+", "/" ):gsub( "/+", "/" )
- local folder, list = sDir:match( ".*/" ), {}
- tFilesClean = {}
- if not fs.isDir( sDir ) then
- return {}
- end
- for k, v in pairs( fs.list( sDir ) ) do
- if fs.isDir( folder .. v ) then
- table.insert( list, 1, "+ " .. v )
- table.insert( tFilesClean, 1, v)
- else
- list[ #list+1 ] = "- " .. v
- tFilesClean[ #tFilesClean + 1 ] = v
- end
- end
- return list
- end
- function fb_drawDir(sDir, scroll)
- local intX = 2
- local intY = 4
- tFiles = fb_getDir(sDir)
- for k, v in ipairs(tFiles) do
- if k >= scroll and k <= 15 then
- term.setCursorPos(intX, intY)
- if v:find("+") then
- setCol("blue", "white")
- print(v)
- else
- setCol("red", "white")
- print(v)
- end
- intY = intY + 1
- end
- end
- end
- function fb_main()
- if fs.exists("vde.lua") then
- RunningUnder = "Verinian"
- else
- RunningUnder = "Unknown"
- end
- fb_finder("/")
- end
- -- Configuration File Protocols
- function saveConfig(table, file)
- fConfig = fs.open(file, "w") or error("Cannot open file "..file, 2)
- fConfig.write(textutils.serialize(table))
- fConfig.close()
- end
- function loadConfig(file)
- fConfig = fs.open(file, "r")
- ret = textutils.unserialize(fConfig.readAll())
- return ret
- end
- -- Peripheral Functions
- function findPeripheral(Perihp) --Returns side of first matching peripheral matching passed string
- for _,s in ipairs(rs.getSides()) do
- if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
- return s
- end
- end
- return false
- end
- -- Common Draw Functions
- function cs()
- term.clear()
- term.setCursorPos(1,1)
- return
- end
- function setCol(textColour, backgroundColour)
- if textColour and backgroundColour then
- if term.isColour() then
- term.setTextColour(colours[textColour])
- term.setBackgroundColour(colours[backgroundColour])
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function resetCol()
- if term.isColour then
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.black)
- return true
- else
- return false
- end
- end
- -- Print Functions
- function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
- local x, y = term.getSize()
- x = x/2 - #Text/2
- term.setCursorPos(x, Line)
- if Color then setCol(Color, BkgColor) end
- term.write(Text)
- if NextLine then
- term.setCursorPos(1, NextLine)
- end
- if Color then resetCol(Color, BkgColor) end
- return true
- end
- function printL(Text, Line, NextLine, Color, BkgColor) -- print line
- local x, y = term.getSize()
- if ((term.isColor) and (term.isColor() == false) and (Text == " ")) then Text = "-" end
- for i = 1, x do
- term.setCursorPos(i, Line)
- if Color then setCol(Color, BkgColor) end
- term.write(Text)
- end
- if NextLine then
- term.setCursorPos(1, NextLine)
- end
- if Color then resetCol(Color, BkgColor) end
- return true
- end
- function printA(Text, xx, yy, NextLine, Color, BkgColor) -- print anywhere
- term.setCursorPos(xx,yy)
- if Color then setCol(Color, BkgColor) end
- term.write(Text)
- if NextLine then
- term.setCursorPos(1, NextLine)
- end
- if Color then resetCol(Color, BkgColor) end
- return true
- end
- function clearLine(Line, NextLine) -- May seem a bit odd, but it may be usefull sometimes
- local x, y = term.getSize()
- for i = 1, x do
- term.setCursorPos(i, Line)
- term.write(" ")
- end
- if not NextLine then
- x, y = term.getCursorPos()
- term.setCursorPos(1, y+1)
- end
- return true
- end
- function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
- local x, y = term.getSize()
- if Color then setCol(Color, BkgColor) end
- if not Text then Text = "*" end
- lengthX = lengthX - 1
- lengthY = lengthY - 1
- EndX = StartX + lengthX
- EndY = StartY + lengthY
- term.setCursorPos(StartX, StartY)
- term.write(string.rep(Text, lengthX))
- term.setCursorPos(StartX, EndY)
- term.write(string.rep(Text, lengthX))
- for i = StartY, EndY do
- term.setCursorPos(StartX, i)
- term.write(Text)
- term.setCursorPos(EndX, i)
- term.write(Text)
- end
- resetCol(Color, BkgColor)
- return true
- end
- function time()
- local nTime = textutils.formatTime(os.time(), true)
- if string.len(nTime) == 4 then
- nTime = "0"..nTime
- end
- os.startTimer(0.5)
- return nTime
- end
- fb_main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement