Skip to content

Commit

Permalink
feat: cli argument to set width of diagnostic output (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
c2bo authored Apr 5, 2024
1 parent cdb8552 commit 4891cb8
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import { fromHex as _fromHex, toHex } from './byte-utils.js'
function usage (code) {
console.error('Usage: cborg <command> <args>')
console.error('Valid commands:')
console.error('\tbin2diag [binary input]')
console.error('\tbin2diag [--width <width>] [binary input]')
console.error('\tbin2hex [binary input]')
console.error('\tbin2json [--pretty] [binary input]')
console.error('\tdiag2bin [diagnostic input]')
console.error('\tdiag2hex [diagnostic input]')
console.error('\tdiag2json [--pretty] [diagnostic input]')
console.error('\thex2bin [hex input]')
console.error('\thex2diag [hex input]')
console.error('\thex2diag [--width <width>] [hex input]')
console.error('\thex2json [--pretty] [hex input]')
console.error('\tjson2bin \'[json input]\'')
console.error('\tjson2diag \'[json input]\'')
console.error('\tjson2diag [--width <width>] \'[json input]\'')
console.error('\tjson2hex \'[json input]\'')
console.error('Input may either be supplied as an argument or piped via stdin')
process.exit(code || 0)
Expand Down Expand Up @@ -54,6 +54,20 @@ function argvPretty () {
return { argv, pretty }
}

function argvWidth () {
const widthIndex = process.argv.findIndex((s) => s === '--width')
if (widthIndex <= 0 || widthIndex + 1 >= process.argv.length) {
return { argv: process.argv.filter((s) => s !== '--width'), width: undefined }
}
const width = parseInt(process.argv[widthIndex + 1], 10)
if (!width || width < 20) {
return { argv: process.argv.filter((s) => s !== '--width'), width: undefined }
}
const argv = process.argv
argv.splice(widthIndex, 2)
return { argv, width }
}

async function run () {
const cmd = process.argv[2]

Expand All @@ -64,8 +78,9 @@ async function run () {

case 'bin2diag': {
/* c8 ignore next 1 */
const bin = process.argv.length < 4 ? (await fromStdin()) : new TextEncoder().encode(process.argv[3])
for (const line of tokensToDiagnostic(bin)) {
const { argv, width } = argvWidth()
const bin = argv.length < 4 ? (await fromStdin()) : new TextEncoder().encode(argv[3])
for (const line of tokensToDiagnostic(bin, width)) {
console.log(line)
}
return
Expand Down Expand Up @@ -114,8 +129,9 @@ async function run () {
}

case 'hex2diag': {
const bin = fromHex(process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3])
for (const line of tokensToDiagnostic(bin)) {
const { argv, width } = argvWidth()
const bin = fromHex(argv.length < 4 ? (await fromStdin()).toString() : argv[3])
for (const line of tokensToDiagnostic(bin, width)) {
console.log(line)
}
return
Expand All @@ -134,9 +150,10 @@ async function run () {
}

case 'json2diag': {
const inp = process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3]
const { argv, width } = argvWidth()
const inp = argv.length < 4 ? (await fromStdin()).toString() : argv[3]
const obj = JSON.parse(inp)
for (const line of tokensToDiagnostic(encode(obj))) {
for (const line of tokensToDiagnostic(encode(obj), width)) {
console.log(line)
}
return
Expand Down

0 comments on commit 4891cb8

Please sign in to comment.