This commit is contained in:
Raven Scott
2026-04-03 19:49:21 -04:00
parent 76dc3ecb7c
commit 4dfee8398b
19 changed files with 504 additions and 250 deletions
+23 -5
View File
@@ -60,7 +60,11 @@ function barePosixBlocks(size) {
return Math.ceil(Number(size) / 512) || 0
}
async function walk(ctx, dir, nameRe, wantType, maxDepth, curDepth) {
/**
* curDepth is distance from the search root directory (0 at the initial path).
* Printed paths use gnu-like depth curDepth + 1 (immediate children of the root are depth 1).
*/
async function walk(ctx, dir, nameRe, wantType, maxDepth, minDepth, curDepth) {
if (maxDepth >= 0 && curDepth > maxDepth) return
let names
try {
@@ -78,15 +82,21 @@ async function walk(ctx, dir, nameRe, wantType, maxDepth, curDepth) {
continue
}
if (!st) continue
const gnuDepth = curDepth + 1
if (!nameRe || nameRe.test(n)) {
if (!wantType || st.type === wantType) ctx.console.log(path)
if (!wantType || st.type === wantType) {
if (gnuDepth >= minDepth) ctx.console.log(path)
}
}
if (st.type === 'directory') await walk(ctx, path, nameRe, wantType, maxDepth, curDepth + 1)
if (st.type === 'directory')
await walk(ctx, path, nameRe, wantType, maxDepth, minDepth, curDepth + 1)
}
}
async function run(ctx, argv) {
let maxDepth = -1
/** Minimum path depth below the search root (1 = default; same as GNU -mindepth 1 for tree walks). */
let minDepth = 1
/** @type {string | null} */
let nameGlob = null
/** @type {'file' | 'directory' | 'symlink' | null} */
@@ -98,6 +108,11 @@ async function run(ctx, argv) {
maxDepth = Number.parseInt(argv[++i], 10)
continue
}
if (a === '-mindepth' && argv[i + 1]) {
minDepth = Number.parseInt(argv[++i], 10)
if (!Number.isFinite(minDepth) || minDepth < 1) minDepth = 1
continue
}
if (a === '-name' && argv[i + 1]) {
nameGlob = argv[++i]
continue
@@ -123,9 +138,12 @@ async function run(ctx, argv) {
const root = rest[0] || '.'
let nameRe = null
if (nameGlob) {
const esc = nameGlob.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.')
const esc = nameGlob
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
.replace(/\*/g, '.*')
.replace(/\?/g, '.')
nameRe = new RegExp('^' + esc + '$')
}
const abs = ctx.vfs.resolveLogical(root)
await walk(ctx, abs, nameRe, wantType, maxDepth, 0)
await walk(ctx, abs, nameRe, wantType, maxDepth, minDepth, 0)
}