@@ -0,0 +1,38 @@
|
||||
/** 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
|
||||
} else if (argv[1] && /^-\d+$/.test(argv[1])) {
|
||||
n = parseInt(argv[1].slice(1), 10) || 10
|
||||
start = 2
|
||||
}
|
||||
const files = argv.slice(start).filter((a) => a !== '--')
|
||||
const take = (s) => {
|
||||
const lines = s.split('\n')
|
||||
const out = lines.slice(0, n).join('\n')
|
||||
ctx.console.log(
|
||||
out + (out && !out.endsWith('\n') && lines.length > n ? '\n' : '')
|
||||
)
|
||||
}
|
||||
if (!files.length) {
|
||||
take(bareStdin(ctx))
|
||||
return
|
||||
}
|
||||
for (const f of files) {
|
||||
const buf = await vfs.readFile(f)
|
||||
if (!buf) {
|
||||
ctx.console.error('head: ' + f + ': No such file')
|
||||
continue
|
||||
}
|
||||
if (files.length > 1) ctx.console.log('==> ' + f + ' <==')
|
||||
take(ctx.b4a.toString(buf))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user