core utils updates
This commit is contained in:
+68
-9
@@ -60,12 +60,63 @@ function barePosixBlocks(size) {
|
||||
return Math.ceil(Number(size) / 512) || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
|
||||
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
|
||||
* @param {Record<string, unknown>} ctx
|
||||
* @param {string | Uint8Array} chunk
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function bareOsEmitRaw(ctx, chunk) {
|
||||
if (typeof ctx.bareOsBinWrite === 'function') {
|
||||
const b4 = ctx.b4a
|
||||
const u8 =
|
||||
typeof chunk === 'string'
|
||||
? b4 && typeof b4.from === 'function'
|
||||
? b4.from(chunk)
|
||||
: new TextEncoder().encode(chunk)
|
||||
: chunk
|
||||
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
|
||||
return true
|
||||
}
|
||||
const w = globalThis.process?.stdout?.write
|
||||
if (typeof w === 'function') {
|
||||
w.call(globalThis.process.stdout, chunk)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function hexByte(b) {
|
||||
return b.toString(16).padStart(2, '0')
|
||||
}
|
||||
|
||||
async function run(ctx, argv) {
|
||||
const paths = argv.slice(1).filter((a) => a && !a.startsWith('-'))
|
||||
let showAddr = true
|
||||
/** @type {'both' | 'hex' | 'char'} */
|
||||
let style = 'both'
|
||||
const paths = []
|
||||
for (let i = 1; i < argv.length; i++) {
|
||||
const a = argv[i]
|
||||
if (a === '-An') {
|
||||
showAddr = false
|
||||
continue
|
||||
}
|
||||
if (a === '-A' && argv[i + 1] === 'n') {
|
||||
showAddr = false
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a === '-x') {
|
||||
style = 'hex'
|
||||
continue
|
||||
}
|
||||
if (a === '-c') {
|
||||
style = 'char'
|
||||
continue
|
||||
}
|
||||
if (!a.startsWith('-')) paths.push(a)
|
||||
}
|
||||
let buf
|
||||
if (!paths.length) {
|
||||
buf = ctx.b4a.from(bareStdin(ctx))
|
||||
@@ -86,14 +137,22 @@ async function run(ctx, argv) {
|
||||
const asc = [...chunk]
|
||||
.map((x) => (x >= 32 && x < 127 ? String.fromCharCode(x) : '.'))
|
||||
.join('')
|
||||
ctx.console.log(
|
||||
off.toString(8).padStart(7, '0') +
|
||||
' ' +
|
||||
hex.padEnd(47, ' ') +
|
||||
' |' +
|
||||
asc +
|
||||
'|'
|
||||
)
|
||||
let line = ''
|
||||
if (showAddr) line += off.toString(8).padStart(7, '0') + ' '
|
||||
if (style === 'hex') line += hex
|
||||
else if (style === 'char')
|
||||
line +=
|
||||
[...chunk]
|
||||
.map((x) => {
|
||||
if (x === 9) return '\\t'
|
||||
if (x === 10) return '\\n'
|
||||
if (x === 13) return '\\r'
|
||||
if (x >= 32 && x < 127) return String.fromCharCode(x)
|
||||
return '\\' + x.toString(8).padStart(3, '0')
|
||||
})
|
||||
.join(' ')
|
||||
else line += hex.padEnd(47, ' ') + ' |' + asc + '|'
|
||||
ctx.console.log(line.trimEnd())
|
||||
off += 16
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user