This commit is contained in:
2026-01-30 00:54:40 +08:00
parent 1d0ab0f89f
commit 150c1a099d
2 changed files with 50 additions and 11 deletions

View File

@@ -105,6 +105,25 @@ export function isOn(val: string | undefined | null): boolean {
lowerVal === "true"; 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 { export function getEnv(envKey: string): string | null {
const homeDir = getHomeDir(); const homeDir = getHomeDir();
if ((homeDir !== null) && envKey) { if ((homeDir !== null) && envKey) {

View File

@@ -4,8 +4,10 @@ import {
formatSize2, formatSize2,
joinPath, joinPath,
log, log,
parseIntVal,
term, 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 = [ const defaultSkipDirs = [
".git", ".git",
@@ -30,10 +32,10 @@ async function listDir(
} }
const fullName = joinPath(dir, dirEntry.name); const fullName = joinPath(dir, dirEntry.name);
if (dirEntry.isDirectory) { if (dirEntry.isDirectory) {
const showNextDepth = depth <= maxDepth; const showNextDepth = (depth + 1) <= maxDepth;
console.log( console.log(
`${tab}- [${dirEntry.name}]${ `${tab}- [${dirEntry.name}]${
showNextDepth ? "" : " ...more dirs..." showNextDepth ? "" : term.blue(" \t[...more dirs...]")
}`, }`,
); );
if (showNextDepth) { if (showNextDepth) {
@@ -46,7 +48,9 @@ async function listDir(
} else if (dirEntry.isFile) { } else if (dirEntry.isFile) {
const fileInfo = await Deno.stat(fullName); const fileInfo = await Deno.stat(fullName);
if (fileInfo.size > 1024 * 1024) { if (fileInfo.size > 1024 * 1024) {
fileDesc = term.red(` - ${formatSize2(fileInfo.size)}`); fileDesc = term.red(
` - ${formatSize2(fileInfo.size)}`,
);
} else { } else {
fileDesc = term.yellow( fileDesc = term.yellow(
` - ${formatSize2(fileInfo.size)}`, ` - ${formatSize2(fileInfo.size)}`,
@@ -54,7 +58,7 @@ async function listDir(
} }
} }
console.log( 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<void> { async function main(): Promise<void> {
let baseDir = "."; const flags = parseArgs(Deno.args, {
if (Deno.args.length > 0) { boolean: ["help"],
baseDir = Deno.args[0]; number: ["depth"],
});
if (flags.help) {
console.log(`Help massage for tree.ts
tree.ts [parameters] <dir>
--depth 3 - Directory depth
<dir> - 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)); main().catch((e) => log.error(e));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260127T005403+08:00.MEUCIFwdYkm52+V1DPMbAfoe // @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260130T005327+08:00.MEUCICFLf8ZlGN4bSzCiRBlW
// b9/2d4Lnf0lfVqFbE3S17N+FAiEA8ySrI+cc6aKtr2eyy1veHPbTxPHoh57Cg3ozdYrzJfI= // AnPVvd4by4hrwq6ZZPaN/cY6AiEAtyx0B6/EINNU2ilPoY1g0+LGc5FylLEJ5Ybn+pkpn8I=