Advertisement
FoxWorn3365

FMan2

Dec 4th, 2021 (edited)
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Width and Height of screen
  2. local w, h = term.getSize()
  3. --Show hidden files and directories
  4. local hidden = false
  5. --Current directory that is open
  6. local dir = "/Hack/files/"
  7. local diskDir = "/disk/TEMP/files"
  8. --Refers to the current selected file or directory
  9. local slc = 0
  10. --Indicates if program is running
  11. local running = true
  12. --If time is measured in 24 h time
  13. local mTime = true
  14. --Table with the files
  15. local files = {}
  16. --Scroll the files
  17. local scroll = 0
  18. --Name of file that is copied and if it should be moved
  19. local copy = {}
  20.  
  21. --Set background- and text-colour
  22. local function setColor(bc, tc)
  23.     if not bc then bc = 32768 end
  24.     if not tc then tc = 1 end
  25.     term.setBackgroundColor(bc)
  26.     term.setTextColor(tc)
  27. end
  28.  
  29. --Print at left side on specific line
  30. local function printLeft(y, str, bc, tc)
  31.     setColor(bc, tc)
  32.     term.setCursorPos(1, y)
  33.     term.write(str)
  34. end
  35.  
  36. --Print at right side of specific line
  37. local function printRight(y, str, bc, tc)
  38.     setColor(bc, tc)
  39.     term.setCursorPos(w-#str+1, y)
  40.     term.write(str)
  41. end
  42.  
  43. --Print centred if specific line
  44. local function printCentred(y, str, bc, tc)
  45.     setColor(bc, tc)
  46.     term.setCursorPos(w/2-#str/2, y)
  47.     term.write(str)
  48. end
  49.  
  50. --Print at specific point on screen
  51. local function printHere(x, y, str, bc, tc)
  52.     setColor(bc, tc)
  53.     term.setCursorPos(x, y)
  54.     term.write(str)
  55. end
  56.  
  57. --Get time
  58. local function getTime()
  59.     return textutils.formatTime(os.time(), mTime)
  60. end
  61.  
  62. --Draw time at point
  63. local function printTime(x, y, bc, tc)
  64.     local tTime = getTime()
  65.     if x == "default" then x = w-#tTime+1 end
  66.     printHere(x, y, tTime, bc, tc)
  67. end
  68.  
  69. --Get the computer files
  70. local function getFiles()
  71.     files = {}
  72.     if hidden then
  73.         files = fs.list(dir)
  74.     else
  75.         local temp = fs.list(dir)
  76.         for k, v in pairs(temp) do
  77.             if v:sub(1, 1) ~= '.' then
  78.                 files[#files+1] = v
  79.             end
  80.         end
  81.     end
  82. end
  83.  
  84. --Draw message if an error or something that is not allowed occurs
  85. local function drawError(msg)
  86.     printCentred(h/2-1, string.rep(' ', #msg), 8)
  87.     printCentred(h/2, msg, 128, 16384)
  88.     printCentred(h/2+1, string.rep(' ', #msg), 8)
  89.     sleep(1.5)
  90. end
  91.  
  92. --Draw header of desktop
  93. local function drawHeader()
  94.     printHere(1, 1, string.rep(' ', w), 8, 8)
  95.     printHere(1, h, string.rep(' ', w), 8, 8)
  96.     printLeft(1, 'X', 16384, 32768)
  97.     printHere(3, 1, dir, 8, 32768)
  98.     printTime("default", h, 8, 32768)
  99.     if dir ~= "/Hack/files/" then
  100.         printLeft(h, "         <back> <home>", 8, 32768)
  101.     end
  102.     if fs.exists("/H") then
  103.       if dir ~= "/disk/TEMP/files/" then
  104.          printLeft(h, "<floppy>", 8, 32768)
  105.       end
  106.     end
  107.     if copy[1] then
  108.         printRight(1, "copiato: "..copy[1]..copy[2], 8, 32768)
  109.     end
  110.     for i = 2, h-1 do
  111.         if i == 2 then
  112.             printRight(i, '^', 1, 32768)
  113.         elseif i == h-1 then
  114.             printRight(i, 'v', 1, 32768)
  115.         else
  116.             printRight(i, ' ', 1, 32768)
  117.         end
  118.     end
  119. end
  120.  
  121. --Draw file menu
  122. local function drawMenu()
  123.     for i = 2, h-1 do
  124.         printHere(w-11, i, "           ", 2048, 1)
  125.     end
  126.     if slc == 0 then
  127.         if hidden then
  128.             printHere(w-11, 5, "Nascondi File", 2048, 1)
  129.         else
  130.             printHere(w-11, 5, " Vedi File", 2048, 1)
  131.         end
  132.         if copy[1] then
  133.             printHere(w-11, 6, " De-Copia   ", 2048, 1)
  134.         end
  135.         printHere(w-11, 2, " Nuovo File  ", 2048, 1)
  136.         printHere(w-11, 3, " Nuova Dir   ", 2048, 1)
  137.         printHere(w-11, 4, " Copia     ", 2048, 1)
  138.     elseif slc > 0 then
  139.         if fs.isDir(dir..files[slc]) then
  140.             printHere(w-11, 2, " Apri      ", 2048, 1)
  141.         else
  142.             printHere(w-11, 2, " Esegui       ", 2048, 1)
  143.             printHere(w-11, 8, " Modifica      ", 2048, 1)
  144.         end
  145.         printHere(w-11, 3, " Deseleziona  ", 2048, 1)
  146.         printHere(w-11, 4, " Rinonima    ", 2048, 1)
  147.         printHere(w-11, 5, " Copia      ", 2048, 1)
  148.         printHere(w-11, 6, " Taglia       ", 2048, 1)
  149.         printHere(w-11, 7, " Rimuovi    ", 2048, 1)
  150.     end
  151. end
  152.  
  153. --Click the options in the file menu
  154. local function clickMenu(event)
  155.     if slc == 0 then
  156.         if event[1] == "mouse_click" then
  157.             if event[3] >= w-11 and event[3] <= w-1 and event[4] == 2 then
  158.                 printHere(w-11, 2, "           ", 2048, 1)
  159.                 term.setCursorPos(w-11, 2)
  160.                 local nName = io.read()
  161.                 if nName ~= "" then
  162.                     if fs.exists(dir..nName) then
  163.                         drawError("Il file esiste di gia'")
  164.                     else
  165.                         local f = io.open(dir..nName, 'w')
  166.                         f:close()
  167.                     end
  168.                 end
  169.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 3 then
  170.                 printHere(w-11, 3, "           ", 2048, 1)
  171.                 term.setCursorPos(w-11, 3)
  172.                 local nName = io.read()
  173.                 if nName ~= "" then
  174.                     if fs.exists(dir..nName) then
  175.                         drawError("c'e' gia la cartella")
  176.                     else
  177.                         fs.makeDir(dir..nName)
  178.                     end
  179.                 end
  180.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 4 then
  181.                 if copy[1] then
  182.                     local problem = false
  183.                     if dir == copy[1]..copy[2]..'/' then problem = true end
  184.                     if copy[3] == "copy" and not problem then
  185.                         if fs.exists(dir..copy[2]) then
  186.                             drawError("il path esiste di gia")
  187.                         else
  188.                             fs.copy(copy[1]..copy[2], dir..copy[2])
  189.                         end
  190.                     elseif copy[3] == "move" and not problem then
  191.                         if fs.exists(dir..copy[2]) then
  192.                             drawError("Non posso spostarlo nella stessa directory")
  193.                         else
  194.                             fs.move(copy[1]..copy[2], dir..copy[2])
  195.                             copy = {}
  196.                         end
  197.                     else
  198.                         drawError("copy:13: Non åA8 possibile copiare una directory al suo interno")
  199.                     end
  200.                 end
  201.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 5 then
  202.                 if hidden then
  203.                     hidden = false
  204.                 else
  205.                     hidden = true
  206.                 end
  207.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 6 then
  208.                 if copy[1] then
  209.                     copy = {}
  210.                 end
  211.             end
  212.         end
  213.     elseif slc > 0 then
  214.         if event[1] == "mouse_click" then
  215.             if event[3] >= w-11 and event[3] <= w-1 and event[4] == 2 then
  216.                 if fs.isDir(dir..files[slc]) then
  217.                     dir = dir..files[slc].."/"
  218.                     slc = 0
  219.                     scroll = 0
  220.                 else
  221.                     setColor(32768, 1)
  222.                     return shell.run(dir..files[slc])
  223.                 end
  224.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 3 then
  225.                 slc = 0
  226.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 4 then
  227.                 local tc = 32768
  228.                 if fs.isDir(dir..files[slc]) then tc = 32 end
  229.                 printHere(2, 2+slc-scroll, string.rep(' ', #files[slc]), 1, tc)
  230.                 term.setCursorPos(2, 2+slc-scroll)
  231.                 local nName = io.read()
  232.                 if nName ~= "" then
  233.                     fs.move(dir..files[slc], dir..nName)
  234.                 end
  235.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 5 then
  236.                 if dir..files[slc] ~= "rom" then
  237.                     copy[1] = dir
  238.                     copy[2] = files[slc]
  239.                     copy[3] = "copy"
  240.                 else
  241.                     drawError("accesso negato")
  242.                 end
  243.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 6 then
  244.                 if dir..files[slc] ~= "rom" and dir..files[slc] ~= "disk" then
  245.                     copy[1] = dir
  246.                     copy[2] = files[slc]
  247.                     copy[3] = "move"
  248.                 else
  249.                     drawError("move:13: accesso negato")
  250.                 end
  251.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 7 then
  252.                 printCentred(h/2, "  Sei sicuro? ", 512, 32768)
  253.                 printCentred(h/2+1, " si        no ", 512, 32768)
  254.                 local run = true
  255.                 while run do
  256.                     os.startTimer(0.1)
  257.                     local temp = {os.pullEvent()}
  258.                     if temp[1] == "timer" then
  259.                         printTime("default", h, 512, 32768)
  260.                     elseif temp[1] == "mouse_click" then
  261.                         if temp[2] == 1 and temp[4] == math.floor(h/2+1) then
  262.                             if temp[3] >= math.floor(w/2-6) and temp[3] <= math.floor(w/2-4) then
  263.                                 if dir..files[slc] ~= "rom" and dir..files[slc] ~= "disk" then
  264.                                     fs.delete(dir..files[slc])
  265.                                     slc = 0
  266.                                 else
  267.                                     drawError("delete:9: accesso negato")
  268.                                 end
  269.                                 run = false
  270.                             elseif temp[3] >= math.floor(w/2+4) and temp[3] <= math.floor(w/2+5) then
  271.                                 run = false
  272.                             end
  273.                         end
  274.                     end
  275.                 end
  276.             elseif event[3] >= w-11 and event[3] <= w-1 and event[4] == 8 then
  277.                 if not fs.isDir(dir..files[slc]) then
  278.                     return shell.run("edit "..dir..files[slc])
  279.                 end
  280.             end
  281.         end
  282.     end
  283.     if event[1] == "mouse_click" and event[2] == 1 and event[3] >= 1 and event[3] <= 8 and event[4] == h and dir ~= "/disk/TEMP/files/" then
  284.         dir = "/disk/TEMP/files/"
  285.         slc = 0
  286.         scroll = 0
  287.     elseif event[1] == "mouse_click" and event[2] == 1 and event[3] >= 10 and event[3] <= 15 and event[4] == h and dir ~= "/Hack/files/" then
  288.       if dir ~= "/disk/TEMP/files/" then
  289.         local slash = 0
  290.         for i = 1, #dir-1 do
  291.         if dir:sub(i, i) == '/' then
  292.           slash = i
  293.         end
  294.      end
  295.         if slash == 0 then dir = "/Hack/files/"
  296.         else dir = dir:sub(1, slash) end
  297.         slc = 0
  298.         scroll = 0
  299.       else
  300.       dir = "/Hack/files/"
  301.       end
  302.     elseif event[1] == "mouse_click" and event[2] == 1 and event[3] >= 17 and event[3] <= 22 and event[4] == h and dir ~= "Hack/files/" then
  303.         dir = "/Hack/files/"
  304.         slc = 0
  305.         scroll = 0
  306. end
  307. end
  308.  
  309. --Draw the files and directories
  310. local function drawFiles()
  311.     for k, v in pairs(files) do
  312.         local bc, tc = 1, 32768
  313.         if fs.isDir(dir..v) then tc = 32 end
  314.         if k == slc then bc = 8 end
  315.         printHere(2, 2+k-scroll, v, bc, tc)
  316.     end
  317.     drawMenu()
  318. end
  319.  
  320. --Scroll in a large list of files
  321. local function scrollFiles(upDown)
  322.     if upDown == -1 then
  323.         if scroll > 0 then scroll = scroll-1 end
  324.     elseif upDown == 1 then
  325.         if scroll < #files-h/1.2 then scroll = scroll+1 end
  326.     end
  327. end
  328.  
  329. --Click files
  330. local function clickFiles(event)
  331.     if event[1] == "mouse_scroll" then
  332.         scrollFiles(event[2])
  333.     elseif event[1] == "mouse_click" or "mouse_drag" then
  334.         if event[2] == 1 then
  335.             if event[3] == 1 and event[4] == 1 then
  336.                 running = false
  337.                 return nil
  338.             elseif event[3] >= w-#getTime()+1 and event[3] <= w and event[4] == h then
  339.                 if mTime then mTime = false
  340.                 else mTime = true end
  341.             elseif event[3] == w and event[4] >= 2 and event[4] <= h/2 then
  342.                 scrollFiles(-1)
  343.             elseif event[3] == w and event[4] > h/2 and event[4] <= h-1 then
  344.                 scrollFiles(1)
  345.             end
  346.             for k, v in pairs(files) do
  347.                 if event[3] >= 2 and event[3] <= #v+1 and event[4] == 2+k-scroll and event[4] ~= 1 and event[4] ~= h then
  348.                     if slc == k and fs.isDir(dir..files[slc]) then
  349.                         dir = dir..files[slc].."/"
  350.                         slc = 0
  351.                         scroll = 0
  352.                     elseif slc == k and not fs.isDir(dir..files[slc]) then
  353.                         setColor(32768, 1)
  354.                         return shell.run(dir..files[slc])
  355.                     else
  356.                         slc = k
  357.                     end
  358.                 end
  359.             end
  360.         end
  361.     end
  362.     clickMenu(event)
  363. end
  364.  
  365. --Draw the desktop environment
  366. local function drawDesktop()
  367.     if running then
  368.         setColor(1)
  369.         term.clear()
  370.         drawFiles()
  371.         drawHeader()
  372.     else
  373.         shell.run("desktop")
  374.     end
  375. end
  376.  
  377. --Main loop
  378. getFiles()
  379. drawDesktop()
  380. while running do
  381.     os.startTimer(0.1)
  382.     local event = {os.pullEvent()}
  383.     if event[1] == "timer" then
  384.         printTime("default", h, 512, 32768)
  385.     else
  386.         clickFiles(event)
  387.         getFiles()
  388.         drawDesktop()
  389.     end
  390. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement