Advertisement
MKlegoman357

RedFile 1.1

May 20th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.30 KB | None | 0 0
  1. --Version: 1.1
  2. --
  3. --Easy to use RedNet file Sender and Receiver with graphical interface
  4. --    - To open file browser just run the program
  5. --      - In file browser you can choose your file, open menu (press m) and send it to any other computer that has this program
  6. --      - File browser also has functions like open, edit, create folder/file, delete
  7. --    - You can also run this program with arguments (this way you can send and get files using other programs; eg.: shell.run("rn", "send", "14", "myfile"))
  8. --      - Sending: rn send <computer id> <file>
  9. --          rn - this program
  10. --          send - 1st argument tells what to do, in this case 'send'
  11. --          <computer id> - 2nd argument is ID of computer that you want to send to (recipient)
  12. --          <file> 3rd argument is file you want to send
  13. --          
  14. --          Example: rn send 28 myprogram
  15. --        
  16. --      - Getting: rn get <computer id>
  17. --          rn - this program
  18. --          get - 1st argument tells what to do, in this case 'get'
  19. --          <computer id> - 2nd argument is optional; use it if you want to get file from a specific computer
  20. --          
  21. --          Example: rn get 28
  22. --    
  23. --     features:
  24. --       - Sending any type of file with any characters
  25. --       - When getting a file you can save it where ever you want
  26. --       - If there is 1 modem attached on a computer - uses that; If there is more than 1 modem - lets you choose wich one you want to use
  27. --       - See proggress in percents when sending and getting a file (try sending rom/programs/secret/alongtimeago, it will crash when it will try to save it, but you will see how it shows the proggress)
  28. --       - You can use this as your file browser
  29. --       - Working on Advanced and normal computers
  30.  
  31. local sides = {}
  32. local side = ""
  33. local col = term.isColor()
  34. local arg = {...}
  35. local path = ""
  36. local sendID = 0
  37. local sendb = false
  38. local getb = false
  39. local running = true
  40.  
  41. --Helper functions
  42.  
  43. local function getSides ()
  44.   local sides = {}
  45.  
  46.   for _, sid in pairs(rs.getSides()) do
  47.     if peripheral.isPresent(sid) and peripheral.getType(sid) == "modem" then
  48.      sides[#sides + 1] = sid
  49.     end
  50.   end
  51.  
  52.   return sides
  53. end
  54.  
  55. local function clear ()
  56.   term.clear()
  57.   term.setCursorPos(1, 1)
  58. end
  59.  
  60. local function resetColors ()
  61.   term.setTextColor(colors.white)
  62.   term.setBackgroundColor(colors.black)
  63. end
  64.  
  65. local function reset ()
  66.   resetColors()
  67.   clear()
  68. end
  69.  
  70. local function conTColor (ColorColor)
  71.   if col or ColorColor == colors.white or ColorColor == colors.black then
  72.     return ColorColor or colors.white
  73.   else
  74.     return colors.black
  75.   end
  76. end
  77.  
  78. local function conBColor (ColorColor)
  79.   if col or ColorColor == colors.white or ColorColor == colors.black then
  80.     return ColorColor or colors.black
  81.   else
  82.     return colors.white
  83.   end
  84. end
  85.  
  86. local function writeText (text, tColor, bColor, x, y, clear)
  87.   if not x then
  88.     x = term.getCursorPos()
  89.   end
  90.  
  91.   if not y then
  92.     _, y = term.getCursorPos()
  93.   end
  94.  
  95.   if tColor then
  96.     term.setTextColor(conTColor(tColor))
  97.   end
  98.  
  99.   if bColor then
  100.     term.setBackgroundColor(conBColor(bColor))
  101.   end
  102.  
  103.   term.setCursorPos(x, y)
  104.  
  105.   if clear == true then
  106.     term.clear()
  107.   end
  108.  
  109.   term.write(tostring(text))
  110. end
  111.  
  112. local function vRep (text, times, tColor, bColor, x, y)
  113.   local w, h = term.getSize()
  114.  
  115.   if not x then
  116.     x = term.getCursorPos()
  117.   end
  118.  
  119.   if not y then
  120.     _, y = term.getCursorPos()
  121.   end
  122.  
  123.   times = times or 1
  124.  
  125.   for i = 1, times do
  126.     writeText(text, tColor, bColor, x, y)
  127.     y = y + 1
  128.   end
  129. end
  130.  
  131. local function clamp (Number, min, max)
  132.   if Number < min then
  133.     return min
  134.   elseif Number > max then
  135.     return max
  136.   else
  137.     return Number
  138.   end
  139. end
  140.  
  141. local function getMin (...)
  142.   local arg = {...}
  143.   local num
  144.  
  145.   for i, number in pairs(arg) do
  146.     if not arg[i - 1] then
  147.       num = tonumber(number)
  148.     else
  149.       if tonumber(number) < num then
  150.         num = number
  151.       end
  152.     end
  153.   end
  154.  
  155.   return num
  156. end
  157.  
  158. local function getMax (...)
  159.   local arg = {...}
  160.   local num
  161.  
  162.   for i, number in pairs(arg) do
  163.     if not arg[i - 1] then
  164.       num = tonumber(number)
  165.     else
  166.       if tonumber(number) > num then
  167.         num = number
  168.       end
  169.     end
  170.   end
  171.  
  172.   return num
  173. end
  174.  
  175. local function selectionMenu(TableSelections, NumberKey, limit)
  176.   local xO, yO = term.getCursorPos()
  177.   local x, y = term.getCursorPos()
  178.   local w, h = term.getSize()
  179.   local selection = 1
  180.   local pos = 1
  181.  
  182.   limit = limit or #TableSelections
  183.  
  184.   local function scroll ()
  185.     if selection < pos then
  186.       pos = selection
  187.     elseif selection > pos + limit - 1 then
  188.       pos = selection - limit + 1
  189.     end
  190.   end
  191.  
  192.   while true do
  193.   x, y = xO, yO
  194.   term.setCursorPos(x, y)
  195.  
  196.   scroll()
  197.  
  198.   for n = pos, getMin(#TableSelections, limit + pos - 1) do
  199.     if TableSelections[n] == "" then
  200.       term.setCursorPos(x, y)
  201.       term.write(TableSelections[n])
  202.       y = y + 1
  203.     elseif TableSelections[n] then
  204.       term.setCursorPos(x, y)
  205.      
  206.       if selection == n then
  207.         term.write(">" .. TableSelections[n])
  208.       else
  209.         term.write(" " .. TableSelections[n])
  210.       end
  211.       y = y + 1
  212.     end
  213.   end
  214.  
  215.   local event, p1, x, y = os.pullEvent()
  216.  
  217.   if event == "key" then
  218.     if p1 == keys.down then
  219.       if selection < #TableSelections then
  220.         repeat
  221.           selection = selection + 1
  222.         until TableSelections[selection] ~= "" or selection <= 1
  223.       end
  224.     elseif p1 == keys.up then
  225.       if selection > 1 then
  226.         repeat
  227.           selection = selection - 1
  228.         until TableSelections[selection] ~= "" or selection >= #TableSelections
  229.       end
  230.     elseif p1 == keys.enter then
  231.       return selection
  232.     elseif NumberKey and p1 == NumberKey then
  233.       return selection, true
  234.     else
  235.       x, y = xO, yO
  236.       term.setCursorPos(x, y)
  237.     end
  238.   elseif event == "mouse_scroll" then
  239.     if p1 < 0 then
  240.       if selection > 1 then
  241.         repeat
  242.           selection = selection - 1
  243.         until TableSelections[selection] ~= "" or selection >= #TableSelections
  244.       end
  245.     else
  246.       if selection < #TableSelections then
  247.         repeat
  248.           selection = selection + 1
  249.         until TableSelections[selection] ~= "" or selection <= 1
  250.       end
  251.     end
  252.   elseif event == "mouse_click" then
  253.     local xw, yw = xO, yO
  254.     local ok = false
  255.    
  256.     for n = pos, getMin(#TableSelections, limit + pos - 1) do
  257.       if y == yw and x >= xw and x <= xw + #TableSelections[n] then
  258.         selection = n
  259.         ok = true
  260.         break
  261.       end
  262.      
  263.       yw = yw + 1
  264.     end
  265.    
  266.     if ok == true and p1 == 1 then
  267.       return selection
  268.     elseif p1 == 2 then
  269.       return selection, true
  270.     end
  271.   end
  272.  
  273.   x, y = xO, yO
  274.   term.setCursorPos(x, y)
  275.  
  276.   for n = pos, getMin(#TableSelections, limit + pos - 1) do
  277.     if TableSelections[n] then
  278.       term.setCursorPos(x, y)
  279.       term.write(string.rep(" ", #TableSelections[n] + 1))
  280.       y = y + 1
  281.     end
  282.   end
  283.  
  284.   selection = clamp(selection, 1, #TableSelections)
  285.   end
  286. end
  287.  
  288. local function closeSides ()
  289.   sides = getSides()
  290.  
  291.   if #sides > 0 then
  292.     for _, s in pairs(sides) do
  293.       rednet.close(s)
  294.     end
  295.   end
  296. end
  297.  
  298. local function readAll (StringPath)
  299.   if not fs.exists(StringPath) then
  300.     return
  301.   end
  302.  
  303.   local file = fs.open(StringPath, "r")
  304.   local line = file.readLine()
  305.   local lines = {}
  306.  
  307.   while line ~= nil do
  308.     table.insert(lines, line)
  309.     line = file.readLine()
  310.   end
  311.  
  312.   file.close()
  313.  
  314.   return lines
  315. end
  316.  
  317. local function list (StringPath, StringType)
  318.   if not fs.isDir(StringPath) then
  319.     return
  320.   end
  321.  
  322.   local list = {}
  323.   local files = {}
  324.  
  325.   if string.sub(StringPath, #StringPath - 1) ~= "/" then
  326.     StringPath = StringPath .. "/"
  327.   end
  328.  
  329.   list = fs.list(StringPath)
  330.  
  331.   if list ~= {} then
  332.     if StringType == "files" or StringType == "file" or StringType == "doc" or StringType == "document" or StringType == "documents" then
  333.       for _, file in pairs(list) do
  334.         if not fs.isDir(StringPath .. file) and fs.exists(StringPath .. file) then
  335.           table.insert(files, file)
  336.         end
  337.       end
  338.     elseif StringType == "folders" or StringType == "directories" or StringType == "folder" or StringType == "dir" or StringType == "directorie" then
  339.       for _, file in pairs(list) do
  340.         if fs.isDir(StringPath .. file) then
  341.           table.insert(files, file .. "/")
  342.         end
  343.       end
  344.     else
  345.       files = list
  346.     end
  347.   end
  348.  
  349.   table.sort(files)
  350.  
  351.   return files
  352. end
  353.  
  354. local function toString (TableText)
  355.   local text = ""
  356.  
  357.   for i = 1, #TableText do
  358.     text = text .. TableText[i] .. "\n"
  359.   end
  360.  
  361.   text = text:sub(1, #text - 1)
  362.  
  363.   return text
  364. end
  365.  
  366. local function writeAll (StringPath, StringText)
  367.   local file = fs.open(StringPath, "w")
  368.  
  369.   file.write(StringText or "")
  370.  
  371.   file.close()
  372. end
  373.  
  374. local function checkFolders (text)
  375.   local folders = {}
  376.   local p1, p2
  377.   local path = ""
  378.  
  379.   text = shell.resolve(text)
  380.  
  381.   for i = 1, #text do
  382.     if text:sub(i, i) ~= "/" then
  383.       if not p1 then
  384.         p1 = i
  385.       end
  386.      
  387.       if i == #text then
  388.         folders[#folders + 1] = text:sub(p1)
  389.       end
  390.     else
  391.       p2 = i - 1
  392.      
  393.       folders[#folders + 1] = text:sub(p1, p2)
  394.      
  395.       p1, p2 = nil, nil
  396.     end
  397.   end
  398.  
  399.   for _, folder in pairs(folders) do
  400.     if not fs.isDir(folder) then
  401.       fs.makeDir(path .. folder)
  402.     end
  403.    
  404.     path = path .. folder .. "/"
  405.   end
  406. end
  407.  
  408. --Error checking and setting variables
  409.  
  410. local function usage (err)
  411.   if err then
  412.     writeText(err, colors.yellow)
  413.     print()
  414.   end
  415.  
  416.   resetColors()
  417.  
  418.   print("Usage:")
  419.   print("rn send <computer id> <file to send>")
  420.   print("rn get <computer id>")
  421.   print()
  422.   print("Don't use any arguments to run GUI version of this utility.")
  423.   print()
  424.   print('Ps: only specify "<computer id>" when getting a file if you want to get it from a specific computer.')
  425.  
  426.   error()
  427. end
  428.  
  429. if #arg > 0 then
  430.   if arg[1]:lower() == "send" then
  431.     if #arg == 3 then
  432.       if fs.exists(arg[3]) and not fs.isDir(arg[3]) then
  433.         path = arg[3]
  434.       else
  435.         usage("Invalid argument (3); specified file doesn't exist.")
  436.       end
  437.      
  438.       if tonumber(arg[2]) and tonumber(arg[2]) > 0 then
  439.         sendID = tonumber(arg[2])
  440.       else
  441.         usage("Invalid argument (2); specified computer id is not valid.")
  442.       end
  443.     else
  444.       usage()
  445.     end
  446.    
  447.     sendb = true
  448.   elseif arg[1]:lower() == "get" then
  449.     if tonumber(arg[2]) and tonumber(arg[2]) > 0 then
  450.       sendID = tonumber(arg[2])
  451.     end
  452.    
  453.     getb = true
  454.   else
  455.     usage()
  456.   end
  457. end
  458.  
  459. --{{Main Code}}
  460.  
  461. local function drawFrame (small, ColorClear)
  462.   local x, y = 1, 1
  463.   local w, h = term.getSize()
  464.  
  465.   if small == true then
  466.     x = w / 4
  467.     y = h / 4
  468.     w = w / 2
  469.     h = h / 2
  470.    
  471.     x = math.floor(x + 0.5)
  472.     y = math.floor(y + 0.5)
  473.     w = math.floor(w + 0.5)
  474.     h = math.floor(h + 0.5)
  475.   end
  476.  
  477.   vRep(" ", h, nil, colors.lightBlue, x, y)
  478.   vRep(" ", h, nil, colors.lightBlue, x + w - 1, y)
  479.   writeText(string.rep(" ", w), nil, colors.lightBlue, x, y)
  480.   writeText(string.rep(" ", w), nil, colors.lightBlue, x, y + h - 1)
  481.  
  482.   for x = x + 1, w + x - 2 do
  483.     for y = y + 1, h + y - 2 do
  484.       writeText(" ", nil, ColorClear or colors.black, x, y)
  485.     end
  486.   end
  487.  
  488.   resetColors()
  489. end
  490.  
  491. local function window (TableContent)
  492.   local x, y = 1, 1
  493.   local w, h = term.getSize()
  494.  
  495.   w, h = w - 1, h - 1
  496.   x, y = w / 4 + 2, h / 4 + 2
  497.  
  498.   drawFrame(true)
  499.  
  500.   for i = 1, #TableContent do
  501.     writeText(TableContent[i], nil, nil, x, y + i - 1)
  502.   end
  503. end
  504.  
  505. --Checking for modems
  506.  
  507. local function waitForModem ()
  508.   drawFrame(true)
  509.  
  510.   local x, y = 1, 1
  511.   local w, h = term.getSize()
  512.  
  513.   w, h = w - 1, h - 1
  514.  
  515.   while running do
  516.     sides = getSides()
  517.     x, y = w / 4 + 2, h / 4 + 2
  518.    
  519.     if #sides > 0 then
  520.       if #sides == 1 then
  521.         side = sides[1]
  522.         running = false
  523.       else
  524.         writeText("Select modem:", nil, nil, x, y)
  525.         term.setCursorPos(x, y + 1)
  526.        
  527.         side = sides[selectionMenu(sides)]
  528.         running = false
  529.       end
  530.     else
  531.       writeText("Attach a modem", nil, nil, x, y)
  532.       writeText("Press any key to cancel", nil, nil, x, y + 2)
  533.      
  534.       repeat
  535.         local event, side = os.pullEvent()
  536.        
  537.         if event == "key" then
  538.           return false
  539.         end
  540.       until event == "peripheral" and peripheral.getType(side) == "modem"
  541.     end
  542.   end
  543.  
  544.   rednet.open(side)
  545.  
  546.   running = true
  547.   return true
  548. end
  549.  
  550. --Sending function
  551.  
  552. local function send ()
  553.   if not waitForModem() then
  554.     return false
  555.   end
  556.  
  557.   local event, id, message
  558.   local name = fs.getName(path)
  559.   local percent = 0
  560.   local file = toString(readAll(path))
  561.   local subfile = ""
  562.   local i = 0
  563.   local x, y = 1, 1
  564.   local w, h = term.getSize()
  565.  
  566.   w, h = w - 1, h - 1
  567.   x, y = w / 4 + 2, h / 4 + 2
  568.  
  569.   local function toPercent ()
  570.     local per = tostring(percent)
  571.    
  572.     while #per < 3 do
  573.       per = per .. " "
  574.     end
  575.    
  576.     return per
  577.   end
  578.  
  579.   while running do
  580.     window({"Waiting for recipient ".. sendID, "Press \"R\" to retry", "", "Press any key to cancel"})
  581.    
  582.     rednet.send(sendID, "request " .. name)
  583.    
  584.     repeat
  585.       event, id, message = os.pullEvent()
  586.      
  587.       if event == "key" then
  588.         if id == keys.r then
  589.           rednet.send(sendID, "request " .. name)
  590.         else
  591.           return false
  592.         end
  593.       end
  594.     until event == "rednet_message" and id == sendID
  595.    
  596.     if message == "get" then
  597.       window({"Sending: " .. name, percent .. "%"})
  598.      
  599.       if #file <= 300 then
  600.         writeText(percent .. "%", nil, nil, x, y + 1)
  601.        
  602.         rednet.send(sendID, toPercent() .. " " .. file)
  603.        
  604.         repeat
  605.           event, id = os.pullEvent("rednet_message")
  606.         until id == sendID
  607.        
  608.         rednet.send(sendID, "sent")
  609.        
  610.         window({"Successfully sent:", name, "", "Press any key to exit"})
  611.         os.pullEvent("key")
  612.         return true
  613.       else
  614.         while true do
  615.         if i ~= #file then
  616.           writeText(percent .. "%", nil, nil, x, y + 1)
  617.          
  618.           if #file <= i + 300 then
  619.             subfile = file:sub(i + 1)
  620.            
  621.             i = #file
  622.           else
  623.             subfile = file:sub(i + 1, i + 300)
  624.            
  625.             percent = math.floor(i / #file * 100)
  626.             i = i + 300
  627.           end
  628.          
  629.           rednet.send(sendID, toPercent() .. " " .. subfile)
  630.          
  631.           repeat
  632.             event, id = os.pullEvent("rednet_message")
  633.           until id == sendID
  634.         else
  635.           rednet.send(sendID, "sent")
  636.          
  637.           window({"Successfully sent:", name, "", "Press any key to exit"})
  638.           os.pullEvent("key")
  639.           return true
  640.         end
  641.         end
  642.       end
  643.     elseif message == "refuse" then
  644.       window({"Recipient refused", "", "Press any key to exit"})
  645.       os.pullEvent("key")
  646.       return false
  647.     end
  648.   end
  649.  
  650.   running = true
  651.  
  652.   closeSides()
  653. end
  654.  
  655. --Getting function
  656.  
  657. local function get ()
  658.   if not waitForModem() then
  659.     return false
  660.   end
  661.  
  662.   local getName = "unknown"
  663.   local text = {"Waiting for request", "Sender ID: any", "", "Press any key to cancel"}
  664.   local event, id, message
  665.   local selection = 0
  666.   local percent = 0
  667.   local file = ""
  668.   local x, y = 1, 1
  669.   local w, h = term.getSize()
  670.  
  671.   w, h = w - 1, h - 1
  672.   x, y = w / 4 + 2, h / 4 + 2
  673.  
  674.   if sendID ~= 0 then
  675.     text[2] = "Sender ID: " .. sendID
  676.   end
  677.  
  678.   while running do
  679.     window(text)
  680.    
  681.     repeat
  682.       event, id, message = os.pullEvent()
  683.      
  684.       if event == "key" then
  685.         return false
  686.       end
  687.     until event == "rednet_message" and (id == sendID or sendID == 0)
  688.    
  689.     if sendID == 0 then
  690.       sendID = id
  691.     end
  692.    
  693.     if message:sub(1, 7) == "request" then
  694.       getName = message:sub(9)
  695.     end
  696.    
  697.     window({"Getting file from " .. sendID, "Name: " .. getName})
  698.     term.setCursorPos(x, y + 3)
  699.     selection = selectionMenu({"Save", "Cancel"})
  700.    
  701.     if selection == 2 then
  702.       rednet.send(sendID, "refuse")
  703.       return false
  704.     else
  705.       window({"Enter save path:"})
  706.       term.setCursorPos(x, y + 1)
  707.       path = shell.resolve(read())
  708.       rednet.send(sendID, "get")
  709.     end
  710.    
  711.     window({"Getting: " .. getName, percent .. "%"})
  712.    
  713.     repeat
  714.       writeText(percent .. "%", nil, nil, x, y + 1)
  715.      
  716.       event, id, message = os.pullEvent("rednet_message")
  717.      
  718.       if id == sendID and message ~= "sent" then
  719.         percent = tonumber(message:sub(1, 3))
  720.        
  721.         file = file .. message:sub(5)
  722.        
  723.         rednet.send(sendID, "ok")
  724.       end
  725.     until id == sendID and message == "sent"
  726.    
  727.     window({"Saving: " .. getName, "Path: " .. path .. "/" .. getName})
  728.     checkFolders(path)
  729.     writeAll(path .. "/" .. getName, file)
  730.     window({"Saved " .. getName, "Path: " .. path .. "/" .. getName, "", "Press any key to exit"})
  731.     os.pullEvent("key")
  732.     return true
  733.   end
  734.  
  735.   running = true
  736.  
  737.   closeSides()
  738. end
  739.  
  740. --Running program and File Manager
  741.  
  742. if sendb == true then
  743.   send()
  744. elseif getb == true then
  745.   get()
  746. else
  747.   local currentPath = "/"
  748.   local files = list(currentPath, "files")
  749.   local folders = list(currentPath, "folders")
  750.   local fileList = {}
  751.   local x, y = 2, 2
  752.   local w, h = term.getSize()
  753.   local selection = 0
  754.   local menu = false
  755.   local selected = 0
  756.  
  757.   local function combine ()
  758.     if #files == 0 then
  759.       return folders
  760.     end
  761.    
  762.     if #folders == 0 then
  763.       return files
  764.     end
  765.    
  766.     local all = folders
  767.    
  768.     for _, file in pairs(files) do
  769.       all[#all + 1] = file
  770.     end
  771.    
  772.     return all
  773.   end
  774.  
  775.   local function back (StringPath)
  776.     if StringPath == "/" then
  777.       return StringPath
  778.     end
  779.    
  780.     repeat
  781.       StringPath = string.sub(StringPath, 1, #StringPath - 1)
  782.     until string.sub(StringPath, #StringPath) == "/" or #StringPath <= 1
  783.    
  784.     return StringPath
  785.   end
  786.  
  787.   local function getFolder (StringPath)
  788.     local string = back(StringPath)
  789.    
  790.     return string.sub(StringPath, #string + 1)
  791.   end
  792.  
  793.   while running do
  794.     files = list(currentPath, "files")
  795.     folders = list(currentPath, "folders")
  796.     fileList = combine()
  797.    
  798.     table.insert(fileList, 1, "/")
  799.     fileList[#fileList + 1] = ""
  800.     fileList[#fileList + 1] = "Exit"
  801.    
  802.     if menu then
  803.       local x, y = 1, 1
  804.       local w, h = term.getSize()
  805.      
  806.       w, h = w - 1, h - 1
  807.       x, y = w / 4 + 2, h / 4 + 2
  808.      
  809.       drawFrame(true)
  810.       term.setCursorPos(x, y)
  811.       selection = selectionMenu({"Open", "Edit", "Send", "Get", "New Folder", "New File", "Delete", "Close modems", "Help", "", "Back"}, nil, math.floor(h / 2) - 1)
  812.      
  813.       if selection == 1 then
  814.         if selected then
  815.           if fs.isDir(currentPath .. selected) then
  816.             currentPath = currentPath .. selected
  817.             menu = false
  818.           else
  819.             shell.run(currentPath .. selected)
  820.             menu = false
  821.           end
  822.         else
  823.           window({"Choose a file first", "", "Press any key"})
  824.           os.pullEvent("key")
  825.           menu = false
  826.         end
  827.       elseif selection == 2 then
  828.         if selected then
  829.           if fs.isDir(currentPath .. selected) then
  830.             window({"Can't edit a folder", "", "Press any key"})
  831.             os.pullEvent("key")
  832.             menu = false
  833.           else
  834.             shell.run("edit", currentPath .. selected)
  835.             menu = false
  836.           end
  837.         else
  838.           window({"Choose a file first", "", "Press any key"})
  839.           os.pullEvent("key")
  840.           menu = false
  841.         end
  842.       elseif selection == 3 then
  843.         if selected then
  844.           path = currentPath .. selected
  845.           window({"Enter recipient ID:"})
  846.           term.setCursorPos(x, y + 1)
  847.           sendID = tonumber(read()) or 0
  848.           send()
  849.           path = ""
  850.           sendID = 0
  851.           menu = false
  852.         else
  853.           window({"Choose a file first", "", "Press any key"})
  854.           os.pullEvent("key")
  855.           menu = false
  856.         end
  857.       elseif selection == 4 then
  858.         window({"Enter sender ID:", "", "", "Leave it blank if you", "don't know sender id"})
  859.         term.setCursorPos(x, y + 1)
  860.         sendID = tonumber(read()) or 0
  861.         get()
  862.         sendID = 0
  863.         path = ""
  864.         menu = false
  865.       elseif selection == 5 then
  866.         window({"Create new folder:"})
  867.         term.setCursorPos(x, y + 1)
  868.         local name = read()
  869.        
  870.         if not fs.exists(currentPath .. name) then
  871.           fs.makeDir(currentPath .. name)
  872.           menu = false
  873.         else
  874.           window({"Couldn't create folder", "", "Press any key"})
  875.           os.pullEvent("key")
  876.           menu = false
  877.         end
  878.       elseif selection == 6 then
  879.         window({"Create new file:"})
  880.         term.setCursorPos(x, y + 1)
  881.         local name = read()
  882.        
  883.         if not fs.exists(currentPath .. name) then
  884.           shell.run("edit", currentPath .. name)
  885.           menu = false
  886.         else
  887.           window({"Couldn't create file", "", "Press any key"})
  888.           os.pullEvent("key")
  889.           menu = false
  890.         end
  891.       elseif selection == 7 then
  892.         if selected then
  893.           window({"Delete " .. selected .. "?"})
  894.           term.setCursorPos(x, y + 1)
  895.           local selection = selectionMenu({"No", "Yes"})
  896.          
  897.           if selection == 2 then
  898.             fs.delete(currentPath .. selected)
  899.             window({"Deleted " .. selected, "", "Press any key"})
  900.             os.pullEvent("key")
  901.             menu = false
  902.           end
  903.         else
  904.           window({"Choose a file first", "", "Press any key"})
  905.           os.pullEvent("key")
  906.           menu = false
  907.         end
  908.       elseif selection == 8 then
  909.         closeSides()
  910.         window({"All modems closed", "", "Press any key"})
  911.         os.pullEvent("key")
  912.       elseif selection == 9 then
  913.         window({"Use mouse or arrow keys", "to navigate the browser.", "Press [M] or right mouse", "button for menu.", "", "Press any key"})
  914.         os.pullEvent("key")
  915.       elseif selection == 10 then
  916.         menu = false
  917.       end
  918.     else
  919.       drawFrame()
  920.       writeText("File Browser | press [M] for menu | ID: " .. os.getComputerID(), nil, nil, x, y)
  921.       writeText("Current path: " .. currentPath, nil, nil, x, y + 1)
  922.       term.setCursorPos(x, y + 3)
  923.       selection, menu = selectionMenu(fileList, keys.m, h - 5)
  924.      
  925.       if not menu then
  926.         if selection == 1 then
  927.           currentPath = back(currentPath)
  928.         elseif selection == #fileList then
  929.           running = false
  930.         else
  931.           selected = fileList[selection]
  932.          
  933.           if not fs.isDir(currentPath .. selected) then
  934.             menu = true
  935.           else
  936.             currentPath = currentPath .. selected
  937.           end
  938.         end
  939.       else
  940.         if selection ~= 1 and selection ~= #fileList then
  941.           selected = fileList[selection]
  942.         else
  943.           selected = nil
  944.         end
  945.       end
  946.     end
  947.   end
  948. end
  949.  
  950. closeSides()
  951.  
  952. reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement