Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- _ _ _ _ _ _ _ _ _
- /\ \ / /\ /\ \ /_/\ /\ \ /\ \ /\ \ / /\ /\ \
- / \ \ / / \ / \ \\ \ \ \ \_\ / \ \ / \ \ / / \ / \ \
- / /\ \ \ / / /\ \__ / /\ \ \\ \ \__/ / / / /\ \ \ / /\ \ \ / / /\ \__ / /\ \ \
- / / /\ \_\/ / /\ \___\ / / /\ \_\\ \__ \/_/ / / /\ \_\ / / /\ \ \ / / /\ \___\ / / /\ \_\
- / /_/_ \/_/\ \ \ \/___// /_/_ \/_/ \/_/\__/\ / / /_/ / // / / \ \_\ \ \ \ \/___// /_/_ \/_/
- / /____/\ \ \ \ / /____/\ _/\/__\ \ / / /__\/ // / / / / / \ \ \ / /____/\
- / /\____\/_ \ \ \ / /\____\/ / _/_/\ \ \ / / /_____// / / / / /_ \ \ \ / /\____\/
- / / / /_/\__/ / / / / /______ / / / \ \ \ / / / / / /___/ / //_/\__/ / / / / /______
- / / / \ \/___/ / / / /_______\ / / / /_/ // / / / / /____\/ / \ \/___/ / / / /_______\
- \/_/ \_____\/ \/__________/ \/_/ \_\/ \/_/ \/_________/ \_____\/ \/__________/
- v2.0
- Complete rewrite :)
- ]]
- local oldTerm = term.current()
- local maxX, maxY = term.getSize()
- local mainWindow = window.create(oldTerm, 1, 1, maxX, maxY)
- --table containing buttons and textboxes, created by fsEAPI
- local items = {}
- local itemsInfo = {}
- local cFiles = {}
- local cDir = "/"
- local history = {}
- local last = ""
- term.redirect(mainWindow)
- os.loadAPI(shell.dir().."fsEAPI")
- fsEAPI.addTextBox(items, "addressBar", cDir, maxX-6, 6, 1)
- fsEAPI.addButton(items, "backButton", "<", 1, 2, 1)
- fsEAPI.addButton(items, "forwardButton", ">", 1, 4, 1)
- fsEAPI.addScrollBox(items, "dir", maxX-2, maxY-3, 2, 3)
- local context = {
- {
- text = "Open",
- bg = colors.white,
- fg = colors.blue,
- },
- {
- text = "Delete",
- bg = colors.white,
- fg = colors.red,
- },
- {
- text = "Copy",
- bg = colors.white,
- fg = colors.black,
- name = "",
- typ = "File",
- data = "",
- folders = {},
- files = {},
- },
- {
- text = "Paste",
- bg = colors.white,
- fg = colors.black,
- },
- {
- text = "Info",
- bg = colors.white,
- fg = colors.black,
- },
- }
- context[1]['func'] = function(path)
- if fs.exists(path) and fs.isDir(path) then
- table.insert(history, cDir)
- cDir = path
- if not string.find(cDir, "/", #cDir) then
- cDir = cDir.."/"
- end
- items.addressBar.text = cDir
- fsEAPI.update(items.addressBar)
- fsEAPI.clearChilds(items.dir)
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- end
- end
- context[2].func = function(path)
- if fs.exists(path) and fs.isReadOnly(path) == false then
- fs.delete(path)
- items.dir.childs = {}
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- end
- end
- context[3].func = function(path)
- context[3].folders = {}
- context[3].files = {}
- if fs.exists(path) and fs.isDir(path) == false then
- local f = fs.open(path, "r")
- context[3].data = f.readAll()
- context[3].name = fs.getName(path)
- context[3].typ = "File"
- f.close()
- elseif fs.exists(path) and fs.isDir(path) then
- table.insert(context[3].folders, fs.getName(path))
- context[3].data = {}
- local function list(path)
- local tab = fs.list(path)
- local root = {}
- local rootf = {}
- local dat = {}
- for k, v in ipairs(tab) do
- if fs.isDir(path.."/"..v) then
- table.insert(rootf, v)
- local c, cf, cd = list(path.."/"..v)
- for each, f in ipairs(c) do
- table.insert(root, v.."/"..f)
- table.insert(dat, cd[each])
- end
- for each, f in ipairs(cf) do
- table.insert(rootf, v.."/"..f)
- end
- else
- table.insert(root, v)
- local f = fs.open(path.."/"..v, "r")
- local liss = f.readAll()
- f.close()
- table.insert(dat, liss)
- end
- end
- return root, rootf, dat
- end
- local c, cf, cd = list(path)
- for k, v in ipairs(c) do
- table.insert(context[3].files, context[3].folders[1].."/"..v)
- table.insert(context[3].data, cd[k])
- end
- for k, v in ipairs(cf) do
- table.insert(context[3].folders, context[3].folders[1].."/"..v)
- end
- context[3].typ = "Folder"
- end
- end
- context[4].func = function(path)
- if fs.isReadOnly(path) == false and #context[3].data > 0 and context[3].typ == "File" then
- if fs.exists(path..context[3].name) then
- local c = 1
- repeat
- local found = fs.exists(path..context[3].name.." - "..tostring(c))
- if found then
- c = c+1
- end
- until found == false
- local f = fs.open(path..context[3].name.." - "..tostring(c), "w")
- f.write(context[3].data)
- f.close()
- else
- local f = fs.open(path..context[3].name, "w")
- f.write(context[3].data)
- f.close()
- end
- elseif fs.isReadOnly(path) == false and #context[3].folders > 0 and context[3].typ == "Folder" then
- local folders = {}
- local files = {}
- local data = {}
- for k, v in ipairs(context[3].files) do
- table.insert(files, v)
- table.insert(data, context[3].data[k])
- end
- for k, v in ipairs(context[3].folders) do
- table.insert(folders, v)
- end
- if fs.exists(path..folders[1]) then
- local oldName = folders[1]
- local c = 1
- repeat
- local found = fs.exists(path..oldName.." - "..tostring(c))
- if found then
- c = c+1
- end
- until found == false
- for k, v in ipairs(folders) do
- folders[k] = string.gsub(folders[k], oldName, oldName.." - "..tostring(c), 1)
- end
- for k, v in ipairs(files) do
- files[k] = string.gsub(files[k], oldName, oldName.." - "..tostring(c), 1)
- end
- end
- local log = fs.open("/log", "w")
- for k, v in ipairs(folders) do
- log.writeLine(v)
- end
- log.close()
- for k, v in ipairs(folders) do
- fs.makeDir(path.."/"..v)
- end
- for k, v in ipairs(files) do
- local f = fs.open(path.."/"..v, "w")
- f.write(data[k])
- f.close()
- end
- end
- end
- fsEAPI.addContextMenu(items.dir, context)
- local w = math.floor(maxX/2)
- local h = math.floor(maxY/2)
- local infoWindow = window.create(oldTerm, math.floor(maxX/2-(w/2)), math.floor(maxY/2-(h/2)), w, h, false)
- fsEAPI.addButton(itemsInfo, "closeButton", "X", 1, w, 1)
- fsEAPI.addScrollBox(itemsInfo, "textInfo", w-2, h-2, 2, 2)
- itemsInfo.closeButton.bg = colors.red
- itemsInfo.closeButton.fg = colors.white
- itemsInfo.textInfo.bg = colors.white
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.black)
- term.clear()
- fsEAPI.updateAll(items, colors.gray)
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- local file = fs.open("/log", "w")
- local function log(str)
- file.writeLine(str)
- file.flush()
- end
- local c = coroutine.create(fsEAPI.run)
- local c2 = coroutine.create(fsEAPI.run)
- local ev = {}
- local inInfo = false
- coroutine.resume(c, items)
- coroutine.resume(c2, itemsInfo)
- while true do
- coroutine.resume(c, unpack(ev))
- ev = {os.pullEvent()}
- if ev[1] == "context_select" and ev[2] == "dir" and ev[4] == "Info" then
- local sel = false
- for _, a in ipairs(items.dir.childs) do
- if a.selected then
- sel = true
- break
- end
- end
- if sel then
- -- clear the previous info text and write a new one
- itemsInfo.textInfo.childs = {}
- local size = fs.getSize(cDir..items.dir.childs[ev[3]].text)
- -- byte to kbyte
- size = size/1000
- local typ = "File"
- if fs.isDir(cDir..items.dir.childs[ev[3]].text) then
- typ = "Folder"
- end
- fsEAPI.addChild(itemsInfo.textInfo, "Type: "..typ)
- itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].bg = colors.white
- itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].fg = colors.black
- fsEAPI.addChild(itemsInfo.textInfo, "Size: "..tostring(size).." KB")
- itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].bg = colors.white
- itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].fg = colors.black
- fsEAPI.addChild(itemsInfo.textInfo, "ReadOnly: "..tostring(fs.isReadOnly(cDir..items.dir.childs[ev[3]].text)))
- itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].bg = colors.white
- itemsInfo.textInfo.childs[#itemsInfo.textInfo.childs].fg = colors.black
- infoWindow.setVisible(true)
- term.redirect(infoWindow)
- fsEAPI.updateAll(itemsInfo, colors.gray)
- local evt = {}
- while true do
- local v = {}
- if evt[1] == "mouse_click" or evt[1] == "mouse_drag" or evt[1] == "mouse_up" or evt[1] == "mouse_down" then
- for each, k in ipairs(evt) do
- table.insert(v, k)
- end
- v[3] = math.floor(v[3]-(maxX/2-(w/2))+1)
- v[4] = math.floor(v[4]-(maxY/2-(h/2))+1)
- else
- for each, k in ipairs(evt) do
- table.insert(v, k)
- end
- end
- coroutine.resume(c2, unpack(v))
- evt = {os.pullEvent()}
- if evt[1] == "button_press" then
- break
- end
- end
- infoWindow.setVisible(false)
- mainWindow.redraw()
- term.redirect(mainWindow)
- end
- elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[1].text then
- local sel = false
- for _, a in ipairs(items.dir.childs) do
- if a.selected then
- sel = true
- break
- end
- end
- if sel then
- context[1].func(cDir..items.dir.childs[ev[3]].text)
- end
- elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[2].text then
- local sel = false
- for _, a in ipairs(items.dir.childs) do
- if a.selected then
- sel = true
- break
- end
- end
- if sel then
- context[2].func(cDir..items.dir.childs[ev[3]].text)
- end
- elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[3].text then
- local sel = false
- for _, a in ipairs(items.dir.childs) do
- if a.selected then
- sel = true
- break
- end
- end
- if sel then
- context[3].func(cDir..items.dir.childs[ev[3]].text)
- end
- elseif ev[1] == "context_select" and ev[2] == "dir" and ev[4] == context[4].text then
- context[4].func(cDir)
- fsEAPI.clearChilds(items.dir)
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- elseif ev[1] == "button_press" and ev[3] == "backButton" and #history > 0 then
- last = cDir
- cDir = history[#history]
- table.remove(history, #history)
- fsEAPI.clearChilds(items.dir)
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- items.addressBar.text = cDir
- fsEAPI.update(items.addressBar)
- elseif ev[1] == "button_press" and ev[3] == "forwardButton" and #last > 0 then
- table.insert(history, cDir)
- cDir = last
- last = ""
- fsEAPI.clearChilds(items.dir)
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- items.addressBar.text = cDir
- fsEAPI.update(items.addressBar)
- elseif ev[1] == "textBox_input" then
- -- check if entered string is a folder, if not change the text, sleep and show the old text
- if not fs.isDir(items[ev[3]].text) then
- items[ev[3]].text = "No such dir."
- items[ev[3]].fg = colors.red
- fsEAPI.update(items[ev[3]])
- sleep(2)
- items[ev[3]].text = cDir
- items[ev[3]].fg = colors.lime
- fsEAPI.update(items[ev[3]])
- else
- table.insert(history, cDir)
- if not string.find(items[ev[3]].text, "/", #items[ev[3]].text) then
- items[ev[3]].text = items[ev[3]].text.."/"
- fsEAPI.update(items[ev[3]])
- end
- cDir = items[ev[3]].text
- fsEAPI.clearChilds(items.dir)
- for each, file in ipairs(fs.list(cDir)) do
- fsEAPI.addChild(items.dir, file)
- if fs.isDir(cDir..file) then
- items.dir.childs[#items.dir.childs].fg = colors.blue
- end
- end
- fsEAPI.update(items.dir)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement