Advertisement
Thod

DreamOS/system/filemanager.ds

Mar 5th, 2021
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.45 KB | None | 0 0
  1. local root = "/DreamOS/"
  2. local curDir = ""
  3. local curDirs = {
  4. [1] = root
  5. }
  6. for k,v in pairs(curDirs) do
  7.     curDir = curDir..v
  8. end
  9. local selectedFile = ""
  10. local SFIsDir = false
  11. local SFPos = 0
  12. local sbSelected = 0
  13. local clipBoard = ""
  14. local clipBoardFileName = ""
  15. local clipBoardIsDir = false
  16. local slc = 0
  17. local brunning = true
  18. local scroll = 0
  19. local scrollEnabled = false
  20. local fileList = {}
  21. local notInFile = true
  22.  
  23. local w,h = term.getSize()
  24. h = h - 1
  25.  
  26. local function newFile()
  27.     local curError = "none"
  28.     term.setCursorPos(1,h+1)
  29.     term.setBackgroundColor(colors.green)
  30.     term.clearLine()
  31.     term.setTextColor(colors.black)
  32.     term.setCursorPos(1,h+1)
  33.     write(">")
  34.     term.setCursorPos(2,h+1)
  35.     local newFileInput = read()
  36.     if newFileInput == "" then
  37.         curError = "Must specify file name."
  38.         term.setCursorPos(1,h+1)
  39.         term.setBackgroundColor(colors.green)
  40.         term.clearLine()
  41.         term.setCursorPos(1,h+1)
  42.         term.setTextColor(colors.red)
  43.         print(curError)
  44.         sleep(3)
  45.     else
  46.         local newFileExists = false
  47.         for k,v in pairs(fileList) do
  48.             if v == newFileInput and fs.isDir(curDir..v) == false then
  49.                 newFileExists = true
  50.             end
  51.         end
  52.         if newFileExists then
  53.             curError = "File exists."
  54.             term.setCursorPos(1,h+1)
  55.             term.setBackgroundColor(colors.green)
  56.             term.clearLine()
  57.             term.setCursorPos(1,h+1)
  58.             term.setBackgroundColor(colors.green)
  59.             term.setTextColor(colors.red)
  60.             print(curError)
  61.             sleep(3)
  62.         else
  63.             local newFileOpen = fs.open(curDir..newFileInput,"w")
  64.             newFileOpen.write(" ")
  65.             newFileOpen.close()
  66.         end
  67.     end
  68. end
  69.  
  70. local function newFolder()
  71.     local curError = "none"
  72.     term.setCursorPos(1,h+1)
  73.     term.setBackgroundColor(colors.green)
  74.     term.clearLine()
  75.     term.setTextColor(colors.black)
  76.     term.setCursorPos(1,h+1)
  77.     write(">")
  78.     term.setCursorPos(2,h+1)
  79.     local newFileInput = read()
  80.     if newFileInput == "" then
  81.         curError = "Must specify folder name."
  82.         term.setCursorPos(1,h+1)
  83.         term.setBackgroundColor(colors.green)
  84.         term.clearLine()
  85.         term.setCursorPos(1,h+1)
  86.         term.setTextColor(colors.red)
  87.         print(curError)
  88.         sleep(3)
  89.     else
  90.         local newFileExists = false
  91.         for k,v in pairs(fileList) do
  92.             if v == newFileInput and fs.isDir(curDir..v) then
  93.                 newFileExists = true
  94.             end
  95.         end
  96.         if newFileExists then
  97.             curError = "Folder exists."
  98.             term.setCursorPos(1,h+1)
  99.             term.setBackgroundColor(colors.green)
  100.             term.clearLine()
  101.             term.setCursorPos(1,h+1)
  102.             term.setBackgroundColor(colors.green)
  103.             term.setTextColor(colors.red)
  104.             print(curError)
  105.             sleep(3)
  106.         else
  107.             shell.run("mkdir",curDir..newFileInput)
  108.         end
  109.     end
  110. end
  111.  
  112. local function editFile()
  113.     if SFIsDir == false then
  114.         shell.run("edit",curDir..selectedFile)
  115.     end
  116. end
  117.  
  118. local function renameFile()
  119.     term.setCursorPos(1,h+1)
  120.     term.setBackgroundColor(colors.green)
  121.     term.clearLine()
  122.     term.setCursorPos(1,h+1)
  123.     term.setTextColor(colors.black)
  124.     write(">")
  125.     write(curDir)
  126.     local renameInput = io.read()
  127.     shell.run("rename",curDir..selectedFile,curDir..renameInput)
  128. end
  129.  
  130. local function copyFile()
  131.     clipBoard = curDir..selectedFile
  132.     clipBoardFileName = selectedFile
  133.     if fs.isDir(clipBoard) then
  134.         clipBoardIsDir = true
  135.     end
  136.     term.setCursorPos(1,h+1)
  137.     term.setBackgroundColor(colors.green)
  138.     term.clearLine()
  139.     term.setCursorPos(1,h+1)
  140.     term.setTextColor(colors.black)
  141.     print("Copied")
  142.     sleep(2)
  143. end
  144.  
  145. local function paste()
  146.     shell.run("copy",clipBoard,curDir..clipBoardFileName)
  147. end
  148.  
  149. local function moveFile()
  150.     term.setCursorPos(1,10)
  151.     local moving = true
  152.     local selectedMoveFolder = ""
  153.     while moving do
  154.         sleep(0.1)
  155.         local i = 0
  156.         term.setBackgroundColor(colors.lightBlue)
  157.         while i <= h-1 do
  158.             i = i + 1
  159.             term.setCursorPos(w-10,i)
  160.             print("           ")
  161.         end
  162.         local mD = 0
  163.         term.setBackgroundColor(colors.white)
  164.         while mD <= h-2 do
  165.             mD = mD + 1
  166.             term.setCursorPos(2,mD+1)
  167.             local mD2 = 0
  168.             while mD2 <= (w-13) do
  169.                 mD2 = mD2 + 1
  170.                 write(" ")
  171.             end
  172.         end
  173.         local moveFiles = fs.list(curDir)
  174.         moveFilesList = {}
  175.         -- This makes it so dirs always show first
  176.         for k,v in pairs(moveFiles) do
  177.             if fs.isDir(curDir..v) then
  178.                 table.insert(moveFilesList, v)
  179.             end
  180.         end
  181.         for k,v in pairs(moveFilesList) do
  182.             if fs.isDir(curDir..v) then
  183.                 if k > scroll and k <= (h-3)+scroll then
  184.                     if selectedMoveFolder == v then
  185.                         term.setBackgroundColor(colors.blue)
  186.                         local d = 0
  187.                         while d <= w-14 do
  188.                             d = d + 1
  189.                             term.setCursorPos(2+d,(k+1)-scroll)
  190.                             write(" ")
  191.                         end
  192.                     else
  193.                         term.setBackgroundColor(colors.white)
  194.                     end
  195.                     term.setCursorPos(2,(k+1)-scroll)
  196.                     term.setTextColor(colors.gray)
  197.                     write("[")
  198.                     term.setTextColor(colors.orange)
  199.                     write("\\")
  200.                     term.setTextColor(colors.gray)
  201.                     write("]")
  202.                     term.setTextColor(colors.green)
  203.                     write(v)
  204.                 end
  205.             end
  206.         end
  207.         term.setCursorPos((w-7),5)
  208.         term.setTextColor(colors.black)
  209.         term.setBackgroundColor(colors.lightBlue)
  210.         write("Cancel")
  211.         e = {os.pullEvent()}
  212.         if e[1] == "mouse_click" and e[2] == 1 then
  213.             for k,v in pairs(moveFilesList) do
  214.                 if e[3]>=2 and e[3]<=#v+4 and e[4]==k+1 then
  215.                     if selectedMoveFolder == v then
  216.                         shell.run("move",curDir..selectedFile,curDir..v)
  217.                         moving = false
  218.                     else
  219.                         selectedMoveFolder = v
  220.                     end
  221.                 end
  222.             end
  223.             if e[3] >=(w-7) and e[3] <=(w-7)+6 and e[4] == 5 then
  224.                 moving = false
  225.                 return
  226.             end
  227.         end
  228.     end
  229. end
  230.  
  231. local sbMenu = {
  232.     [1] = {txt = "Edit", fileOnly = true, cmd = function() editFile() end},
  233.     [2] = {txt = "Rename", fileOnly = false, cmd = function() renameFile() end},
  234.     [3] = {txt = "Copy", fileOnly = false, cmd = function() copyFile() end},
  235.     [4] = {txt = "Move", fileOnly = false, cmd = function() moveFile() end},
  236.     [5] = {txt = "Delete", fileOnly = false, cmd = function() deleteFile() end},
  237.     [6] = {txt = "Unselect", fileOnly = false, cmd = function() sbSelected = 0; selectedFile = ""; SFIsDir = false slc = 0; end}
  238. }
  239.  
  240. local sbMenuNone = {
  241.     [1] = {txt = "New File", cmd = function() newFile() end},
  242.     [2] = {txt = "New Folder", cmd = function() newFolder() end},
  243.     [3] = {txt = "Paste", cmd = function() paste() end}
  244. }
  245.  
  246. local function drawFiles()
  247.     term.setBackgroundColor(colors.white)
  248.     term.setTextColor(colors.orange)
  249.     term.setCursorPos(2,2)
  250.     write(curDir)
  251.     term.setTextColor(colors.orange)
  252.     term.setCursorPos(2,3)
  253.     write("..")
  254.     local files = fs.list(curDir)
  255.     fileList = {}
  256.     -- This makes it so dirs always show first
  257.     for k,v in pairs(files) do
  258.         if fs.isDir(curDir..v) then
  259.             table.insert(fileList, v)
  260.         end
  261.     end
  262.     for k,v in pairs(files) do
  263.         if not fs.isDir(curDir..v) then
  264.             table.insert(fileList, v)
  265.         end
  266.     end
  267.     for k,v in pairs(fileList) do
  268.         if k > scroll and k <= (h-3)+scroll then
  269.             if selectedFile == v then
  270.                 term.setBackgroundColor(colors.blue)
  271.                 local d = 0
  272.                 while d <= w-14 do
  273.                     d = d + 1
  274.                     term.setCursorPos(2+d,(k+3)-scroll)
  275.                     write(" ")
  276.                 end
  277.             else
  278.                 term.setBackgroundColor(colors.white)
  279.             end
  280.             if fs.isDir(curDir..v) then
  281.                 term.setCursorPos(2,(k+3)-scroll)
  282.                 term.setTextColor(colors.gray)
  283.                 write("[")
  284.                 term.setTextColor(colors.orange)
  285.                 write("\\")
  286.                 term.setTextColor(colors.gray)
  287.                 write("]")
  288.                 term.setTextColor(colors.green)
  289.                 write(v)
  290.             else
  291.                 term.setCursorPos(2,(k+3)-scroll)
  292.                 term.setTextColor(colors.gray)
  293.                 write("[")
  294.                 term.setTextColor(colors.lime)
  295.                 write("=")
  296.                 term.setTextColor(colors.gray)
  297.                 write("]")
  298.                 term.setTextColor(colors.black)
  299.                 write(v)
  300.             end
  301.         end
  302.     end
  303.     if #fileList > h-1 then scrollEnabled = true; end  
  304. end
  305.  
  306. local function drawMain()
  307.     -- Have to put this here so it doesn't count slc being changed in one loop
  308.     if selectedFile == "" then slc = 0 else slc = 1 end
  309.     term.setBackgroundColor(colors.white)
  310.     term.clear()
  311.     term.setBackgroundColor(colors.cyan)
  312.     term.setCursorPos(1,1)
  313.     term.clearLine()
  314.     term.setTextColor(colors.white)
  315.     write("[Close]")
  316.     -- Side bar
  317.     term.setBackgroundColor(colors.lightBlue)
  318.     local i = 0
  319.     while i <= h-1 do
  320.         i = i + 1
  321.         term.setCursorPos(w-10,i)
  322.         print("           ")
  323.     end
  324.     term.setCursorPos(1, h+1)
  325.     term.setBackgroundColor(colors.green)
  326.     term.clearLine()
  327.     term.setTextColor(colors.black)
  328.     write("DreamOS File Manager")
  329.     term.setCursorPos(w-#selectedFile, h+1)
  330.     write(selectedFile)
  331.     if slc == 0 then
  332.         for k,v in pairs(sbMenuNone) do
  333.             if sbSelected == k then
  334.                 term.setBackgroundColor(colors.blue)
  335.             else
  336.                 term.setBackgroundColor(colors.lightBlue)
  337.             end
  338.             term.setCursorPos((w-5)-#v.txt/2,(k+1))
  339.             write(v.txt)
  340.         end
  341.     elseif slc == 1 then
  342.         -- Options
  343.         for k,v in pairs(sbMenu) do
  344.             if sbSelected == k then
  345.                 term.setBackgroundColor(colors.blue)
  346.             else
  347.                 term.setBackgroundColor(colors.lightBlue)
  348.             end
  349.             if v.fileOnly == true and SFIsDir then else
  350.                 term.setCursorPos((w-5)-#v.txt/2,(k+1))
  351.                 write(v.txt)
  352.             end
  353.         end
  354.     end
  355.     -- Scroll
  356.     term.setBackgroundColor(colors.gray)
  357.     term.setTextColor(colors.white)
  358.     i = 0
  359.     while i <= (h+1)-3 do
  360.         i = i + 1
  361.         term.setCursorPos(1,i+1)
  362.         if i == 1 then
  363.             write("^")
  364.         elseif i == (h+1)-2 then
  365.             write("v")
  366.         else
  367.             write(" ")
  368.         end
  369.     end
  370.     drawFiles()
  371. end
  372.  
  373. while brunning do
  374.     sleep(0.1)
  375.     drawMain()
  376.     if slc == 0 then
  377.         selectedFile = ""
  378.         SFIsDir = false
  379.     end
  380.     if notInFile then
  381.         e = {os.pullEvent()}
  382.         if e[1] == "mouse_click" and e[2] == 1 then
  383.             for k,v in pairs(fileList) do
  384.                 if e[3]>=2 and e[3]<=w-11 and e[4] == k+3-scroll then
  385.                     if selectedFile == v then else
  386.                         selectedFile = v
  387.                         if fs.isDir(curDir..v) then
  388.                             SFIsDir = true
  389.                         else
  390.                             SFIsDir = false
  391.                         end
  392.                         if slc == 1 then slc = 0 end
  393.                         sbSelected = 0
  394.                         SFPos = k
  395.                     end
  396.                 end
  397.             end
  398.             if slc == 1 then
  399.                 for k,v in pairs(sbMenu) do
  400.                     if e[3]>=((w-5)-#v.txt/2) and e[3]<=((w-5)+#v.txt/2) and e[4] == k+1 then
  401.                         if sbSelected == k then
  402.                             v.cmd()
  403.                         else
  404.                             sbSelected = k
  405.                         end
  406.                     end
  407.                 end
  408.                 -- Click on file/folder
  409.                 if e[3]>=2 and e[3]<=w-14 and e[4] == (SFPos+3)-scroll then
  410.                     if SFIsDir then
  411.                         table.insert(curDirs, selectedFile.."/")
  412.                         curDir = ""
  413.                         for k,v in pairs(curDirs) do
  414.                             curDir = curDir..v
  415.                         end
  416.                         selectedFile = ""
  417.                         SFIsDir = false
  418.                     else
  419.                         term.setCursorPos(1,h+1)
  420.                         shell.run(curDir..selectedFile)
  421.                     end
  422.                 end
  423.             elseif slc == 0 then
  424.                 for k,v in pairs(sbMenuNone) do
  425.                     if e[3]>=((w-5)-#v.txt/2) and e[3]<=((w-5)-#v.txt/2)+(#v.txt/2) and e[4] == k+1 then
  426.                         if sbSelected == k then
  427.                             v.cmd()
  428.                         else
  429.                             sbSelected = k
  430.                         end
  431.                     end
  432.                 end
  433.             end
  434.             if e[3]>=1 and e[3]<=7 and e[4] == 1 then
  435.                 term.clear()
  436.                 brunning = false
  437.             elseif e[3]>=2 and e[3]<=4 and e[4]==3 then
  438.                 table.remove(curDirs,#curDirs)
  439.                 local getNewDir = ""
  440.                 for k,v in pairs(curDirs) do
  441.                     getNewDir = getNewDir..v
  442.                 end
  443.                 if string.find(getNewDir,root) then --Make sure not escaping root
  444.                     curDir = getNewDir
  445.                 else
  446.                     table.insert(curDirs,curDir)
  447.                 end
  448.             end
  449.             if scrollEnabled then
  450.                 if e[3]==1 and e[4] == 2 then
  451.                     if scroll > 0 then
  452.                         scroll = scroll - 1
  453.                     end
  454.                 elseif e[3]==1 and e[4] == h then
  455.                     if scroll < #fileList then
  456.                         scroll = scroll + 1
  457.                     end
  458.                 end
  459.             end
  460.         elseif e[1] == "mouse_scroll" then
  461.             if e[2] == -1 then
  462.                 if scroll > 0 then
  463.                     scroll = scroll - 1
  464.                 end
  465.             elseif e[2] == 1 then
  466.                 if scroll < #fileList then
  467.                     scroll = scroll + 1
  468.                 end
  469.             end
  470.         end
  471.     end
  472. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement