@oclif/table 0.1.23 → 0.2.0

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/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { printTable, printTables } from './table.js';
1
+ export { makeTable, printTable, printTables } from './table.js';
2
2
  export type { TableOptions } from './types.js';
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- export { printTable, printTables } from './table.js';
1
+ export { makeTable, printTable, printTables } from './table.js';
package/lib/table.d.ts CHANGED
@@ -27,10 +27,30 @@ export declare function Skeleton(props: React.PropsWithChildren & {
27
27
  readonly height?: number;
28
28
  }): React.JSX.Element;
29
29
  /**
30
- * Renders a table with the given data.
31
- * @param options see {@link TableOptions}
30
+ * Prints a table based on the provided options. If the data length exceeds 50,000 entries,
31
+ * the table is rendered in chunks to handle large datasets efficiently.
32
+ *
33
+ * @template T - A generic type that extends a record with string keys and unknown values.
34
+ * @param {TableOptions<T>} options - The options for rendering the table, including data and other configurations.
35
+ * @returns {void}
32
36
  */
33
37
  export declare function printTable<T extends Record<string, unknown>>(options: TableOptions<T>): void;
38
+ /**
39
+ * Generates a table as a string based on the provided options.
40
+ *
41
+ * @template T - A generic type extending a record with string keys and unknown values.
42
+ * @param {TableOptions<T>} options - The options to configure the table.
43
+ * @returns {string} The rendered table as a string.
44
+ */
45
+ export declare function makeTable<T extends Record<string, unknown>>(options: TableOptions<T>): string;
46
+ /**
47
+ * Prints multiple tables to the console.
48
+ *
49
+ * @template T - An array of records where each record represents a table.
50
+ * @param {Object.<keyof T, TableOptions<T[keyof T]>>} tables - An object containing table options for each table.
51
+ * @param {Omit<ContainerProps, 'children'>} [options] - Optional container properties excluding 'children'.
52
+ * @throws {Error} Throws an error if the total number of rows across all tables exceeds 30,000.
53
+ */
34
54
  export declare function printTables<T extends Record<string, unknown>[]>(tables: {
35
55
  [P in keyof T]: TableOptions<T[P]>;
36
56
  }, options?: Omit<ContainerProps, 'children'>): void;
package/lib/table.js CHANGED
@@ -1,7 +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
+ import { EventEmitter } from 'node:events';
5
5
  import { sha1 } from 'object-hash';
6
6
  import React from 'react';
7
7
  import stripAnsi from 'strip-ansi';
@@ -279,49 +279,43 @@ export function Skeleton(props) {
279
279
  return React.createElement(Box, { flexDirection: "column" }, texts);
280
280
  }
281
281
  /**
282
- * A custom WriteStream that captures the frames written to stdout.
282
+ * Return a custom WriteStream that captures the frames written to stdout.
283
283
  * This allows us to avoid an issue where Ink rerenders the component twice
284
284
  * because it uses ansiEscapes.clearTerminal, which doesn't seem to have
285
285
  * the desired effect in powershell.
286
+ *
287
+ * Implementation inspired by https://github.com/vadimdemedes/ink/blob/master/test/helpers/create-stdout.ts
286
288
  */
287
- class Stream extends WriteStream {
289
+ const createStdout = () => {
290
+ // eslint-disable-next-line unicorn/prefer-event-target
291
+ const stdout = new EventEmitter();
288
292
  // Override the rows so that ink doesn't clear the entire terminal when
289
293
  // unmounting the component and the height of the output is greater than
290
294
  // the height of the terminal
291
295
  // https://github.com/vadimdemedes/ink/blob/v5.0.1/src/ink.tsx#L174
292
296
  // This might be a bad idea but it works.
293
- rows = 10_000;
294
- frames = [];
295
- lastFrame() {
296
- return this.frames
297
- .filter((f) => {
298
- const stripped = stripAnsi(f);
299
- return stripped !== '' && stripped !== '\n';
300
- })
301
- .at(-1);
302
- }
303
- write(data) {
304
- this.frames.push(data);
297
+ stdout.rows = 10_000;
298
+ stdout.columns = process.stdout.columns ?? 80;
299
+ const frames = [];
300
+ stdout.write = (data) => {
301
+ frames.push(data);
305
302
  return true;
306
- }
307
- }
303
+ };
304
+ stdout.lastFrame = () => frames
305
+ .filter((f) => {
306
+ const stripped = stripAnsi(f);
307
+ return stripped !== '' && stripped !== '\n';
308
+ })
309
+ .at(-1);
310
+ return stdout;
311
+ };
308
312
  class Output {
309
313
  stream;
310
314
  constructor() {
311
- // Use process.stdout if NODE_ENV is `test` OR if tests are being run by wireit on windows (windows + tests run by wireit
312
- // are problematic for an unknown reason)
313
- this.stream =
314
- (process.platform === 'win32' && process.env.npm_lifecycle_script === 'wireit') || process.env.NODE_ENV === 'test'
315
- ? process.stdout
316
- : new Stream(process.env.OCLIF_TABLE_FD ? Number(process.env.OCLIF_TABLE_FD) : 0);
315
+ this.stream = createStdout();
317
316
  }
318
317
  maybePrintLastFrame() {
319
- if (this.stream instanceof Stream) {
320
- process.stdout.write(`${this.stream.lastFrame()}`);
321
- }
322
- else {
323
- process.stdout.write('\n');
324
- }
318
+ process.stdout.write(`${this.stream.lastFrame()}\n`);
325
319
  }
326
320
  }
327
321
  function chunk(array, size) {
@@ -361,8 +355,12 @@ function renderTableInChunks(props) {
361
355
  footerOutput.maybePrintLastFrame();
362
356
  }
363
357
  /**
364
- * Renders a table with the given data.
365
- * @param options see {@link TableOptions}
358
+ * Prints a table based on the provided options. If the data length exceeds 50,000 entries,
359
+ * the table is rendered in chunks to handle large datasets efficiently.
360
+ *
361
+ * @template T - A generic type that extends a record with string keys and unknown values.
362
+ * @param {TableOptions<T>} options - The options for rendering the table, including data and other configurations.
363
+ * @returns {void}
366
364
  */
367
365
  export function printTable(options) {
368
366
  if (options.data.length > 50_000) {
@@ -374,9 +372,30 @@ export function printTable(options) {
374
372
  instance.unmount();
375
373
  output.maybePrintLastFrame();
376
374
  }
375
+ /**
376
+ * Generates a table as a string based on the provided options.
377
+ *
378
+ * @template T - A generic type extending a record with string keys and unknown values.
379
+ * @param {TableOptions<T>} options - The options to configure the table.
380
+ * @returns {string} The rendered table as a string.
381
+ */
382
+ export function makeTable(options) {
383
+ const output = new Output();
384
+ const instance = render(React.createElement(Table, { ...options }), { stdout: output.stream });
385
+ instance.unmount();
386
+ return output.stream.lastFrame() ?? '';
387
+ }
377
388
  function Container(props) {
378
389
  return (React.createElement(Box, { flexWrap: "wrap", flexDirection: props.direction ?? 'row', ...props }, props.children));
379
390
  }
391
+ /**
392
+ * Prints multiple tables to the console.
393
+ *
394
+ * @template T - An array of records where each record represents a table.
395
+ * @param {Object.<keyof T, TableOptions<T[keyof T]>>} tables - An object containing table options for each table.
396
+ * @param {Omit<ContainerProps, 'children'>} [options] - Optional container properties excluding 'children'.
397
+ * @throws {Error} Throws an error if the total number of rows across all tables exceeds 30,000.
398
+ */
380
399
  export function printTables(tables, options) {
381
400
  if (tables.reduce((acc, table) => acc + table.data.length, 0) > 30_000) {
382
401
  throw new Error('The data is too large to print multiple tables. Please use `printTable` instead.');
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.23",
4
+ "version": "0.2.0",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/table/issues",
7
7
  "dependencies": {