Yuri_Pearce

FileVeiwer v2 by Yuri

Nov 10th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.17 KB | None | 0 0
  1. --[[
  2.     CC FileViewer
  3.     v11.10.2013
  4.     change log:
  5.         1:added select feature, copy/paste, and package tool
  6.         2: added blacklist
  7. ]]
  8. os.pullEvent = os.pullEventRaw
  9. function FileViewer(path)
  10.         events = {
  11.             key = {
  12.                 [208] = function()
  13.                     if Sel1 <= #templisting-1 then
  14.                         Sel1 = Sel1 + 1
  15.                     end
  16.                     Sel2 = 1
  17.                 end;
  18.                 [200] = function()
  19.                     if Sel1 ~= 1 then
  20.                         Sel1 = Sel1 - 1
  21.                     end
  22.                     Sel2 = 1
  23.                 end;
  24.                 [205] = function()
  25.                     if Sel2 <= #actions[Ftype]-1 then
  26.                         Sel2 = Sel2 + 1
  27.                     end
  28.                 end;
  29.                 [203] = function()
  30.                     if Sel2 ~= 1 then
  31.                         Sel2 = Sel2 - 1
  32.                     end
  33.                 end;
  34.                 [28] = function()
  35.                     if actions[Ftype][Sel2] ~= nil then
  36.                         local g = actions[Ftype][Sel2]
  37.                         actions[g]()
  38.                     else
  39.                        
  40.                     end
  41.                 end;
  42.                 [16] = function()
  43.                     term.setBackgroundColor(colors.black)
  44.                     term.clear()
  45.                     term.setCursorPos(1,1)
  46.                     ON = false
  47.                 end;
  48.                 [57] = function()
  49.                     if templisting[Sel1] ~= "..." then
  50.                         if Seled.sel[templisting[Sel1]] == nil then
  51.                             Seled.sel[templisting[Sel1]] = true
  52.                             Seled.num = Seled.num + 1
  53.                         else
  54.                             if Seled.sel[templisting[Sel1]] == true then
  55.                                 Seled.sel[templisting[Sel1]] = false
  56.                                 if Seled.num ~= 0 then
  57.                                     Seled.num = Seled.num - 1
  58.                                 end
  59.                             else
  60.                                 Seled.sel[templisting[Sel1]] = true
  61.                                 Seled.num = Seled.num + 1
  62.                             end
  63.                         end
  64.                     end
  65.                 end;
  66.                 [211] = function()
  67.                     actions.delete()
  68.                 end;
  69.             };
  70.             mouse_scroll = {
  71.                 [-1] = function()
  72.                     plus = 16
  73.                     while true do
  74.                         if templisting[Sel1 + plus] ~= nil then
  75.                             Sel1 = Sel1 + plus
  76.                             break
  77.                         else
  78.                             if plus ~= 0 then
  79.                                 plus = plus - 1
  80.                             else
  81.                                 break
  82.                             end
  83.                         end
  84.                     end
  85.                 end;
  86.                 [1] = function()
  87.                     minus = 16
  88.                     while true do
  89.                         if templisting[Sel1 + minus] ~= nil then
  90.                             Sel1 = Sel1 - minus
  91.                             break
  92.                         else
  93.                             if plus ~= 0 then
  94.                                 minus = minus - 1
  95.                             else
  96.                                 break
  97.                             end
  98.                         end
  99.                     end
  100.                 end;
  101.             };
  102.         }
  103.         actions = {
  104.             ["new"] = function(x)
  105.                 TypeSel = true
  106.                 while TypeSel do
  107.                     types = {"folder","file"}
  108.                     term.clear()
  109.                     if x ~= nil then
  110.                         term.setTextColor(colors.red)
  111.                         term.setCursorPos(1,1)
  112.                         write(x)
  113.                     end
  114.                     term.setCursorPos(1,2)
  115.                     term.setTextColor(colors.yellow)
  116.                     write("New: ")
  117.                     for i = 1,#types do
  118.                         term.setTextColor(colors.lightGray)
  119.                         if Sel3 == i then
  120.                             term.setTextColor(colors.yellow)
  121.                         end
  122.                         write(types[i])
  123.                         write(" ")
  124.                     end
  125.                     _event,p1 = os.pullEvent()
  126.                     if _event == "key" then
  127.                         if p1 == 205 then
  128.                             if Sel3 <= #types-1 then
  129.                                 Sel3 = Sel3 + 1
  130.                             end
  131.                         elseif p1 == 203 then
  132.                             if Sel3 ~= 1 then
  133.                                 Sel3 = Sel3 - 1
  134.                             end
  135.                         elseif p1 == 28 then
  136.                             TypeSel = false
  137.                         end
  138.                     end
  139.                 end
  140.                 term.setCursorPos(1,4)
  141.                 term.setTextColor(colors.yellow)
  142.                 write("Name: ")
  143.                 FileName = read()
  144.                 if fs.exists(path.."/"..FileName) then
  145.                     actions.new("File already exists")
  146.                 end
  147.                 if types[Sel3] == "folder" then
  148.                     fs.makeDir(path.."/"..FileName)
  149.                 else
  150.                     File = fs.open(path.."/"..FileName,"w")
  151.                     File.close()
  152.                 end
  153.                 Sel2 = 1
  154.             end;
  155.             ["open"] = function()
  156.                 if Ftype == "..." then
  157.                     pathmin = fs.getName(path)
  158.                     if pathmin == path then
  159.                         path = ""
  160.                     else
  161.                         pos = string.find(path,pathmin)
  162.                         path = string.sub(path,1,pos-2)
  163.                     end
  164.                     Sel1 = 1
  165.                 elseif Ftype == "folder" then
  166.                     path = path.."/"..templisting[Sel1]
  167.                     Sel1 = 1
  168.                 elseif Ftype == "file" then
  169.                     term.setBackgroundColor(colors.black)
  170.                     term.clear()
  171.                     term.setCursorPos(1,1)
  172.                     shell.run(path.."/"..templisting[Sel1])
  173.                     term.setTextColor(colors.red)
  174.                     print()
  175.                     print("FileViewer: program has finished running press any key to continue")
  176.                     _event = os.pullEvent()
  177.                 end
  178.             end;
  179.             ["edit"] = function()
  180.                 local i = path.."/"..templisting[Sel1]
  181.                 shell.run("edit",i)
  182.                 Sel2 = 1
  183.             end;
  184.             ["delete"] = function()
  185.                 term.setCursorPos(1,Sy)
  186.                 term.setBackgroundColor(colors.gray)
  187.                 term.clearLine()
  188.                 term.setTextColor(colors.yellow)
  189.                 items = {"Yes","No"}
  190.                 delsel = 1
  191.                 write("you sure?: ")
  192.                 while true do
  193.                     term.setCursorPos(1,Sy)
  194.                     term.setBackgroundColor(colors.gray)
  195.                     term.clearLine()
  196.                     term.setTextColor(colors.yellow)
  197.                     write("you sure?: ")
  198.                     for i = 1,#items do
  199.                         if i == delsel then
  200.                             term.setTextColor(colors.yellow)
  201.                         else
  202.                             term.setTextColor(colors.lightGray)
  203.                         end
  204.                         write(items[i].." ")
  205.                     end
  206.                     _event,p1 = os.pullEvent()
  207.                     if _event == "key" and p1 == 205 then
  208.                         delsel = 2
  209.                     elseif _event == "key" and p1 == 203 then
  210.                         delsel = 1
  211.                     elseif _event == "key" and p1 == 28 then
  212.                         if delsel == 1 then
  213.                             fs.delete(path.."/"..templisting[Sel1])
  214.                             Sel1 = Sel1 - 1
  215.                         else
  216.                            
  217.                         end
  218.                         Sel2 = 1
  219.                         break  
  220.                     end
  221.                 end
  222.             end;
  223.             ["package"] = function()
  224.                 term.setCursorPos(1,Sy)
  225.                 term.setBackgroundColor(colors.gray)
  226.                 term.clearLine()
  227.                 term.setTextColor(colors.yellow)
  228.                 write("Folder Name:")
  229.                 Fname = read()
  230.                 if fs.makeDir(path.."/"..Fname) then
  231.                     fs.makeDir(path.."/"..Fname)
  232.                 end
  233.                 for name in pairs(Seled.sel) do
  234.                     if Seled.sel[name] == true then
  235.                         fs.move(path.."/"..name,path.."/"..Fname.."/"..name)
  236.                         Seled.sel[name] = false
  237.                         Seled.num = Seled.num - 1
  238.                     end
  239.                 end
  240.                 Sel1 = 1
  241.             end;
  242.             ["copy"] = function()
  243.                 _copy = path.."/"..templisting[Sel1]
  244.                 _copyt = 0
  245.             end;
  246.             ["paste"] = function()
  247.                 if _copy ~= nil then
  248.                     if fs.exists(path.."/"..fs.getName(_copy)) == false then
  249.                         fs.copy(_copy,path.."/"..fs.getName(_copy))
  250.                     else
  251.                         rn = math.random(1,64)
  252.                         if fs.exists(path.."/"..fs.getName(_copy)..rn) == false then
  253.                             fs.copy(_copy,path.."/"..fs.getName(_copy)..rn)
  254.                         end
  255.                     end
  256.                 end
  257.             end;
  258.             ["delete_all"] = function()
  259.                 for name in pairs(Seled.sel) do
  260.                     if Seled.sel[name] == true then
  261.                         fs.delete(path.."/"..name)
  262.                         Seled.sel[name] = false
  263.                         Seled.num = Seled.num - 1
  264.                     end
  265.                 end
  266.                 Sel1 = 1
  267.             end;
  268.             ["rename"] = function()
  269.                 term.setCursorPos(1,Sy)
  270.                 term.setBackgroundColor(colors.gray)
  271.                 term.clearLine()
  272.                 term.setTextColor(colors.yellow)
  273.                 write("New Name:")
  274.                 Fname = read()
  275.                 if #Fname ~= 0 then
  276.                     shell.run("rename",path.."/"..templisting[Sel1],path.."/"..Fname)
  277.                 end
  278.             end;
  279.             ["seled"] = {
  280.                 "package";
  281.                 "delete_all";
  282.             };
  283.             ["..."] = {
  284.                 "open";
  285.                 "new";
  286.                 "paste";
  287.             };
  288.             ["folder"] = {
  289.                 "open";
  290.                 "new";
  291.                 "rename";
  292.                 "copy";
  293.                 "paste";
  294.                 "delete";
  295.             };
  296.             ["file"] = {
  297.                 "open";
  298.                 "new";
  299.                 "edit";
  300.                 "rename";
  301.                 "copy";
  302.                 "paste";
  303.                 "delete";
  304.             };
  305.         }
  306.         local blacklist = {
  307.             password = "1438"; --Change password to what ever you want
  308.             list = {
  309.                 --[[
  310.                     adding template:
  311.                     ["/sample/test/whatever"] = true;
  312.                 ]]
  313.                 ["/"..shell.getRunningProgram()] = true;
  314.             };
  315.         };
  316.         if path == nil then
  317.             if user == nil then
  318.                 if Sys == nil then
  319.                     path = ""
  320.                 else
  321.                     path = Sys.Default
  322.                 end
  323.             else
  324.                 path = user.default
  325.             end
  326.         end
  327.         Sel1,Sel2,Sel3 = 1,1,1
  328.         Seled = {
  329.             num = 0;
  330.             sel = {};
  331.         }
  332.         selx,sely = 2,3
  333.         _copy = nil
  334.         _copyt = 0
  335.         ON = true
  336.         while ON do
  337.             Sx,Sy = term.getSize()
  338.             if blacklist.list[fs.getName(path)] == true and pass ~= blacklist.password then
  339.                 pathmin = fs.getName(path)
  340.                 pos = string.find(path,pathmin)
  341.                 path = string.sub(path,1,pos-2)
  342.             end
  343.             listing = fs.list(path)
  344.             if path ~= "" then
  345.                 templisting = {"..."}
  346.             else
  347.                 templisting = {}
  348.             end
  349.             fx,fy = 1,1
  350.             term.setBackgroundColor(colors.gray)
  351.             term.clear()
  352.             term.setCursorPos(fx,fy)
  353.             term.setTextColor(colors.yellow)
  354.             write("path:"..path)
  355.             term.setCursorPos(1,2)
  356.             for i = 1,Sx do
  357.                 write("-")
  358.             end
  359.             fx,fy = 2,3
  360.             term.setCursorPos(fx,fy)
  361.             --Find Folders and Files
  362.             for i = 1,#listing do
  363.                 if fs.isDir(path.."/"..listing[i]) then
  364.                     table.insert(templisting,listing[i])
  365.                 end
  366.             end
  367.             for i = 1,#listing do
  368.                 if fs.isDir(path.."/"..listing[i]) == false then
  369.                     if path.."/"..listing[i] == "/"..shell.getRunningProgram() then
  370.                        
  371.                     else
  372.                         table.insert(templisting,listing[i])
  373.                     end
  374.                 end
  375.             end
  376.             -- Draw listing
  377.             if #templisting ~= 0 then
  378.                 if fs.isDir(path.."/"..templisting[Sel1]) then
  379.                     Ftype = "folder"
  380.                 else
  381.                     Ftype = "file"
  382.                 end
  383.                 if templisting[Sel1] == "..." then
  384.                     Ftype = "..."
  385.                 end
  386.                 if Seled.num > 0 then
  387.                     Ftype = "seled"
  388.                 end
  389.                 -- Draw Actions
  390.                 term.setCursorPos(1,Sy)
  391.                 for i = 1,#actions[Ftype] do
  392.                     if i == Sel2 then
  393.                         write("["..actions[Ftype][i].."]")
  394.                     else
  395.                         write(" "..actions[Ftype][i].." ")
  396.                     end
  397.                 end
  398.                 for i = 1,#templisting do
  399.                     if i <= 63 then
  400.                         term.setCursorPos(fx,fy)
  401.                         term.setBackgroundColor(colors.gray)
  402.                         if Seled.sel[templisting[i]] == true then
  403.                             term.setBackgroundColor(colors.red)
  404.                             term.setTextColor(colors.yellow)
  405.                             term.setCursorPos(fx-1,fy)
  406.                             write("^           ")
  407.                         end
  408.                         if blacklist.list[path.."/"..templisting[i]] == true then
  409.                             term.setTextColor(colors.red)
  410.                             term.setCursorPos(fx-1,fy)
  411.                             write("*")
  412.                         end
  413.                         if Sel1 == i then
  414.                             term.setTextColor(colors.yellow)
  415.                             term.setCursorPos(fx-1,fy)
  416.                             write(">")
  417.                             selx,sely = term.getCursorPos()
  418.                         end
  419.                         term.setCursorPos(fx,fy)
  420.                         term.setTextColor(colors.white)
  421.                         if fs.isDir(path.."/"..templisting[i]) then
  422.                             term.setTextColor(colors.lime)
  423.                         end
  424.                         if Sel1 == i then
  425.                             write(templisting[i])
  426.                             if #templisting[i] > 10 then
  427.                                 term.setTextColor(colors.red)
  428.                                 write("|")
  429.                             end
  430.                         else
  431.                             if #templisting[i] < 10 then
  432.                                 write(templisting[i])
  433.                             else
  434.                                 write(string.sub(templisting[i],1,8).."..")
  435.                             end
  436.                         end
  437.                         fy = fy + 1
  438.                         if fy == Sy then
  439.                             fx,fy = fx+12,3
  440.                         end
  441.                     else
  442.                        
  443.                     end
  444.                 end
  445.                 term.setCursorPos(selx,sely)
  446.                 term.setTextColor(colors.white)
  447.                 if fs.isDir(path.."/"..templisting[Sel1]) then
  448.                     term.setTextColor(colors.lime)
  449.                 end
  450.                 write(templisting[Sel1])
  451.             else
  452.                 term.setCursorPos(fx,fy)
  453.                 write("Its so lonely in here :(")
  454.             end
  455.             _event,p1 = os.pullEvent()
  456.             if events[_event] ~= nil then
  457.                 if events[_event][p1] ~= nil then
  458.                     if blacklist.list[path.."/"..templisting[Sel1]] == true and p1 == 28 and actions[Ftype][Sel2] ~= "new" then
  459.                         term.setCursorPos(1,Sy)
  460.                         term.clearLine()
  461.                         term.setTextColor(colors.red)
  462.                         write("password:")
  463.                         term.setTextColor(colors.yellow)
  464.                         pass = read("*")
  465.                         if pass == blacklist.password then
  466.                             events[_event][p1]()
  467.                         else
  468.                            
  469.                         end
  470.                     else
  471.                         events[_event][p1]()
  472.                     end
  473.                 end
  474.             end
  475.             sleep(.1)
  476.         end
  477. end
  478. local targs = {...}
  479. FileViewer(targs[1])
Advertisement
Add Comment
Please, Sign In to add comment