34 lines
926 B
Plaintext
34 lines
926 B
Plaintext
/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
|
|
function bareStdin(ctx) {
|
|
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
|
|
}
|
|
|
|
async function run(ctx, argv) {
|
|
const vfs = ctx.vfs
|
|
const files = argv.slice(1).filter((a) => !a.startsWith('-'))
|
|
let body = ''
|
|
if (!files.length) {
|
|
body = bareStdin(ctx)
|
|
if (body === '') return
|
|
} else {
|
|
for (const f of files) {
|
|
const buf = await vfs.readFile(f)
|
|
if (!buf) {
|
|
ctx.console.error('nl: ' + f + ': No such file')
|
|
ctx.exitCode = 1
|
|
continue
|
|
}
|
|
body += ctx.b4a.toString(buf)
|
|
}
|
|
}
|
|
const lines = body.split('\n')
|
|
if (lines.length && lines[lines.length - 1] === '') lines.pop()
|
|
let n = 1
|
|
for (const line of lines) {
|
|
const num = String(n)
|
|
const pad = ' '.repeat(Math.max(0, 6 - num.length)) + num
|
|
ctx.console.log(pad + '\t' + line)
|
|
n++
|
|
}
|
|
}
|