dirtree.lua (745B)
1 -- @module dirtree 2 local dirtree = {} 3 -- 4 local lfs = require 'lfs' -- luafilesystem 5 6 function dirtree.get(dir) 7 assert(dir and dir ~= '', 'directory parameter is missing or empty') 8 9 -- Removes slash if is one 10 if string.sub(dir, -1) == '/' then 11 dir = string.sub(dir, 1, -2) 12 end 13 14 -- Main function of the coroutine (recursive) 15 local function yieldtree(dir) 16 for entry in lfs.dir(dir) do 17 if entry ~= '.' and entry ~= '..' then 18 entry = dir..'/'..entry 19 20 local attr = lfs.attributes(entry) 21 22 coroutine.yield(entry,dir,attr) 23 24 if attr.mode == 'directory' then 25 yieldtree(entry) 26 end 27 end 28 end 29 end 30 31 return coroutine.wrap(function() yieldtree(dir) end) 32 end 33 34 return dirtree