@cto.af/chex-cli 2.1.2 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # chex-cli
2
+
3
+ Like `hexdump -C`, but as a small JS library.
4
+
5
+ ## Install
6
+
7
+ ```
8
+ npm install @cto.af/chex-cli
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ Usage: chex [options] [file...]
15
+
16
+ Arguments:
17
+ file file names to read or "-" for stdin
18
+
19
+ Options:
20
+ -c,--colors force colors on even if stdout is not a tty
21
+ --no-colors force colors off even if stdout is a tty
22
+ -C,--columns <number> how many columns of 8 bytes? (default: 2)
23
+ -d,--dots use dots for more control characters, instead of
24
+ Unicode representations
25
+ -p,--pager [pager command] override PAGER environment variable. If no
26
+ command is specified, turn off paging. Suggested:
27
+ "less -REX". (default: "less -REX")
28
+ -s,--strings <encoding> try to decode strings in this encoding (choices:
29
+ "utf8", "utf-8")
30
+ -V, --version output the version number
31
+ -h, --help display help for command
32
+ ```
33
+
34
+ [![Tests](https://github.com/cto-af/chex/actions/workflows/node.js.yml/badge.svg)](https://github.com/cto-af/chex/actions/workflows/node.js.yml)
35
+ [![codecov](https://codecov.io/gh/cto-af/chex/graph/badge.svg?token=S79ZXFIMZS)](https://codecov.io/gh/cto-af/chex)
package/bin/chex.js CHANGED
@@ -1,16 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import {Command, Option} from 'commander';
4
- import {HexDumpTransform} from '../lib/stream.js';
4
+ import {HexDumpTransform} from '@cto.af/chex';
5
5
  import fs from 'node:fs';
6
6
  import {version as libVersion} from '@cto.af/chex/lib/version.js';
7
+ import {spawn} from 'node:child_process';
7
8
  import {version} from '../lib/version.js';
8
9
 
9
10
  const program = new Command();
10
11
  program
11
12
  .argument('[file...]', 'file names to read or "-" for stdin')
12
13
  .option('-c,--colors', 'force colors on even if stdout is not a tty')
14
+ .option('--no-colors', 'force colors off even if stdout is a tty')
15
+ .option('-C,--columns <number>', 'how many columns of 8 bytes?', 2)
13
16
  .option('-d,--dots', 'use dots for more control characters, instead of Unicode representations')
17
+ .option('-p,--pager [pager command]',
18
+ 'override PAGER environment variable. If no command is specified, turn off paging. Suggested: "less -REX".',
19
+ process.env.PAGER ?? 'more')
14
20
  .addOption(new Option(
15
21
  '-s,--strings <encoding>',
16
22
  'try to decode strings in this encoding'
@@ -24,20 +30,56 @@ if (args.length === 0) {
24
30
  args.push('-');
25
31
  }
26
32
 
27
- function main(argv) {
28
- for (const fn of argv) {
29
- if (argv.length > 1) {
30
- console.log(fn);
33
+ function pager(pg) {
34
+ return new Promise((resolve, reject) => {
35
+ const child = spawn(pg, {
36
+ shell: true,
37
+ stdio: ['pipe', 'inherit', 'inherit'],
38
+ });
39
+ child.on('error', reject);
40
+ child.on('spawn', () => resolve(child.stdin));
41
+ });
42
+ }
43
+
44
+ function readStream(s) {
45
+ return new Promise((resolve, reject) => {
46
+ s.on('error', reject);
47
+ s.on('end', resolve);
48
+ });
49
+ }
50
+
51
+ async function main(argv) {
52
+ if (opts.pager === 'more') {
53
+ opts.colors = false;
54
+ }
55
+ let out = process.stdout;
56
+ if (typeof opts.pager === 'string') {
57
+ out = await pager(opts.pager);
58
+ }
59
+
60
+ try {
61
+ for (const fn of argv) {
62
+ if (argv.length > 1) {
63
+ out.write('File: ');
64
+ out.write(fn);
65
+ out.write('\n');
66
+ }
67
+ const str = (fn === '-') ?
68
+ process.stdin :
69
+ fs.createReadStream(fn);
70
+ str
71
+ .pipe(new HexDumpTransform({
72
+ colors: opts.colors ?? process.stdout.isTTY,
73
+ columns: opts.columns,
74
+ dots: opts.dots,
75
+ decode: opts.strings,
76
+ }))
77
+ .pipe(out, {end: false});
78
+ await readStream(str);
31
79
  }
32
- const str = (fn === '-') ?
33
- process.stdin :
34
- fs.createReadStream(fn);
35
- str.pipe(new HexDumpTransform({
36
- colors: opts.colors || process.stdout.isTTY,
37
- dots: opts.dots,
38
- decode: opts.strings,
39
- })).pipe(process.stdout);
80
+ } finally {
81
+ out.end();
40
82
  }
41
83
  }
42
84
 
43
- main(args);
85
+ main(args).catch(er => console.error(er.message));
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
- // Generated by package-extract v3.1.3
1
+ // Generated by package-extract v3.1.5
2
2
  // Do not modify by hand.
3
3
 
4
- export const version = '2.1.2';
4
+ export const version = '2.2.2';
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@cto.af/chex-cli",
3
- "version": "2.1.2",
3
+ "version": "2.2.2",
4
4
  "description": "hexdump -C as a small JS library",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
7
7
  "bin": {
8
8
  "chex": "bin/chex.js"
9
9
  },
10
+ "files": [
11
+ "lib/",
12
+ "bin/"
13
+ ],
10
14
  "repository": {
11
15
  "type": "git",
12
16
  "url": "git+https://github.com/cto-af/chex.git",
@@ -21,8 +25,8 @@
21
25
  "author": "Joe Hildebrand <joe-github@cursive.net>",
22
26
  "license": "MIT",
23
27
  "dependencies": {
24
- "@cto.af/chex": "2.1.2",
25
- "commander": "14.0.0"
28
+ "@cto.af/chex": "2.2.2",
29
+ "commander": "14.0.3"
26
30
  },
27
31
  "engines": {
28
32
  "node": ">=20"
package/lib/stream.js DELETED
@@ -1,77 +0,0 @@
1
- import {Transform} from 'node:stream';
2
- import {hexDump} from '@cto.af/chex';
3
-
4
- /**
5
- * @typedef {Partial<import('@cto.af/chex').HexOptions> &
6
- * import('node:stream').DuplexOptions} HexDumpStreamOptions
7
- */
8
-
9
- export class HexDumpTransform extends Transform {
10
- /** @type {HexDumpStreamOptions} */
11
- #opts;
12
- #buf = new Uint8Array(16);
13
- #bufLen = 0;
14
-
15
- /**
16
- *
17
- * @param {HexDumpStreamOptions} options
18
- */
19
- constructor(options = {}) {
20
- super(options);
21
- this.#opts = {
22
- firstByte: 0,
23
- ...options,
24
- printLength: false,
25
- };
26
- }
27
-
28
- /**
29
- * Transform a chunk of bytes.
30
- * @param {Uint8Array} chunk
31
- * @param {BufferEncoding} _encoding Should be null
32
- * @param {import('node:stream').TransformCallback} callback
33
- */
34
- _transform(chunk, _encoding, callback) {
35
- let res = '';
36
- if (this.#bufLen > 0) {
37
- const front = Math.min(chunk.length, 16 - this.#bufLen);
38
- if (front > 0) {
39
- this.#buf.set(chunk.subarray(0, front), this.#bufLen);
40
- this.#bufLen += front;
41
- chunk = chunk.subarray(front);
42
- }
43
- if (this.#bufLen === 16) {
44
- res += hexDump(this.#buf, this.#opts);
45
- this.#opts.firstByte += 16;
46
- this.#bufLen = 0;
47
- }
48
- }
49
- if (chunk.length > 0) {
50
- const tail = chunk.length % 16;
51
- if (tail > 0) {
52
- this.#buf.set(chunk.subarray(-tail), this.#bufLen);
53
- this.#bufLen += tail;
54
- chunk = chunk.subarray(0, -tail);
55
- }
56
- // Should be multiple of 16 now
57
- if (chunk.length > 0) {
58
- res += hexDump(chunk, this.#opts);
59
- this.#opts.firstByte += chunk.length;
60
- }
61
- }
62
- callback(null, res);
63
- }
64
-
65
- /**
66
- * @param {import('node:stream').TransformCallback} callback
67
- */
68
- _flush(callback) {
69
- callback(null, hexDump(this.#buf.subarray(0, this.#bufLen), {
70
- ...this.#opts,
71
- printLength: true,
72
- }));
73
- }
74
- }
75
-
76
- //
77
- // process.stdin.pipe(new HexDumpTransform()).pipe(process.stdout);