Advertisement
HPWebcamAble

[CC][1.2.2] File Manager

Jun 8th, 2014
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.45 KB | None | 0 0
  1. --[[
  2. HPWebcamAble Presents...
  3. File Manager
  4.  
  5. === Description ====
  6. This program adds a user interface for file interaction
  7.  
  8.  
  9. ==== Documentation ====
  10. Youtube Video:
  11. https://www.youtube.com/watch?v=pdaWStx-rwA
  12.  
  13.  
  14. ==== Installation and Use ====
  15. Pastebin Code: uz2f7Xbe
  16.  
  17. Only works on Advanced Computers (Not pocket, sorry)
  18.  
  19. pastebin get uz2f7Xbe fm
  20.  
  21. Then run 'fm' (Or what you called it)
  22.  
  23.  
  24. ==== Update History ====
  25. The pastebin will always have the most recent version
  26.  
  27. |1.2.2| <-- This program
  28.   -Updated desciption/version info/other stuff
  29.  
  30. |1.2.1|
  31.   -Fixed a bug that cause folders and files to be the same color
  32.  
  33. |1.2|
  34.   -Tweaked rename and move functions
  35.   -CraftOS 1.6 MAY(?) work...I didn't do anything :/ but havent seen any bugs
  36.   -A few other minor changes
  37.     *HDD is now C:
  38.     *I PLAN to make disk drives other letters
  39.    
  40. |1.1|
  41.   -Centered buttons
  42.   -Added a separate rename function
  43.   -Version and name added to the default path
  44.   -root is now HDD
  45.   -Minor bug fixes
  46.  
  47. |1.0|
  48.   -Release
  49. ]]
  50.  
  51. --Variables--
  52. local version = "1.2"
  53. w,h = term.getSize()
  54. local dir = ""
  55. shell.setDir(dir)
  56. local tProgram = shell.getRunningProgram()
  57. local buttonAPI = "button"
  58. local selected = 0
  59. local menuS = false
  60. local input
  61. local path
  62. local ypos
  63. local tempvar
  64.  
  65. filePos = {
  66.   [1] = {3,3},
  67.   [2] = {15,3},
  68.   [3] = {27,3},
  69.   [4] = {39,3},
  70.   [5] = {3,7},
  71.   [6] = {15,7},
  72.   [7] = {27,7},
  73.   [8] = {39,7},
  74.   [9] = {3,11},
  75.   [10] = {15,11},
  76.   [11]= {27,11},
  77.   [12]= {39,11},
  78.   [13] = {3,15},
  79.   [14]= {15,15},
  80.   [15]= {27,15},
  81.   [16]= {39,15}
  82. }
  83. local cPage = 1
  84. local nPage = 1
  85. local run = true
  86.  
  87. --Checks
  88. if not term.isColor() then
  89.   print("This program can only run on an advanced computer")
  90.   return
  91. --[[elseif os.version() ~= "CraftOS 1.5" then
  92.   print("This program is designed for CraftOS 1.5")
  93.   print("Your version is "..os.version())
  94.   print("Newer or older version may not work")
  95.   print("Run anyways?")
  96.   print("y/n")
  97.   while true do
  98.     event,par1,par2,par3 = os.pullEvent()
  99.     if par1 == keys.y then run = true break end
  100.     if par1 == keys.n then run = false break end
  101.   end]]
  102. end
  103.  
  104. if not run then os.pullEvent() return end
  105.  
  106.  
  107. --Functions--
  108. function clear(color)
  109.   if color then
  110.     term.setBackgroundColor(color)
  111.   end
  112.   term.clear()
  113.   term.setCursorPos(1,1)
  114. end
  115.  
  116. function sColor(color)
  117.   term.setBackgroundColor(color)
  118. end
  119.  
  120. function drawS(x,y,state)
  121.   if state then sColor(colors.cyan) else sColor(colors.white) end
  122.   term.setCursorPos(x-1,y-1)
  123.   print(string.rep(" ",13))
  124.   term.setCursorPos(x-1,y)
  125.   term.write(" ")
  126.   term.setCursorPos(x-1,y+1)
  127.   term.write(" ")
  128.   term.setCursorPos(x-1,y+2)
  129.   term.write(" ")
  130.   term.setCursorPos(x-1,y+3)
  131.   print(string.rep(" ",13))
  132.   term.setCursorPos(x+11,y)
  133.   term.write(" ")
  134.   term.setCursorPos(x+11,y+1)
  135.   term.write(" ")
  136.   term.setCursorPos(x+11,y+2)
  137.   term.write(" ")
  138.   term.setCursorPos(x+11,y+3)
  139.   term.write(" ")
  140. end
  141.  
  142. function printC(text,y)
  143.   if not y then
  144.     error("printC:No Y value specified")
  145.   end
  146.   term.setCursorPos(math.ceil(w/2-#tostring(text)/2),y)
  147.   term.write(text)
  148. end
  149.  
  150. function window(_w,_h,head,ypos)
  151.   if not _w or not _h then
  152.     error("Window:Missing width (w) or height (h)")
  153.   end
  154.   if not ypos or type(ypos) ~= "number" then ypos = 5 end
  155.   sColor(colors.white)
  156.   term.setCursorPos(1,h)
  157.   term.write(string.rep(" ",w))
  158.   sColor(colors.blue)
  159.   term.setCursorPos(w,1)
  160.   term.write(" ")
  161.   printC(string.rep(" ",_w),ypos)
  162.   term.setTextColor(colors.white)
  163.   if head then
  164.     printC(head,ypos)
  165.   end
  166.   sColor(colors.gray)
  167.   for i = 1, _h do
  168.     printC(string.rep(" ",_w),i+ypos)
  169.   end
  170. end
  171.  
  172. function bar()
  173.   sColor(colors.blue)
  174.   term.setCursorPos(1,1)
  175.   term.write(string.rep(" ",w-1))
  176.   sColor(colors.red)
  177.   term.setTextColor(colors.white)
  178.   term.write("X")
  179. end
  180.  
  181. function testSpot(x,y)
  182.   if y > 1 then
  183.     if #cDir < 17 then
  184.       _rep = #cDir
  185.     else
  186.       _rep = 16
  187.     end
  188.     for _c = 1, _rep do
  189.       if x >= filePos[_c][1] and x <= filePos[_c][1]+10 and y >= filePos[_c][2] and y <= filePos[_c][2]+2 then
  190.         return _c
  191.       end
  192.     end
  193.   end
  194.   return false
  195. end
  196.  
  197. function drawDir()
  198.   clear(colors.white)
  199.   bar()
  200.   sColor(colors.blue)
  201.   cDir = fs.list(shell.dir())
  202.   if shell.dir() == "" then
  203.     printC("C: - File Manager "..version,1)
  204.   else
  205.     printC("/"..shell.dir(),1)
  206.   end
  207.   for i = 1, #cDir do
  208.     local num = i
  209.     local a = i+16*(cPage-1)
  210.     local name = tostring(cDir[a])
  211.     if name == "nil" then break end
  212.     if #name > 9 then
  213.       text = string.sub(name,1,6).."..."
  214.     else
  215.       text = name
  216.     end
  217.     if fs.isDir(shell.dir().."/"..name) then
  218.       sColor(colors.yellow)
  219.     else
  220.       sColor(colors.lightGray)
  221.     end
  222.     if shell.dir() == "" then
  223.       location = "C:/"..name
  224.     else
  225.       location = tostring("C:/"..shell.dir().."/"..name)
  226.     end
  227.     if i > 16 then break end
  228.     x = filePos[i][1]
  229.     y = filePos[i][2]
  230.     term.setCursorPos(x,y)
  231.     term.write(string.rep(" ",11))
  232.     term.setCursorPos(x,y+1)
  233.     term.write(string.rep(" ",math.floor(5.5-#tostring(text)/2)))
  234.     term.write(text)
  235.     term.write(string.rep(" ",math.ceil(5.5-#tostring(text)/2)))
  236.     term.setCursorPos(x,y+2)
  237.     term.write(string.rep(" ",11))
  238.   end
  239.   if #cDir > 16 then
  240.     nPage = math.ceil(#cDir/16)
  241.     term.setTextColor(colors.black)
  242.     sColor(colors.white)
  243.     printC("Page "..cPage.." of "..nPage,2)
  244.     nSelect = cDir[selected+16*(cPage-1)]
  245.   else
  246.     nSelect = cDir[selected]
  247.   end
  248.   if selected > 0 then
  249.     drawS(filePos[selected][1],filePos[selected][2],true)
  250.     sType = fs.isDir(shell.resolve(nSelect))
  251.     menuS = true
  252.   else
  253.     menuS = false
  254.   end
  255. end
  256.  
  257. function confirm(text)
  258.   if not text or #text <= 8 then
  259.     window(8,3,text)
  260.   else
  261.     window(#text+2,3,text)
  262.   end    
  263.   term.setCursorPos(23,7)
  264.   sColor(colors.lime)
  265.   term.write("Yes")
  266.   term.setCursorPos(27,7)
  267.   sColor(colors.red)
  268.   term.write("No")
  269.   while true do
  270.     event,par1,par2,par3 = os.pullEvent("mouse_click")
  271.     if par3 == 7 then
  272.       if par2 >= 23 and par2 <=25 then
  273.         return true
  274.       elseif par2 >= 27 and par2 <= 28 then
  275.         return false
  276.       end
  277.     end
  278.   end
  279. end
  280.  
  281. function changeDir()
  282.   window(26,4,"Change Directory")
  283.   printC("Use '/' for root",8)
  284.   sColor(colors.black)
  285.   repeat    
  286.     printC(string.rep(" ",18),7)  
  287.     term.setCursorPos(17,7)
  288.     input = read()
  289.   until input ~= ""
  290.   path = shell.resolve(input)
  291.   if fs.exists(path) and fs.isDir(path) then
  292.     shell.setDir(path)
  293.   else
  294.     sColor(colors.gray)
  295.     term.setTextColor(colors.red)
  296.     printC("Doesn't Exist!",9)
  297.     sleep(1)
  298.   end
  299. end
  300.  
  301. function info(_path)
  302.   if not _path then
  303.     error("No path")
  304.   end
  305.   name = cDir[_path]
  306.   clear(colors.white)
  307.   term.setTextColor(colors.white)
  308.   sColor(colors.blue)
  309.   term.write(string.rep(" ",w))
  310.   printC(name,1)
  311.   term.setCursorPos(w,1)
  312.   sColor(colors.red)
  313.   print("X")
  314.   term.setCursorPos(1,2)
  315.   term.setTextColor(colors.black)
  316.   sColor(colors.white)
  317.   print(" ")
  318.   term.write("File Path:")
  319.   path = shell.resolve(name)
  320.   print("/"..path)
  321.   term.write("Type:")
  322.   if fs.isDir(path) then
  323.     print("Folder")
  324.   else
  325.     print("File")
  326.     print("Size:"..fs.getSize(path))
  327.   end
  328.   if fs.isReadOnly(shell.resolve(nSelect)) then
  329.     print("This is a read-only item")
  330.   end
  331.   if shell.resolve(nSelect) == tProgram then
  332.     term.setTextColor(colors.yellow)
  333.     print("This program is currently running")
  334.   end
  335.   while true do
  336.     event,par1,par2,par3 = os.pullEvent()
  337.     if event == "mouse_click" then
  338.       if par2 == w and par3 == 1 then
  339.         break
  340.       end
  341.     end
  342.   end
  343. end
  344.  
  345. function addItem()
  346.   window(8,7,"Add...")
  347.   term.setTextColor(colors.white)
  348.   sColor(colors.lime)
  349.   printC("Folder",7)
  350.   printC("File",9)
  351.   sColor(colors.red)
  352.   printC("Cancel",11)
  353.   local choice = nil
  354.   while true do
  355.     event,par1,par2,par3 = os.pullEvent("mouse_click")
  356.     if par3 == 7 and par2 >= 23 and par2 <= 28 then
  357.       choice = 1
  358.       break
  359.     elseif par3 == 9 and par2 >= 24 and par2 <= 27 then
  360.       choice = 2
  361.       break
  362.     elseif par3 == 11 and par2 >= 23 and par2 <=28 then
  363.       choice = 3
  364.       break
  365.     end
  366.   end
  367.   if choice ~= 3 then
  368.     window(26,3,"Enter name")
  369.     sColor(colors.black)
  370.     repeat
  371.       printC(string.rep(" ",22),7)
  372.       term.setCursorPos(15,7)
  373.       input = read()
  374.     until input ~= ""
  375.     if fs.exists(shell.resolve(input)) then
  376.       sColor(colors.gray)
  377.       term.setTextColor(colors.red)
  378.       printC("Already Exists!",8)
  379.       sleep(2)
  380.     elseif choice == 1 then
  381.       fs.makeDir(shell.dir().."/"..input)
  382.     elseif choice == 2 then
  383.       f = fs.open(shell.dir().."/"..input,"w")
  384.       f.close()
  385.     end
  386.   end
  387. end
  388.  
  389. function move(item)
  390.   path = shell.resolve(item)
  391.   if #path <= 26 then
  392.     window(26,5,"Move item")
  393.   else
  394.     window(#path+2,5,"Move item")
  395.   end
  396.   printC("Current path:",6)
  397.   printC(path,7)
  398.   printC("New path:",8)
  399.   printC("Use 'cancel' to cancel",10)
  400.   sColor(colors.black)
  401.   repeat
  402.     printC(string.rep(" ",22),9)
  403.     term.setCursorPos(15,9)
  404.     input = read()
  405.   until input ~= ""
  406.   sColor(colors.gray)
  407.   printC(string.rep(" ",26),10)
  408.   term.setTextColor(colors.red)
  409.   if fs.exists(input) then
  410.     printC("Already Exists!",10)
  411.     sleep(2)
  412.   elseif input == "cancel" then
  413.     printC("Canceled",10)
  414.     sleep(2)
  415.   else
  416.     fs.move(path,input)
  417.     term.setTextColor(colors.lime)
  418.     printC("Success",10)
  419.     sleep(2)
  420.   end    
  421. end
  422.  
  423. function cRename(item)
  424.   path = shell.resolve(item)
  425.   if #path <= 26 then
  426.     window(26,5,"Rename item")
  427.   else
  428.     window(#path+2,5,"Rename item")
  429.   end
  430.   printC("Current name:",6)
  431.   printC(item,7)
  432.   printC("New name:",8)
  433.   printC("Use 'cancel' to cancel",10)
  434.   sColor(colors.black)
  435.   repeat
  436.     printC(string.rep(" ",22),9)
  437.     term.setCursorPos(15,9)
  438.     input = read()
  439.   until input ~= ""
  440.   sColor(colors.gray)
  441.   printC(string.rep(" ",26),10)
  442.   term.setTextColor(colors.red)
  443.   if fs.exists(shell.resolve(input)) then
  444.     printC("Already Exists!",10)
  445.     sleep(2)
  446.   elseif string.find(input,"/") then
  447.     printC("Cannot contain '/'",10)
  448.     sleep(2)
  449.   elseif input == "cancel" then
  450.     printC("Canceled",10)
  451.     sleep(2)
  452.   else
  453.     fs.move(path,input)
  454.     term.setTextColor(colors.lime)
  455.     printC("Success",10)
  456.     sleep(2)
  457.   end
  458. end
  459.  
  460. --Main loop program--
  461. function main()
  462.   while true do
  463.     drawDir()
  464.     if menuS then
  465.       term.setTextColor(colors.black)
  466.       sColor(colors.white)
  467.       printC(nSelect,h-1)
  468.       if sType then  --If the item is a folder
  469.         if fs.isReadOnly(shell.resolve(nSelect)) then  --If the folder is read-only
  470.           printC("This is a read-only folder  O:Open Tab:Info",h)
  471.         else  --Normal folder
  472.           printC("O:Open  D:Delete  M:Move  N:Rename  Tab:Info",h)
  473.         end
  474.       else  --If the item is a file
  475.         if shell.resolve(nSelect) == tProgram then  --If the file is this program
  476.           printC("This program is currently running  Tab:Info",h)
  477.         elseif fs.isReadOnly(shell.resolve(nSelect)) then  --If the file is read-only
  478.           printC("This is a read-only file  R:Run P:Preview Tab:Info",h)
  479.         else  --Normal file
  480.           printC("R:Run E:Edit D:Delete M:Move N:Rename Tab:Info",h)
  481.         end
  482.       end
  483.       term.setTextColor(colors.white)
  484.     else
  485.       term.setTextColor(colors.black)
  486.       sColor(colors.white)
  487.       if fs.isReadOnly(shell.dir()) then --If folder is read--only
  488.         if #cDir > 16 then
  489.           printC("B:Back  Tab:Enter Path  Page:PgUp/Down",h)
  490.         else
  491.           printC("B:Back  Tab:Enter Path",h)
  492.           cPage = 1
  493.         end
  494.       else  --Normal Folder
  495.         if #cDir > 16 then
  496.           printC("C:Create... B:Back Tab:Enter path Page:PgUp/Down",h)
  497.         else
  498.           printC("C:Create...  B:Back  Tab:Enter path",h)
  499.           cPage = 1
  500.         end
  501.       end
  502.       term.setTextColor(colors.white)
  503.     end
  504.     event,par1,par2,par3,par4 = os.pullEvent()  --Main pullEvent--
  505.     if event == "mouse_click" then
  506.       if par2 == w and par3 == 1 then
  507.         break
  508.       else
  509.         click = testSpot(par2,par3)
  510.         if click then
  511.           selected = click
  512.         else
  513.           selected = 0
  514.         end
  515.       end
  516.     elseif event == "key" then
  517.       if selected > 0 then  --If an item is selected
  518.         if par1 == keys.tab then
  519.           info(selected)
  520.         end
  521.         if shell.resolve(nSelect) ~= tProgram then
  522.           if par1 == keys.d and not fs.isReadOnly(shell.resolve(nSelect)) then
  523.             if confirm(" Delete? ") then
  524.               fs.delete(shell.resolve(nSelect))
  525.               selected = 0
  526.             end          
  527.           elseif par1 == keys.m and not fs.isReadOnly(shell.resolve(nSelect)) then
  528.             os.pullEvent()
  529.             move(nSelect)
  530.             selected = 0
  531.           elseif par1 == keys.n and not fs.isReadOnly(shell.resolve(nSelect)) then
  532.             os.pullEvent()
  533.             cRename(nSelect)
  534.             selected = 0
  535.           end
  536.           if sType then --If the item is a directory
  537.             if par1 == keys.o then
  538.               shell.setDir(shell.resolve(nSelect))
  539.               selected = 0          
  540.             end
  541.           else  --If the item is a file
  542.             if par1 == keys.r then
  543.               clear(colors.black)
  544.               print("Begin program:")
  545.               state,err = pcall(function() shell.run(nSelect) end )
  546.               if err then
  547.                 clear(colors.white)
  548.                 bar()
  549.                 printC("Program Error Report",1)
  550.                 term.setCursorPos(1,3)
  551.                 term.setTextColor(colors.black)
  552.                 print("An error occured while running "..nSelect)
  553.                 print(err)
  554.                 while true do
  555.                   event,par1,par2,par3 = os.pullEvent("mouse_click")
  556.                   if par2 == w and par3 == 1 then
  557.                     break
  558.                   end
  559.                 end
  560.               else
  561.                 term.setTextColor(colors.white)
  562.                 print("End of program")
  563.                 print("Press any key...")
  564.                 term.setTextColor(colors.black)
  565.                 print("End of program")
  566.                 print("Press any key...")
  567.                 os.pullEvent("key")
  568.               end
  569.               selected = 0
  570.             elseif par1 == keys.e or par1 == keys.p then
  571.               os.pullEvent()
  572.               shell.run("edit "..nSelect)
  573.               selected = 0
  574.             end
  575.           end
  576.         end
  577.       else  --If nothing is selected
  578.         if par1 == keys.tab then
  579.           changeDir()
  580.         elseif par1 == keys.c and not fs.isReadOnly(shell.resolve(shell.dir())) then
  581.           addItem()
  582.         elseif par1 == keys.b then
  583.           shell.run("cd ..")
  584.         elseif par1 == 209 then
  585.           if cPage > 1 then cPage = cPage-1 end
  586.         elseif par1 == 201 then
  587.           if cPage < nPage then cPage = cPage+1 end
  588.         end
  589.       end
  590.     end
  591.   end
  592. end
  593.  
  594.  
  595.  
  596. --Program--
  597. state,err = pcall(function() main() end)
  598.  
  599. if err then
  600.   if not string.find("Terminated",err) then
  601.     term.setTextColor(colors.white)
  602.     sColor(colors.black)
  603.     print("CRITICAL ERROR:")
  604.     print(err)
  605.     print("Press any key to continue")
  606.     os.pullEvent("key")
  607.   end
  608. end
  609.  
  610. --after program ends
  611. clear(colors.black)
  612. term.setTextColor(colors.white)
  613. shell.setDir("")
  614. printC("Thank you for using File Manager",1)
  615. printC("Version "..version,2)
  616. print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement