Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Explorer App by LegoStax
- -- Originally designed for an upcoming security suite...
- local RUNNING = false
- local PATH = "/"
- local COPYPATH = ""
- local CUTPATH = ""
- local w,h = term.getSize()
- local WALL = ""
- -- for drawing and scroll
- local beginFiles = 0
- local scrollpos = 1
- local greatestpos = 1
- local pospossible = 1
- local oldItems = nil
- local pos = nil
- local selectedItem = -1
- if term.isColor() or term.isColour() then
- RUNNING = true
- end
- local function clear(bg)
- bg = bg or colors.black
- term.setBackgroundColor(bg)
- term.clear()
- term.setCursorPos(1,1)
- end
- local function printPos(msg,x,y,bg,fg)
- if bg then term.setBackgroundColor(bg) end
- if fg then term.setTextColor(fg) end
- term.setCursorPos(x,y)
- term.write(msg)
- end
- local function printCenter(msg,y,bg,fg)
- local x = (w-msg:len())/2
- printPos(msg,x,y,bg,fg)
- return x
- end
- local function listFiles(d)
- local dir = nil
- if d then
- dir = shell.resolve(d)
- else
- dir = shell.dir()
- end
- local all = fs.list(dir)
- local files = {}
- local folders = {}
- local hidden = settings.get("list.show_hidden")
- for n, item in pairs(all) do
- if hidden or string.sub(item,1,1) ~= "." then
- local path = fs.combine(dir, item)
- if fs.isDir(path) then
- table.insert(folders, item)
- else
- table.insert(files, item)
- end
- end
- end
- table.sort(folders)
- table.sort(files)
- return folders, files
- end
- local function calculateItems()
- local folders, files = listFiles(PATH)
- local items = folders
- beginFiles = #folders+1
- for i = 1,#files do
- table.insert(items,files[i])
- end
- -- calculate greatestpos
- for i = 1,#items do
- if items[i]:len() > greatestpos then
- greatestpos = items[i]:len()
- end
- end
- pospossible = #items-(h-3)
- return items
- end
- local function drawTopBar()
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,1)
- term.clearLine()
- printPos("QuickScape",1,1,colors.gray,colors.white)
- printPos(WALL,13,1,colors.gray,colors.red)
- printPos("X",w,1,colors.red,colors.white)
- end
- local function drawCurrentPath()
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(1,2)
- term.clearLine()
- printPos(" < "..PATH,1,2,colors.lightGray)
- end
- local function drawItems(items)
- if not items then
- items = oldItems
- end
- oldItems = items
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.blue)
- term.setCursorPos(5,3)
- for i = scrollpos,#items do
- if i > #items then break end
- if i == beginFiles then term.setTextColor(colors.black) end
- if i == selectedItem then
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.white)
- if i < beginFiles then term.setTextColor(colors.blue)
- else term.setTextColor(colors.black) end
- end
- term.write(items[i])
- term.setBackgroundColor(colors.white)
- for i = 1,greatestpos do term.write(" ") end
- local x,y = term.getCursorPos()
- y = y+1
- term.setCursorPos(5,y)
- if y > h then break end
- end
- end
- local function rightClickMenu(x,y,mode)
- if mode == 1 then -- file +11
- printPos(" Cut ",x,y,colors.gray,colors.white)
- printPos(" Copy ",x,y+1)
- printPos(" Delete ",x,y+2)
- printPos(" Rename ",x,y+3)
- printPos(" Open With ",x,y+4)
- elseif mode == 2 then -- folder +8
- printPos(" Cut ",x,y,colors.gray,colors.white)
- printPos(" Copy ",x,y+1)
- printPos(" Delete ",x,y+2)
- printPos(" Rename ",x,y+3)
- else -- misc +12
- printPos(" Paste ",x,y,colors.gray,colors.white)
- printPos(" New File ",x,y+1)
- printPos(" New Folder ",x,y+2)
- end
- end
- local function animSelected(msg,x,y,bg,fg)
- printPos(msg,x,y,bg,fg)
- sleep(0.1)
- end
- local function errorBox(e)
- printPos(e,1,1,colors.red,colors.white)
- while true do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" or e[1] == "key" then break end
- end
- end
- local function drawScreen()
- clear(colors.white)
- drawTopBar()
- drawCurrentPath()
- drawItems(calculateItems())
- WALL = ""
- end
- local function closeApp()
- animSelected("X",w,1,colors.white,colors.red)
- RUNNING = false
- end
- local function logVars(msg)
- local f = fs.open("logs", "a")
- f.write("\n"..msg)
- f.close()
- end
- -- 8:"New File" 10:"New Folder" 6:"Rename"
- local function prompt(msg,data)
- local linepadding = ""
- for i = 1,2+msg:len() do
- linepadding = linepadding .. " "
- end
- local y = math.floor(h/2)
- printCenter(" "..msg.." ",y,colors.lightGray,colors.black)
- printCenter(linepadding,y+1)
- printCenter(linepadding,y+2)
- local x = (w-msg:len())/2
- term.setCursorPos(x,y+1)
- if data then
- for i = 1,data:len() do os.queueEvent("char", string.sub(data,i,i)) end
- end
- local input = read()
- return input
- end
- local function run(path)
- shell.switchTab(shell.openTab(path))
- --[[clear(colors.black)
- printCenter("Awaiting term_resize", (h-1)/2)
- local e = {os.pullEvent("term_resize")}
- --w,h = e[2],e[3]
- drawScreen()]]--
- end
- local function rightClickHandler(x,y,mode,item)
- local actualX, actualY = x,y
- if mode == 1 then
- if x > w-10 then
- actualX = w-10
- end
- if y > h-4 then
- actualY = h-4
- end
- rightClickMenu(actualX, actualY, 1)
- while true do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" and e[2] == 1 then
- if e[3] == w and e[4] == 1 then
- closeApp()
- break
- elseif e[3] >= actualX and e[3] <= actualX+11 then
- if e[4] == actualY then -- cut
- animSelected(" Cut ",actualX,actualY,colors.lightGray,colors.black)
- CUTPATH = shell.resolve(PATH..oldItems[item])
- COPYPATH = ""
- break
- elseif e[4] == actualY+1 then -- copy
- animSelected(" Copy ",actualX,actualY+1,colors.lightGray,colors.black)
- COPYPATH = shell.resolve(PATH..oldItems[item])
- CUTPATH = ""
- break
- elseif e[4] == actualY+2 then -- delete
- animSelected(" Delete ",actualX,actualY+2,colors.lightGray,colors.black)
- fs.delete(PATH..oldItems[item])
- selectedItem = -1
- break
- elseif e[4] == actualY+3 then -- rename
- animSelected(" Rename ",actualX,actualY+3,colors.lightGray,colors.black)
- local newname = PATH..prompt("Rename",oldItems[item])
- if not newname == oldItems[item] and not fs.exists(newname) then fs.move(oldItems[item],newname) end
- break
- elseif e[4] == actualY+4 then -- open with
- animSelected(" Open With ",actualX,actualY+4,colors.lightGray,colors.black)
- local newx, newy = nil,nil
- if actualX > 7 then
- printPos(" Edit ",actualX-7,actualY+3,colors.gray,colors.white)
- printPos(" Paint ",actualX-7,actualY+4,colors.gray,colors.white)
- newx = actualX-7
- newy = actualY+3
- else
- printPos(" Edit ",actualX+11,actualY+3,colors.gray,colors.white)
- printPos(" Paint ",actualX+11,actualY+4,colors.gray,colors.white)
- newx = actualX+11
- newy = actualY+3
- end
- while true do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" and e[2] == 1 then
- if e[3] == w and e[4] == 1 then
- RUNNING = false
- break
- elseif e[3] >= newx and e[3] <= newx+7 then
- if e[4] == newy then --edit
- run("edit "..PATH..oldItems[item])
- break
- elseif e[4] == newy+1 then --paint
- run("paint "..PATH..oldItems[item])
- break
- else break end
- else break end
- end
- end
- break
- elseif e[4] > actualY+4 or e[4] < actualY then break end
- else
- break
- end
- end
- end
- elseif mode == 2 then
- if x > w-7 then
- actualX = w-7
- end
- if y > w-3 then
- actualY = w-3
- end
- rightClickMenu(actualX, actualY, 2)
- while true do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" and e[2] == 1 then
- if e[3] == w and e[4] == 1 then
- closeApp()
- break
- elseif e[3] >= actualX and e[3] <= actualX+8 then
- if e[4] == actualY then -- cut
- animSelected(" Cut ",actualX,actualY,colors.lightGray,colors.black)
- CUTPATH = shell.resolve(PATH..oldItems[item])
- COPYPATH = ""
- break
- elseif e[4] == actualY+1 then -- copy
- animSelected(" Copy ",actualX,actualY+1,colors.lightGray,colors.black)
- COPYPATH = shell.resolve(PATH..oldItems[item])
- CUTPATH = ""
- break
- elseif e[4] == actualY+2 then -- delete
- animSelected(" Delete ",actualX,actualY+2,colors.lightGray,colors.black)
- fs.delete(PATH..oldItems[item])
- selectedItem = -1
- break
- elseif e[4] == actualY+3 then -- rename
- animSelected(" Rename ",actualX,actualY+3,colors.lightGray,colors.black)
- local newname = PATH..prompt("Rename",oldItems[item])
- if not newname == oldItems[item] and not fs.exists(newname) then fs.move(oldItems[item],newname) end
- break
- elseif e[4] > actualY+3 or e[4] < actualY then break end
- else
- break
- end
- end
- end
- else
- if x > w-11 then
- actualX = w-11
- end
- if y > h-2 then
- actualY = h-2
- end
- rightClickMenu(actualX, actualY, 3)
- while true do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" and e[2] == 1 then
- if e[3] == w and e[4] == 1 then
- closeApp()
- break
- elseif e[3] >= actualX and e[3] <= actualX+12 then
- if e[4] == actualY then -- paste
- animSelected(" Paste ",actualX,actualY,colors.lightGray,colors.black)
- if COPYPATH ~= "" then
- if not fs.exists(PATH..fs.getName(COPYPATH)) and fs.exists(COPYPATH) then fs.copy(COPYPATH, PATH..fs.getName(COPYPATH))
- else
- WALL = "Error"
- COPYPATH = ""
- end
- elseif CUTPATH ~= "" then
- if not fs.exists(PATH..fs.getName(CUTPATH)) and fs.exists(CUTPATH) then
- fs.move(CUTPATH, PATH..fs.getName(CUTPATH))
- CUTPATH = ""
- else
- WALL = "Error"
- CUTPATH = ""
- end
- else
- WALL = "Cut or Copy something first"
- end
- break
- elseif e[4] == actualY+1 then -- new file
- animSelected(" New File ",actualX,actualY+1,colors.lightGray,colors.black)
- local newfile = PATH..prompt("New File")
- if not fs.exists(newfile) then
- local f = fs.open(newfile, "w")
- f.write("")
- f.close()
- else
- WALL = "Item exists"
- end
- break
- elseif e[4] == actualY+2 then -- new folder
- animSelected(" New Folder ",actualX,actualY+2,colors.lightGray,colors.black)
- local newfolder = PATH..prompt("New Folder")
- if not fs.exists(newfolder) then
- fs.makeDir(PATH..newfolder)
- else
- WALL = "Item exists"
- end
- break
- elseif e[4] > actualY+2 or e[4] < actualY then break end
- else
- break
- end
- end
- end
- end
- end
- local function evtHandler()
- drawScreen()
- while RUNNING do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" and e[2] == 1 and e[3] == w and e[4] == 1 then
- closeApp()
- elseif e[1] == "key" and e[2] == keys.q then
- closeApp()
- elseif e[1] == "mouse_scroll" then
- if e[2] == 1 and scrollpos < pospossible then
- scrollpos = scrollpos+1
- elseif e[2] == -1 and scrollpos > 1 then
- scrollpos = scrollpos-1
- end
- --logVars()
- drawItems()
- elseif e[1] == "mouse_click" and e[2] == 1 and e[3] >= 1 and e[3] <= 3 and e[4] == 2 then
- animSelected(" < ",1,2,colors.gray,colors.lightGray)
- if PATH ~= "/" then
- for i = PATH:len()-1,1,-1 do
- if string.sub(PATH,i,i) == "/" then
- PATH = string.sub(PATH,1,i)
- break
- end
- end
- selectedItem = -1
- scrollpos = 1
- drawScreen()
- else
- printPos(" < ",1,2,colors.lightGray,colors.white)
- end
- elseif e[1] == "mouse_click" then
- if e[4] >= 3 then
- pos = (e[4]-3)+scrollpos
- if pos <= #oldItems then
- if e[3] >= 5 and e[3] <= oldItems[pos]:len()+4 then
- if pos == selectedItem then
- if pos < beginFiles and e[2] == 1 then
- PATH = PATH..oldItems[pos].."/"
- selectedItem = -1
- scrollpos = 1
- drawScreen()
- elseif pos >= beginFiles and e[2] == 1 then
- run(PATH..oldItems[pos])
- else
- if pos < beginFiles and e[2] == 2 then -- folder mode
- rightClickHandler(e[3],e[4],2,pos)
- drawScreen()
- elseif pos >= beginFiles and e[2] == 2 then -- file mode
- rightClickHandler(e[3],e[4],1,pos)
- drawScreen()
- end
- end
- else
- selectedItem = pos
- drawItems()
- if pos >= beginFiles and e[2] == 2 then -- file mode
- rightClickHandler(e[3],e[4],1,pos)
- drawScreen()
- elseif pos < beginFiles and e[2] == 2 then -- folder mode
- rightClickHandler(e[3],e[4],2,pos)
- drawScreen()
- end
- end
- else
- selectedItem = -1
- drawItems()
- if e[2] == 2 then
- rightClickHandler(e[3],e[4],3,pos)
- drawScreen()
- end
- end
- else
- selectedItem = -1
- drawItems()
- if e[2] == 2 then
- rightClickHandler(e[3],e[4],3,pos)
- drawScreen()
- end
- end
- end
- elseif e[1] == "term_resize" then
- w,h = term.getSize()
- drawScreen()
- end
- end
- end
- evtHandler()
- clear(colors.black)
- coroutine.yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement