@@ -0,0 +1,30 @@
|
||||
/** 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('-'))
|
||||
/** @type {string[]} */
|
||||
let lines = []
|
||||
if (!files.length) {
|
||||
const s = bareStdin(ctx)
|
||||
lines = s.length ? s.split('\n') : []
|
||||
if (lines.length && lines[lines.length - 1] === '') lines.pop()
|
||||
} else {
|
||||
for (const f of files) {
|
||||
const buf = await vfs.readFile(f)
|
||||
if (!buf) {
|
||||
ctx.console.error('sort: ' + f + ': No such file')
|
||||
ctx.exitCode = 1
|
||||
continue
|
||||
}
|
||||
const part = ctx.b4a.toString(buf).split('\n')
|
||||
if (part.length && part[part.length - 1] === '') part.pop()
|
||||
lines.push(...part)
|
||||
}
|
||||
}
|
||||
lines.sort((x, y) => (x < y ? -1 : x > y ? 1 : 0))
|
||||
if (lines.length) ctx.console.log(lines.join('\n'))
|
||||
}
|
||||
Reference in New Issue
Block a user