79 lines
1.6 KiB
Lua
79 lines
1.6 KiB
Lua
local fs = require("filesystem")
|
|
|
|
local FileTree = {}
|
|
function FileTree:new(iterator)
|
|
local file_tree = {
|
|
iterator = iterator,
|
|
filters = {}
|
|
}
|
|
setmetatable(file_tree, self)
|
|
self.__index = self
|
|
return file_tree
|
|
end
|
|
|
|
function directory(path_or_dir)
|
|
if type(path_or_dir) == "string" then
|
|
return FileTree:new(fs.directory_source(path_or_dir))
|
|
end
|
|
if path_or_dir.path ~= nil then
|
|
return FileTree:new(fs.directory_source(path_or_dir.path))
|
|
end
|
|
error("Invalid type for directory")
|
|
end
|
|
|
|
function FileTree:filter(filter_fn)
|
|
table.insert(self.filters, filter_fn)
|
|
return self
|
|
end
|
|
|
|
function FileTree:__call()
|
|
local next = self.iterator()
|
|
if next == nil then
|
|
return nil
|
|
end
|
|
|
|
for _,filter in pairs(self.filters) do
|
|
if (not filter(next)) then
|
|
return self()
|
|
end
|
|
end
|
|
return next
|
|
end
|
|
|
|
function is_dir(entry)
|
|
return entry:is_dir()
|
|
end
|
|
|
|
function is_file(entry)
|
|
return entry:is_dir()
|
|
end
|
|
|
|
function starts_with(prefix)
|
|
return function(entry)
|
|
return entry.path:find("^"..prefix)
|
|
end
|
|
end
|
|
|
|
function contains(infix)
|
|
return function(entry)
|
|
return entry.path:find(infix)
|
|
end
|
|
end
|
|
|
|
function does_not(predicate)
|
|
return function(...) return not predicate(...) end
|
|
end
|
|
|
|
function zip(source)
|
|
print("would zip")
|
|
for entry in source do
|
|
print(entry.path)
|
|
end
|
|
end
|
|
|
|
local comic_dir = directory("/Users/kinch/Desktop/Rebuild World")
|
|
comic_dir:filter(is_dir)
|
|
|
|
for chapter_dir in comic_dir do
|
|
zip(directory(chapter_dir):filter(does_not(contains("._"))))
|
|
end |