Ibot02

FTP GUI

Apr 14th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.61 KB | None | 0 0
  1. if not ftp then
  2.   print("This program uses my ftp AIP.")
  3.   print("It can be found at pastebin.com/b1yTmC2e")
  4.   return false
  5. end
  6. if not ftp.send then
  7.   print("The ftp API has not been loaded correctly.")
  8.   print("Did you open rednet before loading it?")
  9.   return false
  10. end
  11.  
  12. t = term.current()
  13. x,y = t.getSize()
  14. t.clear()
  15. universal = {
  16.   formatID = function(id)
  17.     local s = ""
  18.     if string.len((""..id))<3 then
  19.       s = " "
  20.     end
  21.     return s..id
  22.   end
  23. }
  24.  
  25. simple = {
  26.   displayServerSelection = function()
  27.     servers = {rednet.lookup("ftp")}
  28.     i = 1
  29.     selected = false
  30.     local box = function()
  31.       lines = {}
  32.         lines[-3]="Servers"
  33.         lines[-2]="-------"
  34.         lines[-1]="|     |"
  35.         lines[0] ="|[   ]|"
  36.         lines[1] ="|     |"
  37.         lines[2] ="-------"
  38.       for l,sl in pairs(lines) do
  39.         t.setCursorPos((x/2)-3,(y/2)+l)
  40.         t.write(sl)
  41.       end
  42.     end
  43.     while not selected do
  44.       t.clear()
  45.       box()
  46.       if servers[i-1] then
  47.         t.setCursorPos((x/2)-1,(y/2)-1)
  48.         t.write(universal.formatID(servers[i-1]))
  49.       end
  50.       t.setCursorPos((x/2)-1,y/2)
  51.       t.write(universal.formatID(servers[i]))
  52.       if servers[i+1] then
  53.         t.setCursorPos((x/2)-1,(y/2)+1)
  54.         t.write(universal.formatID(servers[i+1]))
  55.       end
  56.       e,key = os.pullEvent("key")
  57.       if key == 28 then
  58.         selected = true
  59.       end
  60.       if key == 200 then
  61.         if servers[i-1] then
  62.           i = i-1
  63.         end
  64.       end
  65.       if key == 208 then
  66.         if servers[i+1] then
  67.           i = i+1
  68.         end
  69.       end
  70.     end
  71.     return servers[i]
  72.   end,
  73.   mainWindow = function( server )
  74.     t.clear()
  75.     t.setCursorPos(1,1)
  76.     t.write("Loading...")
  77.     tBuffer = window.create(t,1,1,x,y,false)
  78.     localFileDisplay = window.create(tBuffer,2,5,(x-3)/2,y-7,true)
  79.     distantFileDisplay = window.create(tBuffer,((x-3)/2)+3,5,(x-3)/2,y-7,true)
  80.     localDirDisplay = window.create(tBuffer,2,3,(x-3)/2,1,true)
  81.     distantDirDisplay = window.create(tBuffer,((x-3)/2)+3,3,(x-3)/2,1,true)
  82.     localDir = "/"
  83.     distantDir = "/"
  84.     selectedLocal = true
  85.     selectedIndex = 1
  86.     distantDirectoryContentIndexed = {}
  87.     distantIsDir = {}
  88.     localDirectoryContentIndexed = {}
  89.     localIsDir = {}
  90.     local updateLocalDirectoryContent = function()
  91.       localDirectoryContentIndexed = {}
  92.       localIsDir = {}
  93.       local j = 0
  94.        if localDir~="/" then
  95.         j = 1
  96.         localDirectoryContentIndexed[1] = ".."
  97.         localIsDir[1] = true
  98.       end
  99.       for index,file in pairs(fs.list(localDir)) do
  100.         localIsDir[index+j] = fs.isDir(fs.combine(localDir,file))
  101.         if localIsDir[index+j] then
  102.           file = file.."/"
  103.         end
  104.         localDirectoryContentIndexed[index+j] = file
  105.       end
  106.     end
  107.     local updateDistantDirectoryContent = function()
  108.       distantDirectoryContentIndexed = {}
  109.       distantIsDir = {}
  110.        local index = 1
  111.        if distantDir ~= "/" then
  112.             distantDirectoryContentIndexed[1] = ".."
  113.             distantIsDir[1]=true
  114.             index = 2
  115.       end
  116.       for file,isDir in pairs(ftp.getDirectoryListing(server,distantDir)) do
  117.         if isDir then
  118.           file = file.."/"
  119.         end
  120.         distantDirectoryContentIndexed[index]=file
  121.         distantIsDir[index]=isDir
  122.         index = index + 1
  123.       end
  124.       if not selectedLocal then
  125.         selectedIndex = 1
  126.       end
  127.     end
  128.     local writeDirectoryContent = function()
  129.       local lx,ly = localFileDisplay.getSize()
  130.       local dx,dy = distantFileDisplay.getSize()
  131.       localFileDisplay.clear()
  132.       distantFileDisplay.clear()
  133.       for index,name in pairs(localDirectoryContentIndexed) do
  134.         local write = ""
  135.         local append = ""
  136.         if selectedLocal and selectedIndex == index then
  137.           write = "["
  138.           append = "]"
  139.         else
  140.           write = " "
  141.           append = " "
  142.         end
  143.         write = write..name
  144.         localFileDisplay.setCursorPos(1,index)
  145.         localFileDisplay.write(write)
  146.         localFileDisplay.setCursorPos(lx,index)
  147.         localFileDisplay.write(append)
  148.       end
  149.       for index,name in pairs(distantDirectoryContentIndexed) do
  150.         local write = ""
  151.         local append = ""
  152.         if not selectedLocal and selectedIndex == index then
  153.           write = "["
  154.           append = "]"
  155.         else
  156.           write = " "
  157.           append = " "
  158.         end
  159.         write = write..name
  160.         distantFileDisplay.setCursorPos(1,index)
  161.         distantFileDisplay.write(write)
  162.         distantFileDisplay.setCursorPos(dx,index)
  163.         distantFileDisplay.write(append)
  164.       end
  165.     end
  166.     local redraw = function()
  167.       tBuffer.setVisible(false)
  168.       tBuffer.setCursorPos(1,1)
  169.       local box = function()
  170.         s = ""
  171.         for n = 0,x do
  172.           s = s.."-"
  173.         end
  174.         tBuffer.write(s)
  175.         for n=2,y-1 do
  176.           tBuffer.setCursorPos(1,n)
  177.           tBuffer.write("|")
  178.           tBuffer.setCursorPos(x,n)
  179.           tBuffer.write("|")
  180.         end
  181.         tBuffer.setCursorPos(1,y)
  182.         tBuffer.write(s)
  183.       end
  184.       box()
  185.       localDirDisplay.clear()
  186.       localDirDisplay.setCursorPos(1,1)
  187.       localDirDisplay.write(localDir)
  188.       distantDirDisplay.clear()
  189.       distantDirDisplay.setCursorPos(1,1)
  190.       distantDirDisplay.write(distantDir)
  191.       writeDirectoryContent()
  192.       tBuffer.setVisible(true)
  193.     end
  194.     updateLocalDirectoryContent()
  195.     updateDistantDirectoryContent()
  196.     redraw()
  197.     running = true
  198.     while running do
  199.       e,p1 = os.pullEvent("key")
  200.       if p1 == 16 then
  201.         running = false
  202.         tBuffer.clear()
  203.       elseif p1 == 200 then
  204.         if selectedIndex > 1 then
  205.           selectedIndex = selectedIndex - 1
  206.           redraw()
  207.         end
  208.       elseif p1 == 208 then
  209.         if selectedLocal then
  210.           if selectedIndex < #localDirectoryContentIndexed then
  211.             selectedIndex = selectedIndex + 1
  212.           else
  213.             print(selectedIndex.." - "..#localDirectoryContentIndexed)
  214.           end
  215.         else
  216.           if selectedIndex < #distantDirectoryContentIndexed then
  217.             selectedIndex = selectedIndex + 1
  218.           end
  219.         end
  220.         redraw()
  221.       elseif p1 == 15 then
  222.         selectedLocal = not selectedLocal
  223.         selectedIndex = 1
  224.         redraw()
  225.       elseif p1 == 28 then
  226.         if selectedLocal then
  227.           if localIsDir[selectedIndex] then
  228.             localDir = fs.combine(localDir,localDirectoryContentIndexed[selectedIndex]).."/"
  229.             updateLocalDirectoryContent()
  230.             redraw()
  231.           else
  232.             ftp.send(server,fs.combine(localDir,localDirectoryContentIndexed[selectedIndex]),fs.combine(distantDir,localDirectoryContentIndexed[selectedIndex]))
  233.             updateDistantDirectoryContent()
  234.             redraw()
  235.           end
  236.         else
  237.           if distantIsDir[selectedIndex] then
  238.             distantDir = fs.combine(distantDir,distantDirectoryContentIndexed[selectedIndex]).."/"
  239.             updateDistantDirectoryContent()
  240.             redraw()
  241.           else
  242.             ftp.receive(server,fs.combine(distantDir,distantDirectoryContentIndexed[selectedIndex]),fs.combine(localDir,distantDirectoryContentIndexed[selectedIndex]))
  243.             updateLocalDirectoryContent()
  244.             redraw()
  245.           end
  246.         end
  247.         redraw()
  248.       end
  249.     end
  250.   end
  251. }
  252.  
  253. used = simple
  254. --[[if t.isColor() then
  255.   used = advanced
  256. end]]
  257.  
  258. local a = rednet.lookup("ftp")
  259. if a then
  260.   server = used.displayServerSelection()
  261.   used.mainWindow(server)
  262. else
  263.   print("No ftp servers found")
  264. end
Advertisement
Add Comment
Please, Sign In to add comment