core utils updates
This commit is contained in:
+127
-13
@@ -60,39 +60,153 @@ function barePosixBlocks(size) {
|
||||
return Math.ceil(Number(size) / 512) || 0
|
||||
}
|
||||
|
||||
function count(s, b4a) {
|
||||
/**
|
||||
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
|
||||
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
|
||||
* @param {Record<string, unknown>} ctx
|
||||
* @param {string | Uint8Array} chunk
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function bareOsEmitRaw(ctx, chunk) {
|
||||
if (typeof ctx.bareOsBinWrite === 'function') {
|
||||
const b4 = ctx.b4a
|
||||
const u8 =
|
||||
typeof chunk === 'string'
|
||||
? b4 && typeof b4.from === 'function'
|
||||
? b4.from(chunk)
|
||||
: new TextEncoder().encode(chunk)
|
||||
: chunk
|
||||
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
|
||||
return true
|
||||
}
|
||||
const w = globalThis.process?.stdout?.write
|
||||
if (typeof w === 'function') {
|
||||
w.call(globalThis.process.stdout, chunk)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* wc — line, word, and byte counts.
|
||||
* Default (no flags): -l -w -c. Flags are combinable.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string | Uint8Array} input
|
||||
* @param {*} b4a
|
||||
*/
|
||||
function wcCount(input, b4a) {
|
||||
const bytes =
|
||||
input instanceof Uint8Array
|
||||
? input.byteLength
|
||||
: b4a.from(String(input), 'utf8').length
|
||||
const s =
|
||||
input instanceof Uint8Array ? b4a.toString(input) : String(input)
|
||||
const lines = (s.match(/\n/g) || []).length
|
||||
const words = s.trim() ? s.trim().split(/\s+/).length : 0
|
||||
const bytes = b4a.from(s, 'utf8').length
|
||||
const words = (s.match(/\S+/g) || []).length
|
||||
return { lines, words, bytes }
|
||||
}
|
||||
|
||||
async function run(ctx, argv) {
|
||||
const vfs = ctx.vfs
|
||||
const files = argv.slice(1).filter((a) => !a.startsWith('-'))
|
||||
if (!files.length) {
|
||||
let wantL = false
|
||||
let wantW = false
|
||||
let wantC = false
|
||||
const paths = []
|
||||
let i = 1
|
||||
while (i < argv.length) {
|
||||
const a = argv[i]
|
||||
if (a === '--') {
|
||||
i++
|
||||
break
|
||||
}
|
||||
if (a === '-l' || a === '--lines') {
|
||||
wantL = true
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a === '-w' || a === '--words') {
|
||||
wantW = true
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a === '-c' || a === '--bytes' || a === '-m') {
|
||||
wantC = true
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a.startsWith('-') && a.length > 1) {
|
||||
let j = 1
|
||||
let ok = true
|
||||
while (j < a.length) {
|
||||
const c = a[j++]
|
||||
if (c === 'l') wantL = true
|
||||
else if (c === 'w') wantW = true
|
||||
else if (c === 'c' || c === 'm') wantC = true
|
||||
else {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
}
|
||||
paths.push(a)
|
||||
i++
|
||||
}
|
||||
if (!wantL && !wantW && !wantC) {
|
||||
wantL = true
|
||||
wantW = true
|
||||
wantC = true
|
||||
}
|
||||
|
||||
function fmt(c, name) {
|
||||
const parts = []
|
||||
if (wantL) parts.push(String(c.lines))
|
||||
if (wantW) parts.push(String(c.words))
|
||||
if (wantC) parts.push(String(c.bytes))
|
||||
let s = ' ' + parts.join(' ')
|
||||
if (name != null) s += ' ' + name
|
||||
return s
|
||||
}
|
||||
|
||||
if (!paths.length) {
|
||||
const s = bareStdin(ctx)
|
||||
const c = count(s, ctx.b4a)
|
||||
ctx.console.log(' ' + c.lines + ' ' + c.words + ' ' + c.bytes)
|
||||
const c = wcCount(s, ctx.b4a)
|
||||
ctx.console.log(fmt(c, null))
|
||||
return
|
||||
}
|
||||
let tLines = 0
|
||||
let tWords = 0
|
||||
let tBytes = 0
|
||||
for (const f of files) {
|
||||
for (const f of paths) {
|
||||
if (f === '-') {
|
||||
const s = bareStdin(ctx)
|
||||
const c = wcCount(s, ctx.b4a)
|
||||
tLines += c.lines
|
||||
tWords += c.words
|
||||
tBytes += c.bytes
|
||||
ctx.console.log(fmt(c, '-'))
|
||||
continue
|
||||
}
|
||||
const buf = await vfs.readFile(f)
|
||||
if (!buf) {
|
||||
ctx.console.error('wc: ' + f + ': No such file')
|
||||
continue
|
||||
}
|
||||
const s = ctx.b4a.toString(buf)
|
||||
const c = count(s, ctx.b4a)
|
||||
const u8 = buf instanceof Uint8Array ? buf : ctx.b4a.from(buf)
|
||||
const c = wcCount(u8, ctx.b4a)
|
||||
tLines += c.lines
|
||||
tWords += c.words
|
||||
tBytes += c.bytes
|
||||
ctx.console.log(' ' + c.lines + ' ' + c.words + ' ' + c.bytes + ' ' + f)
|
||||
ctx.console.log(fmt(c, f))
|
||||
}
|
||||
if (files.length > 1) {
|
||||
ctx.console.log(' ' + tLines + ' ' + tWords + ' ' + tBytes + ' total')
|
||||
if (paths.length > 1) {
|
||||
ctx.console.log(
|
||||
fmt({ lines: tLines, words: tWords, bytes: tBytes }, 'total')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user