Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not ftp then
- print("This program uses my ftp AIP.")
- print("It can be found at pastebin.com/b1yTmC2e")
- return false
- end
- if not ftp.send then
- print("The ftp API has not been loaded correctly.")
- print("Did you open rednet before loading it?")
- return false
- end
- t = term.current()
- x,y = t.getSize()
- t.clear()
- universal = {
- formatID = function(id)
- local s = ""
- if string.len((""..id))<3 then
- s = " "
- end
- return s..id
- end
- }
- simple = {
- displayServerSelection = function()
- servers = {rednet.lookup("ftp")}
- i = 1
- selected = false
- local box = function()
- lines = {}
- lines[-3]="Servers"
- lines[-2]="-------"
- lines[-1]="| |"
- lines[0] ="|[ ]|"
- lines[1] ="| |"
- lines[2] ="-------"
- for l,sl in pairs(lines) do
- t.setCursorPos((x/2)-3,(y/2)+l)
- t.write(sl)
- end
- end
- while not selected do
- t.clear()
- box()
- if servers[i-1] then
- t.setCursorPos((x/2)-1,(y/2)-1)
- t.write(universal.formatID(servers[i-1]))
- end
- t.setCursorPos((x/2)-1,y/2)
- t.write(universal.formatID(servers[i]))
- if servers[i+1] then
- t.setCursorPos((x/2)-1,(y/2)+1)
- t.write(universal.formatID(servers[i+1]))
- end
- e,key = os.pullEvent("key")
- if key == 28 then
- selected = true
- end
- if key == 200 then
- if servers[i-1] then
- i = i-1
- end
- end
- if key == 208 then
- if servers[i+1] then
- i = i+1
- end
- end
- end
- return servers[i]
- end,
- mainWindow = function( server )
- t.clear()
- t.setCursorPos(1,1)
- t.write("Loading...")
- tBuffer = window.create(t,1,1,x,y,false)
- localFileDisplay = window.create(tBuffer,2,5,(x-3)/2,y-7,true)
- distantFileDisplay = window.create(tBuffer,((x-3)/2)+3,5,(x-3)/2,y-7,true)
- localDirDisplay = window.create(tBuffer,2,3,(x-3)/2,1,true)
- distantDirDisplay = window.create(tBuffer,((x-3)/2)+3,3,(x-3)/2,1,true)
- localDir = "/"
- distantDir = "/"
- selectedLocal = true
- selectedIndex = 1
- distantDirectoryContentIndexed = {}
- distantIsDir = {}
- localDirectoryContentIndexed = {}
- localIsDir = {}
- local updateLocalDirectoryContent = function()
- localDirectoryContentIndexed = {}
- localIsDir = {}
- local j = 0
- if localDir~="/" then
- j = 1
- localDirectoryContentIndexed[1] = ".."
- localIsDir[1] = true
- end
- for index,file in pairs(fs.list(localDir)) do
- localIsDir[index+j] = fs.isDir(fs.combine(localDir,file))
- if localIsDir[index+j] then
- file = file.."/"
- end
- localDirectoryContentIndexed[index+j] = file
- end
- end
- local updateDistantDirectoryContent = function()
- distantDirectoryContentIndexed = {}
- distantIsDir = {}
- local index = 1
- if distantDir ~= "/" then
- distantDirectoryContentIndexed[1] = ".."
- distantIsDir[1]=true
- index = 2
- end
- for file,isDir in pairs(ftp.getDirectoryListing(server,distantDir)) do
- if isDir then
- file = file.."/"
- end
- distantDirectoryContentIndexed[index]=file
- distantIsDir[index]=isDir
- index = index + 1
- end
- if not selectedLocal then
- selectedIndex = 1
- end
- end
- local writeDirectoryContent = function()
- local lx,ly = localFileDisplay.getSize()
- local dx,dy = distantFileDisplay.getSize()
- localFileDisplay.clear()
- distantFileDisplay.clear()
- for index,name in pairs(localDirectoryContentIndexed) do
- local write = ""
- local append = ""
- if selectedLocal and selectedIndex == index then
- write = "["
- append = "]"
- else
- write = " "
- append = " "
- end
- write = write..name
- localFileDisplay.setCursorPos(1,index)
- localFileDisplay.write(write)
- localFileDisplay.setCursorPos(lx,index)
- localFileDisplay.write(append)
- end
- for index,name in pairs(distantDirectoryContentIndexed) do
- local write = ""
- local append = ""
- if not selectedLocal and selectedIndex == index then
- write = "["
- append = "]"
- else
- write = " "
- append = " "
- end
- write = write..name
- distantFileDisplay.setCursorPos(1,index)
- distantFileDisplay.write(write)
- distantFileDisplay.setCursorPos(dx,index)
- distantFileDisplay.write(append)
- end
- end
- local redraw = function()
- tBuffer.setVisible(false)
- tBuffer.setCursorPos(1,1)
- local box = function()
- s = ""
- for n = 0,x do
- s = s.."-"
- end
- tBuffer.write(s)
- for n=2,y-1 do
- tBuffer.setCursorPos(1,n)
- tBuffer.write("|")
- tBuffer.setCursorPos(x,n)
- tBuffer.write("|")
- end
- tBuffer.setCursorPos(1,y)
- tBuffer.write(s)
- end
- box()
- localDirDisplay.clear()
- localDirDisplay.setCursorPos(1,1)
- localDirDisplay.write(localDir)
- distantDirDisplay.clear()
- distantDirDisplay.setCursorPos(1,1)
- distantDirDisplay.write(distantDir)
- writeDirectoryContent()
- tBuffer.setVisible(true)
- end
- updateLocalDirectoryContent()
- updateDistantDirectoryContent()
- redraw()
- running = true
- while running do
- e,p1 = os.pullEvent("key")
- if p1 == 16 then
- running = false
- tBuffer.clear()
- elseif p1 == 200 then
- if selectedIndex > 1 then
- selectedIndex = selectedIndex - 1
- redraw()
- end
- elseif p1 == 208 then
- if selectedLocal then
- if selectedIndex < #localDirectoryContentIndexed then
- selectedIndex = selectedIndex + 1
- else
- print(selectedIndex.." - "..#localDirectoryContentIndexed)
- end
- else
- if selectedIndex < #distantDirectoryContentIndexed then
- selectedIndex = selectedIndex + 1
- end
- end
- redraw()
- elseif p1 == 15 then
- selectedLocal = not selectedLocal
- selectedIndex = 1
- redraw()
- elseif p1 == 28 then
- if selectedLocal then
- if localIsDir[selectedIndex] then
- localDir = fs.combine(localDir,localDirectoryContentIndexed[selectedIndex]).."/"
- updateLocalDirectoryContent()
- redraw()
- else
- ftp.send(server,fs.combine(localDir,localDirectoryContentIndexed[selectedIndex]),fs.combine(distantDir,localDirectoryContentIndexed[selectedIndex]))
- updateDistantDirectoryContent()
- redraw()
- end
- else
- if distantIsDir[selectedIndex] then
- distantDir = fs.combine(distantDir,distantDirectoryContentIndexed[selectedIndex]).."/"
- updateDistantDirectoryContent()
- redraw()
- else
- ftp.receive(server,fs.combine(distantDir,distantDirectoryContentIndexed[selectedIndex]),fs.combine(localDir,distantDirectoryContentIndexed[selectedIndex]))
- updateLocalDirectoryContent()
- redraw()
- end
- end
- redraw()
- end
- end
- end
- }
- used = simple
- --[[if t.isColor() then
- used = advanced
- end]]
- local a = rednet.lookup("ftp")
- if a then
- server = used.displayServerSelection()
- used.mainWindow(server)
- else
- print("No ftp servers found")
- end
Advertisement
Add Comment
Please, Sign In to add comment