SHARE
TWEET

FileViewer v1.0

Xelostar Jul 1st, 2016 (edited) 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.   This software is made by Xelostar.
  3.   Please give me credit when showing someone this program.
  4.  
  5.   Links:
  6.   -https://www.youtube.com/watch?v=8khhCTGfGzM
  7.   -http://www.computercraft.info/forums2/index.php?/topic/27066-fileviewer/
  8. ]]--
  9.  
  10. path = {}
  11. folderI = paintutils.loadImage("/FileViewer/folder.img")
  12. executeableI = paintutils.loadImage("/FileViewer/executeable.img")
  13. textI = paintutils.loadImage("/FileViewer/text.img")
  14. imageI = paintutils.loadImage("/FileViewer/image.img")
  15. blueprintI = paintutils.loadImage("/FileViewer/blueprint.img")
  16.  
  17. clipboard = ""
  18. deleteOld = false
  19.  
  20. screenWidth, screenHeight = term.getSize()
  21.  
  22. page = 0
  23.  
  24. files = {}
  25.  
  26. function getPath()
  27.   local pathT = "/"
  28.  
  29.   for k, v in pairs(path) do
  30.     if k == 1 then
  31.       pathT = pathT .. v
  32.     else
  33.       pathT = pathT .. "/" .. v
  34.     end
  35.   end
  36.  
  37.   return pathT
  38. end
  39.  
  40. function getName(text)
  41.   term.setBackgroundColor(colors.white)
  42.   paintutils.drawLine(3, 9, 47, 9)
  43.   paintutils.drawLine(3, 10, 47, 10)
  44.   paintutils.drawLine(3, 11, 47, 11)
  45.  
  46.   term.setTextColor(colors.black)
  47.   term.setCursorPos(4, 10)
  48.   write(text)
  49.   local name = read()
  50.  
  51.   return name
  52. end
  53.  
  54. function up()
  55.   local pathT = path
  56.   path = {}
  57.  
  58.   for k, file in pairs(pathT) do
  59.     if k ~= table.getn(pathT) then
  60.       table.insert(path, file)
  61.     end
  62.   end
  63. end
  64.  
  65. function getFile()
  66.   local stop = false
  67.   while not stop do
  68.     local event, button, X, Y = os.pullEventRaw("mouse_click")
  69.    
  70.     for k, file in pairs(files) do
  71.       if k >= (page * 10) and k <= (page * 10 + 10) then
  72.         local pathT = getPath()
  73.        
  74.         if pathT == "/" then
  75.           pathT = ""
  76.         end
  77.        
  78.         local n = k - page * 10
  79.         local add = 0
  80.         local number = n
  81.        
  82.         while true do
  83.           number = number - 5
  84.          
  85.           if number <= 0 then
  86.             break
  87.           end
  88.          
  89.           add = add + 1
  90.         end
  91.        
  92.         if X >= (-7 + (n * 9) - add * 45) and X <= (-7 + (n * 9) - add * 45 + 6) and Y >= (3 + add * 8) and Y <= (3 + add * 8 + 6) then
  93.           stop = true
  94.           return file
  95.         end
  96.       end
  97.     end
  98.   end
  99. end
  100.  
  101. function drawScreen()
  102.   term.setBackgroundColor(colors.lightBlue)
  103.   term.clear()
  104.  
  105.   term.setCursorPos(1, 1)
  106.   term.setBackgroundColor(colors.lightGray)
  107.   term.clearLine()
  108.  
  109.   term.setBackgroundColor(colors.white)
  110.   term.setTextColor(colors.black)
  111.   term.setCursorPos(2, 1)
  112.   print("Up")
  113.  
  114.   term.setTextColor(colors.orange)
  115.   term.setBackgroundColor(colors.yellow)
  116.   term.setCursorPos(5, 1)
  117.   print("+")
  118.  
  119.   term.setBackgroundColor(colors.white)
  120.   term.setTextColor(colors.black)
  121.   term.setCursorPos(6, 1)
  122.   print("+")
  123.  
  124.   term.setBackgroundColor(colors.blue)
  125.   term.setTextColor(colors.purple)
  126.   term.setCursorPos(7, 1)
  127.   print("P")
  128.  
  129.   term.setBackgroundColor(colors.white)
  130.   term.setTextColor(colors.black)
  131.   term.setCursorPos(9, 1)
  132.   print("CP")
  133.   term.setBackgroundColor(colors.red)
  134.   term.setTextColor(colors.white)
  135.   term.setCursorPos(11, 1)
  136.   print("C")
  137.  
  138.   term.setBackgroundColor(colors.red)
  139.   term.setTextColor(colors.white)
  140.   term.setCursorPos(13, 1)
  141.   print("U")
  142.  
  143.   term.setBackgroundColor(colors.white)
  144.   term.setTextColor(colors.black)
  145.   term.setCursorPos(15, 1)
  146.   print("Path: " .. getPath())
  147.  
  148.   term.setBackgroundColor(colors.red)
  149.   term.setTextColor(colors.white)
  150.   term.setCursorPos(screenWidth, 1)
  151.   print("X")
  152.  
  153.   local rows = math.floor((screenHeight - 1) / 8)
  154.   local columns = math.floor(screenWidth / 9)
  155.   local filesPerPage = rows * columns
  156.  
  157.   for fileNr, file in pairs(files) do
  158.     if fileNr >= (page * filesPerPage) and fileNr <= (page * filesPerPage + filesPerPage) then
  159.       local pathT = getPath()
  160.      
  161.       if pathT == "/" then
  162.         pathT = ""
  163.       end
  164.      
  165.       local n = fileNr - page * filesPerPage
  166.       local row = 0
  167.       local number = n
  168.      
  169.       while true do
  170.         number = number - columns
  171.        
  172.         if number <= 0 then
  173.           break
  174.         end
  175.        
  176.         row = row + 1
  177.       end
  178.      
  179.       local fType = "executeable"
  180.       if file:match"([^.]*).(.*)" ~= nil then
  181.         a, extension = file:match"([^.]*).(.*)"
  182.        
  183.         if extension == "txt" then
  184.           fType = "text"
  185.         elseif extension == "img" then
  186.           fType = "image"
  187.         elseif extension == "bpr" then
  188.           fType = "blueprint"
  189.         end
  190.       end
  191.  
  192.       if fs.isDir(pathT .. "/" .. file) then
  193.         paintutils.drawImage(folderI, -7 + (n * 9) - row * (columns * 9), 3 + row * 8)
  194.       else
  195.         if fType == "text" then
  196.           paintutils.drawImage(textI, -7 + (n * 9) - row * (columns * 9), 3 + row * 8)
  197.         elseif fType == "image" then
  198.           paintutils.drawImage(imageI, -7 + (n * 9) - row * (columns * 9), 3 + row * 8)
  199.         elseif fType == "blueprint" then
  200.           paintutils.drawImage(blueprintI, -7 + (n * 9) - row * (columns * 9), 3 + row * 8)
  201.         else
  202.           paintutils.drawImage(executeableI, -7 + (n * 9) - row * (columns * 9), 3 + row * 8)
  203.         end
  204.       end
  205.      
  206.       term.setBackgroundColor(colors.white)
  207.       term.setTextColor(colors.black)
  208.      
  209.       term.setCursorPos(-7 + (n * 9) - row * (columns * 9), 8 + row * 8)
  210.      
  211.       local number = 0
  212.       for character in file:gmatch"." do
  213.         number = number + 1
  214.         if number == 9 then
  215.           term.setCursorPos(-7 + (n * 9) - row * (columns * 9), 9 + row * 8)
  216.         end
  217.         if number == 15 then
  218.           write("..")
  219.           break
  220.         end
  221.         write(character)
  222.       end
  223.     end
  224.   end
  225.  
  226.   local xOfBar = screenWidth - 1
  227.   local height = screenHeight - 4
  228.   local start = 2
  229.   local pointer = 1
  230.  
  231.   while pointer <= height do
  232.     term.setBackgroundColor(colors.gray)
  233.     term.setTextColor(colors.white)
  234.     term.setCursorPos(xOfBar, start + pointer)
  235.     if pointer == 1 then
  236.       print("^")
  237.     elseif pointer == start + height - 2 then
  238.       print("V")
  239.     else
  240.       print("|")
  241.     end
  242.     pointer = pointer + 1
  243.   end
  244.  
  245.   term.setBackgroundColor(colors.white)
  246.   term.setTextColor(colors.black)
  247.   term.setCursorPos(xOfBar - 1, math.floor(screenHeight / 2) + 1)
  248.   print(page)
  249. end
  250.  
  251.  
  252. while true do
  253.   files = {}
  254.   for k, v in pairs(fs.list(getPath())) do
  255.     table.insert(files, v)
  256.   end
  257.   drawScreen()
  258.   local event, button, X, Y = os.pullEventRaw()
  259.   if event == "mouse_click" then
  260.     if button == 1 then
  261.       if X == screenWidth and Y == 1 then
  262.         break
  263.       elseif X >= 2 and X <= 3 and Y == 1 then
  264.         up()
  265.         page = 0
  266.       elseif X == screenWidth - 1 and Y == 3 then
  267.         if page ~= 0 then
  268.           page = page - 1
  269.         end
  270.       elseif X == screenWidth - 1 and Y == screenHeight - 2 then
  271.         page = page + 1
  272.       elseif X == 5 and Y == 1 then
  273.         local mapName = getName("Map name: ")
  274.         shell.run("mkdir", getPath() .. "/" .. mapName)
  275.       elseif X == 6 and Y == 1 then
  276.         local fileName = getName("File name: ")
  277.         shell.run("edit", getPath() .. "/" .. fileName .. ".txt")
  278.       elseif X == 7 and Y == 1 then
  279.         local imageName = getName("Image name: ")
  280.         shell.run("paint", getPath() .. "/" .. imageName .. ".img")
  281.       elseif X == 9 and Y == 1 then
  282.         clipboard = getPath() .. "/" .. getFile()
  283.         deleteOld = false
  284.       elseif X == 10 and Y == 1 then
  285.         if deleteOld == false then
  286.           shell.run("copy", clipboard, getPath())
  287.         else
  288.           shell.run("move", clipboard, getPath())
  289.         end
  290.       elseif X == 11 and Y == 1 then
  291.         clipboard = getPath() .. "/" .. getFile()
  292.         deleteOld = true
  293.       elseif X == 13 and Y == 1 then
  294.         term.setBackgroundColor(colors.white)
  295.         term.setTextColor(colors.black)
  296.         paintutils.drawLine(3, 9, 49, 9)
  297.         paintutils.drawLine(3, 10, 49, 10)
  298.         paintutils.drawLine(3, 11, 49, 11)
  299.         term.setCursorPos(13, 10)
  300.         print("Click on a file to delete it.")
  301.  
  302.         sleep(1)
  303.  
  304.         drawScreen()
  305.  
  306.         local file = getFile()
  307.  
  308.         term.setBackgroundColor(colors.white)
  309.         term.setTextColor(colors.black)
  310.         paintutils.drawLine(3, 9, 49, 9)
  311.         paintutils.drawLine(3, 10, 49, 10)
  312.         paintutils.drawLine(3, 11, 49, 11)
  313.         term.setCursorPos(4, 10)
  314.         print("Are you sure you want to delete " .. file .. "?")
  315.        
  316.         term.setBackgroundColor(colors.lime)
  317.         term.setCursorPos(42, 10)
  318.         print("Yes")
  319.        
  320.         term.setBackgroundColor(colors.red)
  321.         term.setCursorPos(46, 10)
  322.         print("No ")
  323.        
  324.         local choice = false
  325.        
  326.         while true do
  327.           local event, button, X, Y = os.pullEventRaw("mouse_click")
  328.           if button == 1 then
  329.             if Y == 10 then
  330.               if X >= 42 and X <= 44 then
  331.                 choice = true
  332.                 break
  333.               elseif X >= 46 and X <= 48 then
  334.                 break
  335.               end
  336.             end
  337.           end
  338.         end
  339.        
  340.         if choice == true then
  341.           shell.run("delete", getPath() .. "/" .. file)
  342.         end
  343.       else
  344.         local rows = math.floor((screenHeight - 1) / 8)
  345.         local columns = math.floor(screenWidth / 9)
  346.         local filesPerPage = rows * columns
  347.  
  348.         for k, file in pairs(files) do
  349.           if k >= (page * filesPerPage) and k <= (page * filesPerPage + filesPerPage) then
  350.             local pathT = getPath()
  351.            
  352.             if pathT == "/" then
  353.               pathT = ""
  354.             end
  355.            
  356.             local n = k - page * filesPerPage
  357.             local row = 0
  358.             local number = n
  359.            
  360.             while true do
  361.               number = number - columns
  362.              
  363.               if number <= 0 then
  364.                 break
  365.               end
  366.              
  367.               row = row + 1
  368.             end
  369.            
  370.             if X >= -7 + (n * 9) - row * (columns * 9) and X <= -7 + (n * 9) - row * (columns * 9) + 6 and Y >= 3 + row * 8 and Y <= 3 + row * 8 + 6 then
  371.               local fType = "executeable"
  372.              
  373.               if file:match"([^.]*).(.*)" then
  374.                 a, extension = file:match"([^.]*).(.*)"
  375.                
  376.                 if extension == "txt" then
  377.                   fType = "text"
  378.                 elseif extension == "img" then
  379.                   fType = "image"
  380.                 elseif extension == "blueprint" then
  381.                   fType = "blueprint"
  382.                 end
  383.               end
  384.              
  385.               if fs.isDir(pathT .. "/" .. file) then
  386.                 table.insert(path, file)
  387.                 page = 0
  388.               else
  389.                 if fType == "text" then
  390.                   shell.run("edit", getPath() .. "/" .. file)
  391.                 elseif fType == "image" then
  392.                   shell.run("paint", getPath() .. "/" .. file)
  393.                 elseif fType == "blueprint" then
  394.                   shell.run("edit", getPath() .. "/" .. file)
  395.                 else
  396.                   shell.run(getPath() .. "/" .. file)
  397.                 end
  398.               end
  399.             end
  400.           end
  401.         end
  402.       end
  403.     end
  404.   end
  405. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top