Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Argument to set width for diagnostic cli outputs #118

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading