Last active
July 10, 2021 08:16
-
-
Save Miqueas/6b75b25731f9a785678951cfcef8c002 to your computer and use it in GitHub Desktop.
[Lua + Gio] Reads directory content recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lgi = require("lgi") | |
local Gio = lgi.require("Gio", "2.0") | |
local Content = {} | |
local Dir = "" | |
function ReadDir(Path, Tab, Indent) | |
local file = Gio.File.new_for_path(Path) | |
local enum = file:enumerate_children("standard::name", Gio.FileQueryInfoFlags.NONE) | |
local path = file:get_path() .. "/" | |
local indent = Indent or "" | |
if not enum then | |
error("Can't open path: ".. Path) | |
else | |
local info = enum:next_file() | |
local count = 1 | |
while info do | |
local name = info:get_name() | |
local file_type = info:get_file_type() | |
local full_path = path .. info:get_name() | |
if file_type == "DIRECTORY" then | |
Tab[count] = { | |
Type = file_type, | |
Basename = name, | |
Path = full_path, | |
Files = {} | |
} | |
ReadDir(full_path, Tab[count].Files, indent .. " > ") | |
else | |
Tab[count] = { | |
Type = file_type, | |
Basename = name, | |
Path = full_path | |
} | |
end | |
info = enum:next_file() | |
count = count + 1 | |
end | |
end | |
end | |
local function RPairs(T, I) | |
local i = I or "" | |
for k, v in pairs(T) do | |
print(("%s %s %s"):format(i, k, tostring(v))) | |
if type(v) == "table" then | |
RPairs(v, i .. "> ") | |
end | |
end | |
end | |
if #arg == 0 then | |
error("No arguments!") | |
elseif #arg == 1 then | |
Dir = arg[1] | |
Content = (#Content > 0) and {} or Content | |
ReadDir(Dir, Content) | |
RPairs(Content) | |
else | |
Content = (#Content > 0) and {} or Content | |
for i = 1, #arg do | |
Dir = arg[i] | |
Content[i] = { | |
Root = Dir, | |
Files = {} | |
} | |
ReadDir(Dir, Content[i].Files) | |
RPairs(Content) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment