@oclif/table 0.1.14 → 0.1.16

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.
Files changed (2) hide show
  1. package/lib/table.js +37 -4
  2. package/package.json +2 -1
package/lib/table.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable react/prop-types */
2
2
  import cliTruncate from 'cli-truncate';
3
3
  import { Box, Text, render } from 'ink';
4
+ import { WriteStream } from 'node:tty';
4
5
  import { sha1 } from 'object-hash';
5
6
  import React from 'react';
6
7
  import stripAnsi from 'strip-ansi';
@@ -254,19 +255,51 @@ export function Skeleton(props) {
254
255
  const texts = Array.from({ length: props.height ?? 1 }, (_, i) => (React.createElement(Text, { key: i, ...rest }, children)));
255
256
  return React.createElement(Box, { flexDirection: "column" }, texts);
256
257
  }
258
+ /**
259
+ * A custom WriteStream that captures the frames written to stdout.
260
+ * This allows us to avoid an issue where Ink rerenders the component twice
261
+ * because it uses ansiEscapes.clearTerminal, which doesn't seem to have
262
+ * the desired effect in powershell.
263
+ */
264
+ class Stream extends WriteStream {
265
+ frames = [];
266
+ lastFrame() {
267
+ return this.frames.filter((f) => stripAnsi(f) !== '').at(-1);
268
+ }
269
+ write(data) {
270
+ this.frames.push(data);
271
+ return true;
272
+ }
273
+ }
274
+ class Output {
275
+ stream;
276
+ constructor(fd = 1) {
277
+ this.stream = process.env.NODE_ENV === 'test' ? process.stdout : new WriteStream(fd);
278
+ }
279
+ maybePrintLastFrame() {
280
+ if (this.stream instanceof Stream) {
281
+ process.stdout.write(`${this.stream.lastFrame()}\n`);
282
+ }
283
+ else {
284
+ process.stdout.write('\n');
285
+ }
286
+ }
287
+ }
257
288
  /**
258
289
  * Renders a table with the given data.
259
290
  * @param options see {@link TableOptions}
260
291
  */
261
292
  export function printTable(options) {
262
- const instance = render(React.createElement(Table, { ...options }));
293
+ const output = new Output();
294
+ const instance = render(React.createElement(Table, { ...options }), { stdout: output.stream });
263
295
  instance.unmount();
264
- process.stdout.write('\n');
296
+ output.maybePrintLastFrame();
265
297
  }
266
298
  function Container(props) {
267
299
  return (React.createElement(Box, { flexWrap: "wrap", flexDirection: props.direction ?? 'row', ...props }, props.children));
268
300
  }
269
301
  export function printTables(tables, options) {
302
+ const output = new Output();
270
303
  const leftMargin = options?.marginLeft ?? options?.margin ?? 0;
271
304
  const rightMargin = options?.marginRight ?? options?.margin ?? 0;
272
305
  const columns = process.stdout.columns - (leftMargin + rightMargin);
@@ -275,7 +308,7 @@ export function printTables(tables, options) {
275
308
  // adjust maxWidth to account for margin and columnGap
276
309
  maxWidth: determineConfiguredWidth(table.maxWidth, columns) - (options?.columnGap ?? 0) * tables.length,
277
310
  }));
278
- const instance = render(React.createElement(Container, { ...options }, processed.map((table) => (React.createElement(Table, { key: sha1(table), ...table })))));
311
+ const instance = render(React.createElement(Container, { ...options }, processed.map((table) => (React.createElement(Table, { key: sha1(table), ...table })))), { stdout: output.stream });
279
312
  instance.unmount();
280
- process.stdout.write('\n');
313
+ output.maybePrintLastFrame();
281
314
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oclif/table",
3
3
  "description": "Display table in terminal",
4
- "version": "0.1.14",
4
+ "version": "0.1.16",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/table/issues",
7
7
  "dependencies": {
@@ -19,6 +19,7 @@
19
19
  "devDependencies": {
20
20
  "@commitlint/config-conventional": "^19",
21
21
  "@oclif/prettier-config": "^0.2.1",
22
+ "@oclif/test": "^4.0.9",
22
23
  "@types/chai": "^4.3.16",
23
24
  "@types/mocha": "^10.0.8",
24
25
  "@types/node": "^18",