diff --git a/examples/tour.luau b/examples/tour.luau index 0914fae..0a6928d 100644 --- a/examples/tour.luau +++ b/examples/tour.luau @@ -1,27 +1,98 @@ local fs = require("@lune/fs") local zip = require("../lib") +local asciitable = require("../luau_packages/asciitable") local file = fs.readFile("tests/data/files_and_dirs.zip") local reader = zip.load(buffer.fromstring(file)) -print("Directory structure:") +--- Transforms a tree of recursive `{ [string]: EntryData }` to a recursive tree of +--- `{ [string]: string }` +local function formatTree(tree: Tree): Tree + local result: Tree = {} + for key, value in tree do + if typeof(value) == "table" and not value.size then + -- Directory, recurse + result[key] = formatTree(value :: Tree) + continue + end + + -- File, format the value + local fileEntry = value :: EntryData + local content = reader:extract(assert(reader:findEntry(fileEntry.path)), { isString = true }) :: string + local truncContents = content:gsub("\n", ""):sub(1, 100) + + result[key] = string.format( + "(%d bytes), attrs: 0x%x, content: %s", + fileEntry.size, + fileEntry.attributes, + truncContents .. (if #content > 100 then "..." else "") + ) + end + + return result +end + +local tree: Tree = {} +type Tree = { [string]: T | Tree } +type EntryData = { + path: string, + size: number, + attributes: number, + content: string, +} + +-- Create a tree of entries by recursively walking the ZIP reader:walk(function(entry, depth) - local prefix = string.rep(" ", depth) - local suffix = if not entry.isDirectory - then string.format(" (%d bytes), attrs: 0x%x, content: %s", entry.size, entry.attributes, reader:extract(entry, { isString = true }) :: string) - else "" - print(prefix .. entry:getPath() .. suffix) + local current = tree + local parts = string.split(entry:getPath(), "/") + + if #parts == 1 and parts[1] == "" then + -- Skip root directory + return + end + + -- Build the directory tree + for i = 1, #parts - 1 do + local part = parts[i] + if part ~= "" then + if not current[part] then + current[part] = {} + end + current = current[part] :: { [string]: EntryData } + end + end + + -- Add the file or directory entry + local name = parts[#parts] + if name ~= "" then + if entry.isDirectory then + current[name] = {} + else + current[name] = { + path = entry:getPath(), + size = entry.size, + attributes = entry.attributes, + content = reader:extract(entry, { isString = true }) :: string, + } + end + end end) -print("\Children of `/`:") -local assets = reader:listDirectory("/") -for _, entry in assets do - print(` {entry.name} - {if entry.isDirectory then "DIR" else "FILE"} ({entry.method})`) +-- Format the tree and print it +local formattedTree = formatTree({ ["/"] = tree }) +print(asciitable.tree("Directory structure:", formattedTree)) + +-- List the children of the root directory +local children = reader:listDirectory("/") +local formattedChildren: { [string]: string } = {} +for _, entry in children do + formattedChildren[entry.name] = `{if entry.isDirectory then "DIR" else "FILE"} ({entry.method})` end +print(asciitable.tree("\nChildren of `/`:", formattedChildren)) -- Get archive statistics local stats = reader:getStats() print("\nArchive stats:") print("Files:", stats.fileCount) print("Directories:", stats.dirCount) -print("Total size:", stats.totalSize, "bytes") \ No newline at end of file +print("Total size:", stats.totalSize, "bytes") diff --git a/pesde.lock b/pesde.lock index 572bec8..f8c23a5 100644 --- a/pesde.lock +++ b/pesde.lock @@ -1,5 +1,5 @@ name = "0x5eal/unzip" -version = "0.0.1-rc.1" +version = "0.0.1-rc.2" target = "luau" [graph."itsfrank/frktest"."0.0.2 lune"] @@ -60,6 +60,24 @@ index_url = "https://github.com/daimond113/pesde-index" environment = "lune" lib = "init.luau" +[graph."kimpure/asciitable"."0.1.4 luau"] +direct = ["asciitable", { name = "kimpure/asciitable", version = "^0.1.4" }, "dev"] +resolved_ty = "dev" + +[graph."kimpure/asciitable"."0.1.4 luau".target] +environment = "luau" +lib = "src/init.luau" + +[graph."kimpure/asciitable"."0.1.4 luau".pkg_ref] +ref_ty = "pesde" +name = "kimpure/asciitable" +version = "0.1.4" +index_url = "https://github.com/pesde-pkg/index" + +[graph."kimpure/asciitable"."0.1.4 luau".pkg_ref.target] +environment = "luau" +lib = "src/init.luau" + [graph."lukadev_0/option"."1.2.0 lune"] resolved_ty = "standard" diff --git a/pesde.toml b/pesde.toml index 0c9e5ea..27eb538 100644 --- a/pesde.toml +++ b/pesde.toml @@ -22,3 +22,4 @@ default = "https://github.com/pesde-pkg/index" [dev_dependencies] stylua = { name = "pesde/stylua", version = "^2.0.2", target = "lune" } frktest = { name = "itsfrank/frktest", version = "^0.0.2", target = "lune" } +asciitable = { name = "kimpure/asciitable", version = "^0.1.4" }