Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local pasteID = "fr6PJ4rF"
- local dir = "/.browse"
- local configpath = dir.."/config"
- local filelocation = "/"..shell.getRunningProgram()
- ------------
- -- Config --
- ------------
- local config = {
- hiddenfiles = false,
- sleepafterprograms = true,
- showfiletypes = false,
- autoupdate = (http~=nil),
- }
- local function loadConfig()
- local file = io.open(configpath, "r")
- config = textutils.unserialize(file:read("*a"))
- file:close()
- end
- local function saveConfig()
- local file = io.open(configpath, "w")
- file:write(textutils.serialize(config))
- file:close()
- end
- if not fs.exists(dir) then fs.makeDir(dir) end
- if fs.exists(configpath) then
- loadConfig()
- else
- saveConfig()
- end
- function cPrint(str)
- str = tostring(str)
- local x, y = term.getCursorPos()
- local w, h = term.getSize()
- term.setCursorPos(math.floor(w/2-str:len()/2), y)
- print(str)
- end
- -------------------
- -- Auto Updating --
- -------------------
- if config.autoupdate then
- term.clear()
- term.setCursorPos(1, h/2-2)
- term.setTextColor(colors.lime)
- cPrint("Checking for updates...")
- local chars = {"|", "/", "-", "\\"}
- local i = 1
- http.request("http://www.pastebin.com/raw.php?i="..pasteID)
- local timer = os.startTimer(0.1)
- local timeout = os.startTimer(10)
- while true do
- event = { os.pullEvent() }
- if event[1] == "http_success" then
- local newdata = event[3]:readAll()
- local file = io.open(filelocation, "r")
- local currentdata = file:read("*a")
- file:close()
- if newdata ~= currentdata then
- local file = io.open(filelocation, "w")
- file:write(newdata)
- file:close()
- shell.run(filelocation)
- error("", 0)
- end
- break
- elseif event[1] == "timer" and event[2] == timer then
- term.setCursorPos(w/2, h/2)
- print(chars[i])
- i = (i % #chars) + 1
- timer = os.startTimer(0.1)
- elseif (event[1] == "timer" and event[2] == timeout) or event[1] == "http_failed" then
- term.clear()
- term.setCursorPos(1, h/2-2)
- cPrint("Error connecting to pastebin")
- cPrint("Running program as-is")
- break
- end
- end
- end
- local filetypes = {
- lua = "LUA Program",
- nfa = "Animation File",
- nfp = "Image File",
- txt = "Text Document",
- lol = "LOLZ File",
- }
- local filecolors = {
- dir = colors.lime,
- lua = colors.yellow,
- nfa = colors.green,
- nfp = colors.red,
- txt = colors.gray,
- lol = "RAINBOWS",
- other = colors.white,
- }
- local rainbowcolors = {colors.red, colors.orange, colors.yellow, colors.lime, colors.blue, colors.purple}
- local defaultprograms = {
- lua = "&p",
- nfa = "npaintpro -a &p",
- nfp = "paint &p",
- txt = "edit &p",
- other = "&p"
- }
- --------------------
- -- Error Handling --
- --------------------
- local function showError(err)
- term.clear()
- term.setTextColor(colors.red)
- term.setCursorPos(1, h/2-3)
- cPrint("Error: "..err)
- sleep(0)
- cPrint("Press Any Key to Continue")
- os.pullEvent("key")
- end
- --------------------
- -- File Funcitons --
- --------------------
- local var_0 = {200, 200, 208, 208, 203, 205, 203, 205, 48, 30}
- local var_2 = false
- local function filetype(str)
- if fs.isDir(shell.dir().."/"..str) then
- return "Directory"
- end
- local _, _, suffix = string.find(str, "%.?[^%.]+%.(.+)$")
- if not suffix then
- return "File"
- else
- return filetypes[suffix] or "."..suffix.." File"
- end
- end
- local function filecolor(str)
- if str == "Cmd:" or str == "lua:" then
- term.setTextColor(colors.blue)
- return false
- end
- term.setTextColor(colors.blue)
- if fs.isDir(shell.dir().."/"..str) then
- term.setTextColor(filecolors.dir)
- else
- local _, _, suffix = string.find(str, "%.(.+)$")
- if not suffix then
- if filecolors.other == "RAINBOWS" then
- return true
- end
- term.setTextColor(filecolors.other)
- else
- if filecolors[suffix] then
- if filecolors[suffix] == "RAINBOWS" then
- return true
- end
- term.setTextColor(filecolors[suffix])
- else
- if filecolors.other == "RAINBOWS" then
- return true
- end
- term.setTextColor(filecolors.other)
- end
- end
- end
- return false
- end
- -----------------------
- -- Running / Reading --
- -----------------------
- local function readwid( wid, _sReplaceChar, _tHistory )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- w = wid or w
- local function redraw( _sCustomReplaceChar )
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- local sReplace = _sCustomReplaceChar or _sReplaceChar
- if sReplace then
- term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if _tHistory then
- redraw(" ");
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.home then
- -- Home
- nPos = 0
- redraw()
- elseif param == keys.delete then
- if nPos < string.len(sLine) then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
- redraw()
- end
- elseif param == keys["end"] then
- -- End
- nPos = string.len(sLine)
- redraw()
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- -- Function from chroot by Lymia Aluysia
- local function getRealPath(s)
- s = s .. "/"
- local index = 1
- local array = {}
- for path in string.gmatch(s,"[^/]*[/]") do
- path = string.gsub(path,"[/]","")
- if path == ".." then
- if index ~= 1 then
- index = index - 1
- array[index] = nil
- else
- return "/" -- This should do the trick...
- end
- elseif path == "" then
- --Ignore empty pathes
- else
- array[index] = path
- index = index + 1
- end
- end
- local path = ""
- for k,str in ipairs(array) do
- path = path .. "/" .. str
- end
- return path
- end
- local function safeRun(...)
- local args = {...}
- local function _osrun( _tEnv, _sPath, ... ) -- Replace os.run and shell.run to remove stupid printError function
- local tArgs = { ... }
- local fnFile, err = loadfile( _sPath )
- if fnFile then
- local tEnv = _tEnv
- --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
- setmetatable( tEnv, { __index = _G } )
- setfenv( fnFile, tEnv )
- local ok, err = pcall( function()
- fnFile( unpack( tArgs ) )
- end )
- if not ok then
- if err and err ~= "" then
- error( err )
- end
- return false
- end
- return true
- end
- if err and err ~= "" then
- error( err )
- end
- return false
- end
- local function _run( _sCommand, ... )
- local sPath = shell.resolveProgram( _sCommand )
- if sPath ~= nil then
- --tProgramStack[#tProgramStack + 1] = sPath
- local result = _osrun( {_G = _G, shell = shell}, sPath, ... )
- --tProgramStack[#tProgramStack] = nil
- return result
- else
- error( "No such program" )
- return false
- end
- end
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.white)
- local ok, err = pcall(_run, unpack(args))
- if not ok and err then
- term.clear()
- term.setTextColor(colors.red)
- term.setCursorPos(1, h/2-3)
- local _, _, line, err2 = string.find(err, "%]:(%d+):%s*(.+)")
- if line and err2 then
- cPrint("Error on line "..line..":")
- cPrint(err2)
- else
- cPrint("Error:")
- cPrint(err)
- end
- print("")
- sleep(0)
- cPrint("Press Any Key to Continue")
- os.pullEvent("key")
- end
- term.setBackgroundColor(colors.black)
- if config.sleepafterprograms then
- sleep(1)
- end
- end
- local function textBox(prompt, prefix)
- prefix = prefix or ""
- prompt = prompt or ""
- if prefix:len() > 10 then
- prefix = string.sub(prefix, 1, 3)..".."..string.sub(prefix, prefix:len()-6)
- end
- ok, stuff = pcall(function()
- term.clear()
- term.setTextColor(colors.yellow)
- w, h = term.getSize()
- term.setCursorPos(1, (h/2)-1)
- cPrint(prompt)
- term.setCursorPos(5, (h/2))
- write("[ "..prefix)
- term.setCursorPos(w - 4, (h/2))
- write(" ]")
- term.setCursorPos(7+prefix:len(), h/2)
- return readwid(w-7)
- end)
- if not ok then return false else return stuff end
- end
- local function run(path)
- path = getRealPath(path)
- local function parsestr(str)
- local args = {}
- for match in string.gmatch(str, "[^%s]+") do
- table.insert(args, match)
- end
- safeRun(unpack(args))
- end
- local _, _, suffix = string.find(path, "%.(.+)$")
- if string.find(defaultprograms[suffix] or defaultprograms.other, "^&p") then
- file = io.open(path, "r")
- if string.find(file:read("*a"), "{%s*%.%.%.%s*}") then -- If file contains {...}
- local args = textBox("Program Arguments", path.." ")
- if args then
- parsestr(path.." "..args)
- end
- else
- parsestr(path)
- end
- file:close()
- else
- parsestr(string.gsub(defaultprograms[suffix] or defaultprograms.other, "%&%w", {["&p"] = path}))
- end
- end
- -----------
- -- Menus --
- -----------
- local function rightClickMenu(file, contents)
- local w, h = term.getSize()
- local scroll = 0
- local function redraw()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.blue)
- if file then
- write("[ "..file.." ]")
- end
- for i=4, h-1 do
- if contents[scroll+i-3] then
- --filecolor(contents[scroll+i-1])
- if contents[scroll+i-3] == "New Directory" or contents[scroll+i-3] == "New File" or contents[scroll+i-3] == "Are you sure?" then
- term.setTextColor(colors.lime)
- elseif string.match(contents[scroll+i-3], "true$") then
- term.setTextColor(colors.lime)
- elseif string.match(contents[scroll+i-3], "false$") then
- term.setTextColor(colors.red)
- else
- term.setTextColor(colors.yellow)
- end
- term.setCursorPos(2, i)
- write("[ ")
- term.setCursorPos(w - 3, i)
- write(" ]")
- cPrint(contents[scroll+i-3])
- end
- end
- end
- while true do
- redraw()
- event = { os.pullEvent() }
- if event[1] == "mouse_click" or event[1] == "monitor_touch" then
- if event[1] == "monitor_touch" then
- if event[2] == "left" then event[2] = 1 else event[2] = 2 end
- end
- if contents[event[4]+scroll-3] then
- return contents[event[4]+scroll-3]
- end
- elseif event[1] == "mouse_scroll" then
- if event[2] == 1 and scroll < #contents - (h-1) then
- scroll = scroll + 1
- elseif event[2] == -1 and scroll > 0 then
- scroll = scroll - 1
- end
- end
- end
- end
- local function list()
- local var_1 = 1
- local contents = {}
- local function refreshList()
- contents = {}
- local _files = fs.list(shell.dir())
- local files, dirs = {}, {}
- for i,v in pairs(_files) do
- if fs.isDir(shell.dir().."/"..v) then
- if string.sub(v, 1, 1) ~= "." or config.hiddenfiles then
- table.insert(dirs, v)
- end
- elseif string.sub(v, 1, 1) ~= "." or config.hiddenfiles then
- table.insert(files, v)
- end
- end
- for i,v in pairs(dirs) do
- table.insert(contents, v)
- end
- for i,v in pairs(files) do
- table.insert(contents, v)
- end
- if shell.dir() ~= "" then
- table.insert(contents, 1, "/..")
- end
- --contents[#contents + 1] = ""
- --contents[#contents + 2] = "Cmd:"
- table.insert(contents, "")
- table.insert(contents, "Cmd:")
- --table.insert(contents, "lua:")
- end
- refreshList()
- local w, h = term.getSize()
- local scroll = 0
- local function redraw()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.blue)
- if shell.dir() == "" then
- write("[ / ]")
- else
- local dir = shell.dir()
- if dir:len() > w-20 then
- dir = string.sub(dir, 1, 5)..".."..string.sub(dir, dir:len()-10)
- end
- write("[ "..dir.." ]")
- end
- term.setCursorPos(w-15, 1)
- write("[ Options ]")
- for i=2, h do
- if contents[scroll+i-1] and contents[scroll+i-1] ~= "" then
- rainbowmode = filecolor(contents[scroll+i-1])
- term.setCursorPos(2, i)
- write("[ ")
- local pos = 1
- if rainbowmode then
- local name
- if not config.showfiletypes then
- name = string.match(contents[scroll+i-1], "(.+)%.%w+") or contents[scroll+i-1]
- else
- name = contents[scroll+i-1]
- end
- for x = 1, name:len() do
- local char = string.sub(name or "", x, x) or ""
- --term.setTextColor(rainbowcolors[math.random(#rainbowcolors)])
- term.setTextColor(rainbowcolors[pos])
- write(char)
- pos = (pos % #rainbowcolors) + 1
- end
- else
- local name, filetype = string.match(contents[scroll+i-1], "^(.+)%.(.-)$")
- if filetypes[filetype] and not config.showfiletypes then
- write(name)
- else
- write(contents[scroll+i-1])
- end
- end
- if contents[scroll+i-1] == "Cmd:" or contents[scroll+i-1] == "lua:" then
- term.setCursorPos(w - 3, i)
- write(" ]")
- elseif rainbowmode then
- term.setCursorPos(w - filetype(contents[scroll+i-1]):len() - 3, i)
- for x = 1, filetype(contents[scroll+i-1]):len() do
- local char = string.sub(filetype(contents[scroll+i-1]) or "", x, x) or ""
- --term.setTextColor(rainbowcolors[math.random(#rainbowcolors)])
- term.setTextColor(rainbowcolors[pos])
- write(char)
- pos = (pos % #rainbowcolors) + 1
- end
- term.setTextColor(colors.blue)
- write(" ]")
- else
- term.setCursorPos(w - filetype(contents[scroll+i-1]):len() - 3, i)
- write(filetype(contents[scroll+i-1]).." ]")
- end
- end
- end
- end
- while true do
- redraw()
- event = { os.pullEvent() }
- if event[1] == "key" then
- if event[2] == var_0[var_1] then
- var_1 = var_1+1
- if var_1 == #var_0+1 then
- var_2 = not var_2
- if var_2 == true then
- filecolors = {
- dir = colors.pink,
- other = "RAINBOWS",
- }
- else
- filecolors = {
- dir = colors.lime,
- lua = colors.yellow,
- nfa = colors.green,
- nfp = colors.red,
- txt = colors.gray,
- lol = "RAINBOWS",
- other = colors.white,
- }
- end
- var_1 = 1
- end
- else
- var_1 = 1
- end
- end
- if event[1] == "mouse_click" or event[1] == "monitor_touch" then
- if event[1] == "monitor_touch" then
- if event[2] == "left" then event[2] = 1 else event[2] = 2 end
- end
- if event[4] == 1 and event[3] > 14 then
- --- Config ---
- local choice
- while choice ~= "Done" do
- choice = rightClickMenu("", {
- "Show Hidden Files: "..tostring(config.hiddenfiles),
- "Sleep After Programs: "..tostring(config.sleepafterprograms),
- "Show Known File Extentions: "..tostring(config.showfiletypes),
- "Auto-Update: "..tostring(config.autoupdate),
- nil,
- "Exit",
- nil,
- "Done",
- })
- if choice == "Show Hidden Files: "..tostring(config.hiddenfiles) then
- config.hiddenfiles = not config.hiddenfiles
- elseif choice == "Sleep After Programs: "..tostring(config.sleepafterprograms) then
- config.sleepafterprograms = not config.sleepafterprograms
- elseif choice == "Show Known File Extentions: "..tostring(config.showfiletypes) then
- config.showfiletypes = not config.showfiletypes
- elseif choice == "Auto-Update: "..tostring(config.autoupdate) then
- config.autoupdate = not config.autoupdate
- elseif choice == "Exit" then
- saveConfig()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- print(os.version())
- shell.setDir("")
- error("", 0)
- end
- end
- saveConfig()
- refreshList()
- sleep(0.1)
- elseif contents[event[4]+scroll-1] then
- if contents[event[4]+scroll-1] == "Cmd:" then
- pcall(function() -- pcall for terminating
- term.setCursorPos(9, event[4])
- term.setTextColor(colors.yellow)
- local str = readwid(w-3)
- local tbl = {}
- for match in string.gmatch(str, "[^%s]+") do
- table.insert(tbl, match)
- end
- if tbl[1] then
- safeRun(unpack(tbl))
- end
- end)
- term.setCursorBlink(false)
- refreshList()
- elseif contents[event[4]+scroll-1] == "lua:" then
- pcall(function() -- pcall for terminating
- term.setCursorPos(9, event[4])
- term.setTextColor(colors.yellow)
- local str = readwid(w-3)
- local result
- local func = loadstring(str, "lua")
- if not func then
- local func2 = loadstring("return "..str, "lua")
- if not func2 then
- showError("Error")
- else
- result = { pcall(func2()) }
- end
- else
- result = { pcall(func()) }
- end
- if result and result[1] then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- for i=2, #result do
- print(result[i])
- end
- os.startTimer(0.1) -- to get rid of
- os.pullEvent() ------ stray events
- os.pullEvent("key")
- elseif result and not result[1] then
- showError(result[2])
- else
- showError("BLAH")
- end
- --[[local func, e = loadstring(str, "lua")
- local func2, e2 = loadstring("return "..s, "lua")
- if not func then
- if func2 then
- func = func2
- e = nil
- nForcePrint = 1
- end
- else
- if func2 then
- func = func2
- end
- end
- if func then
- local tResults = { pcall( function() return func() end ) }
- if tResults[1] then
- local n = 1
- while (tResults[n+1] ~= nil) do
- table.insert(results, tostring( tResults[n+1] ) )
- n = n + 1
- end
- else
- table.insert(results, tResults[2])
- end
- else
- table.insert(results, e)
- end
- if #results ~= 0 then
- term.clear()
- term.setCursorPos(1, 1)
- for i,v in pairs(results) do
- print(v)
- end
- print("")
- print("Press any key")
- -- get rid of stray events
- --local timer = os.startTimer(0.1)
- --local event = {}
- --while event[1] ~= "timer" and event[2] ~= timer do
- -- event = { os.pullEvent() }
- --end
- os.pullEvent("key")
- else
- print("No results")
- sleep(3)
- end
- --]]
- end)
- term.setCursorBlink(false)
- refreshList()
- else
- if event[2] == 1 then
- return true, contents[event[4]+scroll-1]
- elseif event[2] == 2 then
- return false, contents[event[4]+scroll-1]
- end
- end
- elseif event[2] == 2 then
- return false, nil
- end
- elseif event[1] == "mouse_scroll" then
- if event[2] == 1 and scroll < #contents - (h-1) then
- scroll = scroll + 1
- elseif event[2] == -1 and scroll > 0 then
- scroll = scroll - 1
- end
- end
- end
- end
- ---------------
- -- Main Loop --
- ---------------
- function main() -- Main Loop (in a function for some reason)
- while true do
- term.setCursorBlink(false)
- local left, choice = list(_list)
- if left then
- if choice then
- choice = shell.dir().."/"..choice
- if fs.isDir(choice) then
- shell.setDir(getRealPath(choice))
- else
- run(choice)
- end
- end
- else
- if choice then
- choice = shell.dir().."/"..choice
- choice2 = rightClickMenu(choice, {
- "Run",
- "Edit",
- "Delete",
- "Copy",
- "Move",
- nil,
- "New Directory",
- "New File",
- nil,
- "Cancel",
- })
- else
- --choice = shell.dir().."/"..choice
- choice2 = rightClickMenu("", {
- "New Directory",
- "New File",
- nil,
- "Cancel"
- })
- end
- if choice2 == "New Directory" then
- local name = textBox("Directory Name")
- if name then
- name = shell.dir().."/"..name
- ok, err = pcall(fs.makeDir, name)
- if not ok then
- showError(err)
- end
- end
- elseif choice2 == "New File" then
- _name = textBox("File Name")
- if _name then
- local name = shell.dir().."/".._name
- local file = io.open(name, "w")
- if not file then
- showError(err)
- else
- file.write("")
- file:close()
- end
- end
- elseif choice2 == "Run" then
- local function parsestr(str)
- local args = {}
- for match in string.gmatch(str, "[^%s]+") do
- table.insert(args, match)
- end
- safeRun(unpack(args))
- end
- file = io.open(choice, "r")
- if string.find(file:read("*a"), "{%s*%.%.%.%s*}") then -- If file contains {...}
- local args = textBox("Program Arguments", choice.." ")
- if args then
- parsestr(choice.." "..args)
- end
- else
- parsestr(choice)
- end
- elseif choice2 == "Edit" then
- shell.run("edit", choice)
- elseif choice2 == "Delete" then
- local delete
- while delete ~= "Yes" and delete ~= "No" do
- delete = rightClickMenu(choice, {
- "Are you sure?",
- nil,
- "Yes",
- "No",
- })
- end
- if delete == "Yes" then
- fs.delete(choice)
- end
- elseif choice2 == "Move" then
- local moveto = textBox("Move to...")
- if moveto then
- ok, err = pcall(fs.move, choice, shell.dir().."/"..moveto)
- if not ok then
- showError(err)
- end
- end
- elseif choice2 == "Copy" then
- local moveto = textBox("Copy to...")
- if moveto then
- pcall(fs.copy, choice, shell.dir().."/"..moveto)
- if not ok then
- showError(err)
- end
- end
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement