35 lines
969 B
Plaintext
35 lines
969 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
|
|
let n = 10
|
|
let start = 1
|
|
if (argv[1] === '-n' && argv[2]) {
|
|
n = parseInt(argv[2], 10) || 10
|
|
start = 3
|
|
}
|
|
const files = argv.slice(start).filter((a) => a !== '--')
|
|
const take = (s) => {
|
|
let lines = s.split('\n')
|
|
if (s.endsWith('\n') && lines[lines.length - 1] === '') lines.pop()
|
|
const slice = lines.length <= n ? lines : lines.slice(-n)
|
|
ctx.console.log(slice.join('\n'))
|
|
}
|
|
if (!files.length) {
|
|
take(bareStdin(ctx))
|
|
return
|
|
}
|
|
for (const f of files) {
|
|
const buf = await vfs.readFile(f)
|
|
if (!buf) {
|
|
ctx.console.error('tail: ' + f + ': No such file')
|
|
continue
|
|
}
|
|
if (files.length > 1) ctx.console.log('==> ' + f + ' <==')
|
|
take(ctx.b4a.toString(buf))
|
|
}
|
|
}
|