From ba6019ad3b557d356f43bba1e8f88e66e6e319b2 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 11 Apr 2026 09:29:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Enhance=20tree.ts=20script=20wit?= =?UTF-8?q?h=20configurable=20options=20and=20update=20metadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-meta-v2.json | 6 +++--- single-scripts/tree.ts | 44 +++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/script-meta-v2.json b/script-meta-v2.json index 03b9165..82c63b6 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -322,12 +322,12 @@ }, "tree.ts": { "script_name": "tree.ts", - "script_length": 2839, - "script_sha256": "0dacf96ab36a38f4d82ec357e5dde466f0dffd734fd843057aa0fd51af532c93", + "script_length": 3356, + "script_sha256": "3ff71e47f3fde268f470a37b750a8c84842e46c4d4a23c1777786da0ff6e53d8", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/tree.ts", "single_script_file": true, "publish_time": 1769361581018, - "update_time": 1775868903980 + "update_time": 1775870977279 }, "trim.ts": { "script_name": "trim.ts", diff --git a/single-scripts/tree.ts b/single-scripts/tree.ts index f1968ae..49fc266 100755 --- a/single-scripts/tree.ts +++ b/single-scripts/tree.ts @@ -19,10 +19,15 @@ const defaultSkipDirs = [ "node_modules", ]; +interface ListDirOptions { + maxDepth: number; + hideMoreDirs: boolean; +} + async function listDir( dir: string, depth: number, - maxDepth: number, + options: ListDirOptions, ): Promise { const tab = " ".repeat(depth * 4); try { @@ -32,14 +37,20 @@ async function listDir( } const fullName = joinPath(dir, dirEntry.name); if (dirEntry.isDirectory) { - const showNextDepth = (depth + 1) <= maxDepth; - console.log( - `${tab}- [${dirEntry.name}]${ - showNextDepth ? "" : term.auto("[blue][[[ \t[...more dirs...]]]][/]") - }`, - ); + const showNextDepth = (depth + 1) <= options.maxDepth; + if (!options.hideMoreDirs) { + console.log( + `${tab}- [${dirEntry.name}]${ + showNextDepth + ? "" + : term.auto( + "[blue][[[ \t[...more dirs...]]]][/]", + ) + }`, + ); + } if (showNextDepth) { - await listDir(fullName, depth + 1, maxDepth); + await listDir(fullName, depth + 1, options.maxDepth); } } else { let fileDesc = ""; @@ -58,7 +69,9 @@ async function listDir( } } console.log( - `${tab}- ${term.auto("[green][[["+dirEntry.name+"]]][/]")} \t${fileDesc}`, + `${tab}- ${ + term.auto("[green][[[" + dirEntry.name + "]]][/]") + } \t${fileDesc}`, ); } } @@ -69,7 +82,7 @@ async function listDir( async function main(): Promise { const flags = parseArgs(Deno.args, { - boolean: ["help"], + boolean: ["help", "hide-more-dirs"], number: ["depth"], }); if (flags.help) { @@ -77,18 +90,23 @@ async function main(): Promise { tree.ts [parameters] ---depth 3 - Directory depth - - default '.' current dir`); +--depth 3 - Directory depth +--hide-more-dirs - Hide more dirs + - default '.' current dir`); return; } const maxDepth = parseIntVal(flags.depth, 10); + const hideMoreDirs = flags["hide-more-dirs"]; let baseDir = "."; if (flags._.length > 0) { baseDir = flags._[0]; } - await listDir(baseDir, 0, maxDepth); + await listDir(baseDir, 0, { + maxDepth, + hideMoreDirs, + }); } main().catch((e) => log.error(e));