Post by warspyking on Nov 14, 2015 20:53:41 GMT
SystemBrowser:
local Extensions = {
png = true,
jpg = true,
mp3 = true,
txt = true,
html = true,
js = true,
lua = true,
plist = true,
strings = true,
wav = true,
nib = true
}
function TypeOfFile(dir, name)
local results = {sys.dir(dir.."/"..name)}
if not results[1] then
if results[2] == "Operation not permitted" then
return "L Dir"
elseif results[2] == "Not a directory" then
local extension = (name:match("^.+%.(.+)$") or name:match("%.(.*)$"))
if Extensions[extension] then
return extension:upper()
else
return "File"
end
elseif results[2] == "No such file or directory" then
return false
else
return "File"
end
else
return "Dir"
end
end
function Line()
print("-------------------------------")
end
function Display(dpath)
print("Type", "ID", "Name")
Line()
for i,v in ipairs(sys.dir(dpath)) do
print(TypeOfFile(dpath, v), i, v)
end
Line()
if dpath ~= "/" then
print("Options:")
print("__OPTION", "back")
end
end
function WaitEnter()
Line()
print("Press return to continue...")
io.read()
end
function Contents(dir)
local results = {sys.dir(dir)}
if results[2] then
print(results[2])
else
local f = io.open(dir)
for line in f:lines() do
print(line)
end
f:close()
end
WaitEnter()
end
function Action(npath, opath, nname)
if nname == "back" then
opath[#opath] = nil
return Enter(table.concat(opath, "/"), opath)
else
local Type = TypeOfFile(npath, nname)
if tonumber(nname) then
return Action(npath, opath, sys.dir(npath)[tonumber(nname)])
elseif Type == "Dir" then
opath[#opath+1] = nname
return Enter(npath.."/"..nname, opath)
elseif Type == "PNG" or Type == "JPG" then
if draw then
if draw.image then
draw.setscreen(1)
draw.settitle("Please touch the screen to continue")
draw.image(npath.."/"..nname, 0, 49)
draw.waittouch()
draw.setscreen(0)
else
print("Drawing is not supported on your device!")
WaitEnter()
end
else
print("Please unlock the draw library!")
end
return Enter(npath, opath)
elseif Type == "MP3" then
if audio then
audio.playbg(npath.."/"..nname)
else
print("Please unlock the audio library!")
WaitEnter()
end
return Enter(npath, opath)
elseif Type == "TXT" or Type == "LUA" or Type == "JS" or Type == "HTML" then
Contents(npath.."/"..nname)
return Enter(npath, opath)
elseif Type == "L Dir" then
print("Directory is locked!")
WaitEnter()
return Enter(npath, opath)
elseif Type == "File" then
print("Unknown File Type")
Contents(npath.."/"..nname)
return Enter(npath, opath)
elseif Type ~= nil then
print("Unsupported File Type")
WaitEnter()
return Enter(npath, opath)
elseif Type == nil then
print("No file called "..nname.."!")
WaitEnter()
return Enter(npath, opath)
end
end
end
function Enter(npath, opath)
npath = npath or "/"
opath = opath or {"/"}
clear()
Display(npath)
print("")
sys.print(npath.."/")
local nname = io.read()
return Action(npath, opath, nname)
end
Enter()