Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local home
- local root
- local clipboard
- if #tArgs > 0 then
- home = tArgs[1]
- else
- home = ""
- end
- if #tArgs > 1 then
- root = tArgs[2]
- else
- root = "/"
- end
- history = {}
- history[1] = root .. home
- local w,h = term.getSize()
- local function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={} ; i=1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- historyclear = function()
- history = {}
- history[1] = root..home
- end
- shellcom = {historyclear}
- local function diabox(txt)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- paintutils.drawFilledBox(math.floor(w/2-(#txt+2)/2),math.floor(h/2)-2,math.floor(w/2+(#txt+2)/2),math.floor(h/2)+2,colors.white)
- term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)-1)
- term.write(txt)
- term.setCursorPos(math.floor(w/2-(#txt+2)/2)+1,math.floor(h/2)+1)
- end
- function readtextbox(txt)
- diabox(txt)
- return read()
- end
- function buttonbox(txt,buttons)
- if type(buttons) ~= "table" then
- buttons = {{" Yes ",colors.red,colors.white},{" No ",colors.gray,colors.white}}
- end
- if txt == "" or txt == nil then
- txt = " Are you sure? "
- end
- local x,y
- diabox(txt)
- for i=1,#buttons do
- x,y = term.getCursorPos()
- x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
- term.setCursorPos(x,y)
- term.setBackgroundColor(buttons[i][2])
- term.setTextColor(buttons[i][3])
- term.write(buttons[i][1])
- end
- while true do
- local _,b,x2,y2 = os.pullEvent("mouse_click")
- if b == 1 and y == y2 then
- for i=1,#buttons do
- local x = math.floor((w/2-(#txt+2)/2)+((#txt+2)/i)*(i-1)) + 1
- if x2 > x - 1 and x2 < x + #buttons[i][1] then
- return i
- end
- end
- end
- end
- end
- local function clear()
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- local function list(path,scroll,selected)
- clear()
- local items = fs.list(path)
- for i=scroll+1,scroll+h-2 do
- if type(items[i]) == "string" then
- if i ~= selected then
- if not fs.isDir(path..items[i]) then
- term.setTextColor(colors.black)
- else
- term.setTextColor(colors.yellow)
- end
- else
- term.setTextColor(colors.white)
- end
- print(items[i])
- end
- end
- return items
- end
- local function drawMenu(path)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.setCursorPos(1,h-1)
- term.clearLine()
- term.setCursorPos(1,h-1)
- term.write(path)
- term.setCursorPos(1,h)
- term.clearLine()
- term.setCursorPos(1,h)
- term.write("| New | MkDir | More | Root | Home | Back | Exit |")
- end
- local function rPrint(txt,y)
- if y == nil then _,y = term.getCursorPos() end
- term.setCursorPos(w-#txt,y)
- term.write(txt)
- term.setCursorPos(1,y+1)
- end
- local function popupMenu(isDir)
- paintutils.drawFilledBox(math.floor(w/2),1,w,h-1)
- if (isDir) then
- rPrint("Open",2)
- rPrint("Unpack")
- else
- rPrint("Edit",2)
- rPrint("Paint")
- rPrint("Open with ...")
- rPrint("Run")
- rPrint("Run w/Args")
- end
- rPrint("Rename",7)
- rPrint("Move")
- rPrint("Copy")
- rPrint("Copy to clipboard")
- rPrint("Delete")
- rPrint("Deselect")
- end
- local function createNewName(name,type)
- if type == nil then type = 1 end
- if type == 1 then return "Unpacked_" .. name end
- if type == 2 then return name .. math.random(1,10000) end
- return name .. "_pasted"
- end
- local function drawLine(y,txt,col)
- if type(col) == nil then col = colors.white end
- term.setCursorPos(1,y)
- term.clearLine()
- term.setCursorPos(math.floor(w/2-#txt/2),y)
- term.write(txt)
- end
- local function customShell()
- clear()
- while true do
- term.setCursorPos(1,h)
- term.clearLine()
- term.setCursorPos(1,h)
- term.write("? ")
- local command = read()
- clear()
- term.setCursorPos(1,1)
- if command == "exit" then return end
- if type(shellcom[command]) == "function" then
- shellcom[command]()
- else
- print("Not a command")
- end
- end
- end
- local function downloadfile(path)
- clear()
- local url = readtextbox(" Please enter url ")
- clear()
- print("Progress:")
- print("Downloading...")
- local httpgot = http.get(url)
- print("Done")
- local name = readtextbox("Please name the file")
- while fs.exists(path..name) do name = createNewName(name,3) end
- local file = fs.open(path..name,"w")
- file.write(httpgot.readAll())
- file.close()
- httpgot.close()
- clear()
- buttonbox("File downloaded",{{"OK",colors.gray,colors.white}})
- end
- local function more(path) --draw ui
- while true do
- clear()
- print("More actions:")
- term.setTextColor(colors.black)
- drawLine(3,"Shell")
- drawLine(5,"Download file")
- drawLine(7,"Paste from clipboard")
- drawLine(9,"Paste from pastebin.com")
- drawLine(11,"Open webpage")
- drawLine(13,"New file...")
- drawLine(15,"Go back")
- --get event
- local _,_,_,y = os.pullEvent()
- if y == 3 then
- customShell()
- elseif y == 5 then
- downloadfile(path)
- elseif y == 7 then
- if fs.exists(clipboard) then
- if clipboard:sub(#clipboard,#clipboard) == "/" then clipboard = clipboard:sub(1,#clipboard-1) end
- local splitted = split(clipboard,"/")
- local name = splitted[#splitted]
- while fs.exists(path..name) do name = createNewName(name,3) end
- fs.copy(clipboard,path..name)
- buttonbox("Pasted as "..name.." in directory "..path,{{"OK",colors.gray,colors.white}})
- else
- buttonbox("File not found: "..clipboard,{{"OK",colors.gray,colors.white}})
- end
- elseif y == 9 then
- --paste from pastebin
- local name
- repeat
- name = readtextbox("Please enter a filename")
- until not fs.exists(path..name)
- local url = readtextbox("Please enter the pastebin URL")
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.clear()
- print("Downloading file: ")
- shell.run("pastebin get "..url.." "..path..name)
- print("Ended. Press any key to continue")
- os.pullEvent()
- elseif y == 11 then
- --open webpage:
- local url = readtextbox(" Please enter the url ")
- local httpsth = http.get(url)
- local cont = httpsth.readAll()
- httpsth.close()
- clear()
- print(cont)
- os.pullEvent()
- elseif y == 13 then
- --new file options
- clear()
- local exc = buttonbox("Please choose a program",{{"Paint",colors.yellow,colors.black},{"Custom",colors.yellow,colors.black}})
- clear()
- local filename = readtextbox("Please enter a filename")
- if not fs.exists(path..filename) then
- if exc == 1 then
- shell.run("paint",path..filename)
- else
- clear()
- local prog = readtextbox("Please choose a program "..root)
- shell.run(prog,path..filename)
- end
- end
- elseif y == 15 then
- return
- end
- end
- end
- local path = root .. home
- local scroll = 0
- local selected = 0
- local items
- local endprogram = false
- while not endprogram do
- repeat
- items = list(path,scroll,selected)
- drawMenu(path)
- if items[selected] ~= nil then popupMenu(fs.isDir(path..items[selected])) end
- local e,b,x,y = os.pullEvent()
- if e == "mouse_click" then
- if items[selected] ~= nil and x > math.floor(w/2) and y < h-1 then
- if fs.isDir(path..items[selected]) then
- --folder specific
- if y == 2 then
- path = path .. items[selected] .. "/"
- selected = 0
- scroll = 0
- history[#history+1] = path
- elseif y == 3 then
- local con = fs.list(path..items[selected].."/")
- for i=1,#con do
- local altname = con[i]
- while fs.exists(path..altname) do
- altname = createNewName(altname)
- end
- fs.move(path..items[selected].."/"..con[i],path..altname)
- end
- fs.delete(path..items[selected])
- selected = 0
- end
- else
- --file specific
- if y == 2 then
- --edit
- shell.run("edit",path.. items[selected])
- elseif y == 3 then
- --open with paint
- shell.run("paint",path..items[selected])
- elseif y == 4 then
- --open with...
- local program = readtextbox("Please choose a program: "..root)
- if program ~= nil or program ~= "" then
- shell.run(root..program,path..items[selected])
- end
- elseif y == 5 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- os.pullEvent()
- shell.run(path..items[selected])
- sleep(0)
- print("Press any key to continue")
- os.pullEvent("key")
- elseif y == 6 then
- local args = readtextbox("Please enter all arguments to run with!")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- os.pullEvent()
- shell.run(path..items[selected] .. " " .. args)
- sleep(0)
- print("Press any key to continue")
- os.pullEvent("key")
- end
- end
- --universal
- if y == 7 then
- local nname = readtextbox("Please enter the new name (empty = cancel)")
- if nname ~= nil or nname ~= "" then
- if not fs.exists(path..nname) then
- fs.move(path..items[selected],path..nname)
- clear()
- buttonbox("Success",{{"OK",colors.gray,colors.white}})
- selected = 0
- else
- clear()
- buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
- end
- end
- elseif y == 8 then
- --move
- local dest = readtextbox("Move to (empty=cancel) "..root)
- if dest ~= nil or dest ~= "" then
- if not fs.exists(root..dest) then
- fs.move(path..items[selected],root..dest)
- clear()
- buttonbox("Success",{{"OK",colors.gray,colors.white}})
- else
- clear()
- buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
- end
- end
- elseif y == 9 then
- --copy
- local destination = readtextbox("Copy to (emplty=cancel) "..root)
- if destination ~= nil or destination ~= "" then
- if not fs.exists(root..destination) then
- fs.copy(path..items[selected],root..destination)
- clear()
- buttonbox("Success",{{"OK",colors.gray,colors.white}})
- else
- clear()
- buttonbox("File already exists",{{"OK",colors.gray,colors.white}})
- end
- end
- elseif y == 10 then
- --clipboard
- clipboard = path .. items[selected]
- elseif y == 11 then
- --delete
- local bclicked = buttonbox()
- if bclicked == 1 then
- fs.delete(path..items[selected])
- clear()
- buttonbox("File deleted",{{"OK",colors.gray,colors.white}})
- selected = 0
- end
- elseif y == 12 then
- selected = 0
- end
- break
- end
- if y == h-1 then
- local npath = readtextbox("Enter new path: (empty=cancel) "..root)
- if npath ~= nil and npath ~= "" and fs.isDir(root..npath) then
- path = root .. npath
- selected = 0
- scroll = 0
- end
- history[#history+1] = path
- elseif y == h then
- --new
- if x > 1 and x < 7 then
- local name = readtextbox("Filename: (empty=cancel) ")
- if name ~= nil or name ~= "" then
- if not fs.exists(path..name) then
- shell.run("edit",path..name)
- end
- end
- end
- if x > 7 and x < 15 then
- local name = readtextbox("Directory's name: (empty=cancel)")
- if name ~= nil or name ~= "" then
- if not fs.exists(path..name) then
- fs.makeDir(path..name)
- end
- end
- end
- if x > 15 and x < 22 then
- --more...
- more(path)
- end
- if x > 22 and x < 29 then
- path = root
- history[#history+1] = path
- selected = 0
- scroll = 0
- end
- if (x > 29 and x < 36) and fs.exists(root .. home) then
- path = root .. home
- history[#history+1] = path
- selected = 0
- scroll = 0
- end
- if x > 36 and x < 43 then
- while true do
- history[#history] = nil
- if #history == 0 then
- if fs.exists(root..home) then
- path = root..home
- history[1] = path
- elseif fs.exists(root) then
- path = root
- history[1] = path
- else
- error("Root folder deleted",0)
- end
- end
- if fs.exists(history[#history]) then
- path = history[#history]
- break
- end
- end
- selected = 0
- end
- if x > 43 and x < 50 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.clear()
- return
- end
- elseif e == "mouse_scroll" then
- if b == 1 and #items - scroll > h-2 then
- scroll = scroll + 1
- end
- if b == -1 and scroll > 0 then
- scroll = scroll - 1
- end
- else
- if y + scroll == selected then
- if items[selected] ~= nil then
- if fs.isDir(path..items[selected]) then
- path = path .. items[selected] .. "/"
- history[#history+1] = path
- selected = 0
- scroll = 0
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.clear()
- shell.run(path..items[selected])
- end
- end
- else
- selected = y + scroll
- end
- end
- end
- until true
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement