@@ -0,0 +1,41 @@
|
||||
/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
|
||||
function bareStdin(ctx) {
|
||||
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
|
||||
}
|
||||
|
||||
function count(s) {
|
||||
const lines = (s.match(/\n/g) || []).length
|
||||
const words = s.trim() ? s.trim().split(/\s+/).length : 0
|
||||
const bytes = new TextEncoder().encode(s).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) {
|
||||
const s = bareStdin(ctx)
|
||||
const c = count(s)
|
||||
ctx.console.log(' ' + c.lines + ' ' + c.words + ' ' + c.bytes)
|
||||
return
|
||||
}
|
||||
let tLines = 0
|
||||
let tWords = 0
|
||||
let tBytes = 0
|
||||
for (const f of files) {
|
||||
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)
|
||||
tLines += c.lines
|
||||
tWords += c.words
|
||||
tBytes += c.bytes
|
||||
ctx.console.log(' ' + c.lines + ' ' + c.words + ' ' + c.bytes + ' ' + f)
|
||||
}
|
||||
if (files.length > 1) {
|
||||
ctx.console.log(' ' + tLines + ' ' + tWords + ' ' + tBytes + ' total')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user