Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- CC FileViewer
- v11.10.2013
- change log:
- 1:added select feature, copy/paste, and package tool
- 2: added blacklist
- ]]
- os.pullEvent = os.pullEventRaw
- function FileViewer(path)
- events = {
- key = {
- [208] = function()
- if Sel1 <= #templisting-1 then
- Sel1 = Sel1 + 1
- end
- Sel2 = 1
- end;
- [200] = function()
- if Sel1 ~= 1 then
- Sel1 = Sel1 - 1
- end
- Sel2 = 1
- end;
- [205] = function()
- if Sel2 <= #actions[Ftype]-1 then
- Sel2 = Sel2 + 1
- end
- end;
- [203] = function()
- if Sel2 ~= 1 then
- Sel2 = Sel2 - 1
- end
- end;
- [28] = function()
- if actions[Ftype][Sel2] ~= nil then
- local g = actions[Ftype][Sel2]
- actions[g]()
- else
- end
- end;
- [16] = function()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- ON = false
- end;
- [57] = function()
- if templisting[Sel1] ~= "..." then
- if Seled.sel[templisting[Sel1]] == nil then
- Seled.sel[templisting[Sel1]] = true
- Seled.num = Seled.num + 1
- else
- if Seled.sel[templisting[Sel1]] == true then
- Seled.sel[templisting[Sel1]] = false
- if Seled.num ~= 0 then
- Seled.num = Seled.num - 1
- end
- else
- Seled.sel[templisting[Sel1]] = true
- Seled.num = Seled.num + 1
- end
- end
- end
- end;
- [211] = function()
- actions.delete()
- end;
- };
- mouse_scroll = {
- [-1] = function()
- plus = 16
- while true do
- if templisting[Sel1 + plus] ~= nil then
- Sel1 = Sel1 + plus
- break
- else
- if plus ~= 0 then
- plus = plus - 1
- else
- break
- end
- end
- end
- end;
- [1] = function()
- minus = 16
- while true do
- if templisting[Sel1 + minus] ~= nil then
- Sel1 = Sel1 - minus
- break
- else
- if plus ~= 0 then
- minus = minus - 1
- else
- break
- end
- end
- end
- end;
- };
- }
- actions = {
- ["new"] = function(x)
- TypeSel = true
- while TypeSel do
- types = {"folder","file"}
- term.clear()
- if x ~= nil then
- term.setTextColor(colors.red)
- term.setCursorPos(1,1)
- write(x)
- end
- term.setCursorPos(1,2)
- term.setTextColor(colors.yellow)
- write("New: ")
- for i = 1,#types do
- term.setTextColor(colors.lightGray)
- if Sel3 == i then
- term.setTextColor(colors.yellow)
- end
- write(types[i])
- write(" ")
- end
- _event,p1 = os.pullEvent()
- if _event == "key" then
- if p1 == 205 then
- if Sel3 <= #types-1 then
- Sel3 = Sel3 + 1
- end
- elseif p1 == 203 then
- if Sel3 ~= 1 then
- Sel3 = Sel3 - 1
- end
- elseif p1 == 28 then
- TypeSel = false
- end
- end
- end
- term.setCursorPos(1,4)
- term.setTextColor(colors.yellow)
- write("Name: ")
- FileName = read()
- if fs.exists(path.."/"..FileName) then
- actions.new("File already exists")
- end
- if types[Sel3] == "folder" then
- fs.makeDir(path.."/"..FileName)
- else
- File = fs.open(path.."/"..FileName,"w")
- File.close()
- end
- Sel2 = 1
- end;
- ["open"] = function()
- if Ftype == "..." then
- pathmin = fs.getName(path)
- if pathmin == path then
- path = ""
- else
- pos = string.find(path,pathmin)
- path = string.sub(path,1,pos-2)
- end
- Sel1 = 1
- elseif Ftype == "folder" then
- path = path.."/"..templisting[Sel1]
- Sel1 = 1
- elseif Ftype == "file" then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- shell.run(path.."/"..templisting[Sel1])
- term.setTextColor(colors.red)
- print()
- print("FileViewer: program has finished running press any key to continue")
- _event = os.pullEvent()
- end
- end;
- ["edit"] = function()
- local i = path.."/"..templisting[Sel1]
- shell.run("edit",i)
- Sel2 = 1
- end;
- ["delete"] = function()
- term.setCursorPos(1,Sy)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.setTextColor(colors.yellow)
- items = {"Yes","No"}
- delsel = 1
- write("you sure?: ")
- while true do
- term.setCursorPos(1,Sy)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.setTextColor(colors.yellow)
- write("you sure?: ")
- for i = 1,#items do
- if i == delsel then
- term.setTextColor(colors.yellow)
- else
- term.setTextColor(colors.lightGray)
- end
- write(items[i].." ")
- end
- _event,p1 = os.pullEvent()
- if _event == "key" and p1 == 205 then
- delsel = 2
- elseif _event == "key" and p1 == 203 then
- delsel = 1
- elseif _event == "key" and p1 == 28 then
- if delsel == 1 then
- fs.delete(path.."/"..templisting[Sel1])
- Sel1 = Sel1 - 1
- else
- end
- Sel2 = 1
- break
- end
- end
- end;
- ["package"] = function()
- term.setCursorPos(1,Sy)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.setTextColor(colors.yellow)
- write("Folder Name:")
- Fname = read()
- if fs.makeDir(path.."/"..Fname) then
- fs.makeDir(path.."/"..Fname)
- end
- for name in pairs(Seled.sel) do
- if Seled.sel[name] == true then
- fs.move(path.."/"..name,path.."/"..Fname.."/"..name)
- Seled.sel[name] = false
- Seled.num = Seled.num - 1
- end
- end
- Sel1 = 1
- end;
- ["copy"] = function()
- _copy = path.."/"..templisting[Sel1]
- _copyt = 0
- end;
- ["paste"] = function()
- if _copy ~= nil then
- if fs.exists(path.."/"..fs.getName(_copy)) == false then
- fs.copy(_copy,path.."/"..fs.getName(_copy))
- else
- rn = math.random(1,64)
- if fs.exists(path.."/"..fs.getName(_copy)..rn) == false then
- fs.copy(_copy,path.."/"..fs.getName(_copy)..rn)
- end
- end
- end
- end;
- ["delete_all"] = function()
- for name in pairs(Seled.sel) do
- if Seled.sel[name] == true then
- fs.delete(path.."/"..name)
- Seled.sel[name] = false
- Seled.num = Seled.num - 1
- end
- end
- Sel1 = 1
- end;
- ["rename"] = function()
- term.setCursorPos(1,Sy)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.setTextColor(colors.yellow)
- write("New Name:")
- Fname = read()
- if #Fname ~= 0 then
- shell.run("rename",path.."/"..templisting[Sel1],path.."/"..Fname)
- end
- end;
- ["seled"] = {
- "package";
- "delete_all";
- };
- ["..."] = {
- "open";
- "new";
- "paste";
- };
- ["folder"] = {
- "open";
- "new";
- "rename";
- "copy";
- "paste";
- "delete";
- };
- ["file"] = {
- "open";
- "new";
- "edit";
- "rename";
- "copy";
- "paste";
- "delete";
- };
- }
- local blacklist = {
- password = "1438"; --Change password to what ever you want
- list = {
- --[[
- adding template:
- ["/sample/test/whatever"] = true;
- ]]
- ["/"..shell.getRunningProgram()] = true;
- };
- };
- if path == nil then
- if user == nil then
- if Sys == nil then
- path = ""
- else
- path = Sys.Default
- end
- else
- path = user.default
- end
- end
- Sel1,Sel2,Sel3 = 1,1,1
- Seled = {
- num = 0;
- sel = {};
- }
- selx,sely = 2,3
- _copy = nil
- _copyt = 0
- ON = true
- while ON do
- Sx,Sy = term.getSize()
- if blacklist.list[fs.getName(path)] == true and pass ~= blacklist.password then
- pathmin = fs.getName(path)
- pos = string.find(path,pathmin)
- path = string.sub(path,1,pos-2)
- end
- listing = fs.list(path)
- if path ~= "" then
- templisting = {"..."}
- else
- templisting = {}
- end
- fx,fy = 1,1
- term.setBackgroundColor(colors.gray)
- term.clear()
- term.setCursorPos(fx,fy)
- term.setTextColor(colors.yellow)
- write("path:"..path)
- term.setCursorPos(1,2)
- for i = 1,Sx do
- write("-")
- end
- fx,fy = 2,3
- term.setCursorPos(fx,fy)
- --Find Folders and Files
- for i = 1,#listing do
- if fs.isDir(path.."/"..listing[i]) then
- table.insert(templisting,listing[i])
- end
- end
- for i = 1,#listing do
- if fs.isDir(path.."/"..listing[i]) == false then
- if path.."/"..listing[i] == "/"..shell.getRunningProgram() then
- else
- table.insert(templisting,listing[i])
- end
- end
- end
- -- Draw listing
- if #templisting ~= 0 then
- if fs.isDir(path.."/"..templisting[Sel1]) then
- Ftype = "folder"
- else
- Ftype = "file"
- end
- if templisting[Sel1] == "..." then
- Ftype = "..."
- end
- if Seled.num > 0 then
- Ftype = "seled"
- end
- -- Draw Actions
- term.setCursorPos(1,Sy)
- for i = 1,#actions[Ftype] do
- if i == Sel2 then
- write("["..actions[Ftype][i].."]")
- else
- write(" "..actions[Ftype][i].." ")
- end
- end
- for i = 1,#templisting do
- if i <= 63 then
- term.setCursorPos(fx,fy)
- term.setBackgroundColor(colors.gray)
- if Seled.sel[templisting[i]] == true then
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.yellow)
- term.setCursorPos(fx-1,fy)
- write("^ ")
- end
- if blacklist.list[path.."/"..templisting[i]] == true then
- term.setTextColor(colors.red)
- term.setCursorPos(fx-1,fy)
- write("*")
- end
- if Sel1 == i then
- term.setTextColor(colors.yellow)
- term.setCursorPos(fx-1,fy)
- write(">")
- selx,sely = term.getCursorPos()
- end
- term.setCursorPos(fx,fy)
- term.setTextColor(colors.white)
- if fs.isDir(path.."/"..templisting[i]) then
- term.setTextColor(colors.lime)
- end
- if Sel1 == i then
- write(templisting[i])
- if #templisting[i] > 10 then
- term.setTextColor(colors.red)
- write("|")
- end
- else
- if #templisting[i] < 10 then
- write(templisting[i])
- else
- write(string.sub(templisting[i],1,8).."..")
- end
- end
- fy = fy + 1
- if fy == Sy then
- fx,fy = fx+12,3
- end
- else
- end
- end
- term.setCursorPos(selx,sely)
- term.setTextColor(colors.white)
- if fs.isDir(path.."/"..templisting[Sel1]) then
- term.setTextColor(colors.lime)
- end
- write(templisting[Sel1])
- else
- term.setCursorPos(fx,fy)
- write("Its so lonely in here :(")
- end
- _event,p1 = os.pullEvent()
- if events[_event] ~= nil then
- if events[_event][p1] ~= nil then
- if blacklist.list[path.."/"..templisting[Sel1]] == true and p1 == 28 and actions[Ftype][Sel2] ~= "new" then
- term.setCursorPos(1,Sy)
- term.clearLine()
- term.setTextColor(colors.red)
- write("password:")
- term.setTextColor(colors.yellow)
- pass = read("*")
- if pass == blacklist.password then
- events[_event][p1]()
- else
- end
- else
- events[_event][p1]()
- end
- end
- end
- sleep(.1)
- end
- end
- local targs = {...}
- FileViewer(targs[1])
Advertisement
Add Comment
Please, Sign In to add comment