Advertisement
MKlegoman357

RedFile 1.2

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