diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index 3ae74d5..243bdde 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -105,6 +105,25 @@ export function isOn(val: string | undefined | null): boolean { lowerVal === "true"; } +export function isUndefined(val: any): boolean { + return typeof val === "undefined"; +} + +export function isUndefinedOrNull(val: any): boolean { + return isUndefined(val) || (val === null); +} + +export function parseIntVal(val: any, defaultVal: number): number { + if (isUndefinedOrNull(val)) { + return defaultVal; + } + const parsedVal = parseInt(val, 10); + if (isNaN(parsedVal)) { + return defaultVal; + } + return parsedVal; +} + export function getEnv(envKey: string): string | null { const homeDir = getHomeDir(); if ((homeDir !== null) && envKey) { diff --git a/single-scripts/tree.ts b/single-scripts/tree.ts index a503c6c..bef3a10 100755 --- a/single-scripts/tree.ts +++ b/single-scripts/tree.ts @@ -4,8 +4,10 @@ import { formatSize2, joinPath, log, + parseIntVal, term, -} from "https://global.hatter.ink/script/get/@16/deno-commons-mod.ts"; +} from "https://global.hatter.ink/script/get/@21/deno-commons-mod.ts"; +import {parseArgs} from "jsr:@std/cli/parse-args"; const defaultSkipDirs = [ ".git", @@ -30,10 +32,10 @@ async function listDir( } const fullName = joinPath(dir, dirEntry.name); if (dirEntry.isDirectory) { - const showNextDepth = depth <= maxDepth; + const showNextDepth = (depth + 1) <= maxDepth; console.log( `${tab}- [${dirEntry.name}]${ - showNextDepth ? "" : " ...more dirs..." + showNextDepth ? "" : term.blue(" \t[...more dirs...]") }`, ); if (showNextDepth) { @@ -46,7 +48,9 @@ async function listDir( } else if (dirEntry.isFile) { const fileInfo = await Deno.stat(fullName); if (fileInfo.size > 1024 * 1024) { - fileDesc = term.red(` - ${formatSize2(fileInfo.size)}`); + fileDesc = term.red( + ` - ${formatSize2(fileInfo.size)}`, + ); } else { fileDesc = term.yellow( ` - ${formatSize2(fileInfo.size)}`, @@ -54,7 +58,7 @@ async function listDir( } } console.log( - `${tab}- ${term.green(dirEntry.name)} ${fileDesc}`, + `${tab}- ${term.green(dirEntry.name)} \t${fileDesc}`, ); } } @@ -64,14 +68,30 @@ async function listDir( } async function main(): Promise { - let baseDir = "."; - if (Deno.args.length > 0) { - baseDir = Deno.args[0]; + const flags = parseArgs(Deno.args, { + boolean: ["help"], + number: ["depth"], + }); + if (flags.help) { + console.log(`Help massage for tree.ts + +tree.ts [parameters] + +--depth 3 - Directory depth + - default '.' current dir`); + return; } - await listDir(baseDir, 0, 10); + + const maxDepth = parseIntVal(flags.depth, 10); + + let baseDir = "."; + if (flags._.length > 0) { + baseDir = flags._[0]; + } + await listDir(baseDir, 0, maxDepth); } main().catch((e) => log.error(e)); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260127T005403+08:00.MEUCIFwdYkm52+V1DPMbAfoe -// b9/2d4Lnf0lfVqFbE3S17N+FAiEA8ySrI+cc6aKtr2eyy1veHPbTxPHoh57Cg3ozdYrzJfI= +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260130T005327+08:00.MEUCICFLf8ZlGN4bSzCiRBlW +// AnPVvd4by4hrwq6ZZPaN/cY6AiEAtyx0B6/EINNU2ilPoY1g0+LGc5FylLEJ5Ybn+pkpn8I=