@oclif/table 0.1.24 → 0.2.1
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 +1 -1
- package/lib/index.js +1 -1
- package/lib/table.d.ts +22 -2
- package/lib/table.js +29 -4
- package/lib/utils.js +1 -1
- package/package.json +1 -1
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
|
-
*
|
|
31
|
-
*
|
|
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
|
@@ -37,7 +37,7 @@ function determineConfiguredWidth(providedWidth, columns = process.stdout.column
|
|
|
37
37
|
* This allows us to use the minimum width required to display the table if the configured width is too small.
|
|
38
38
|
*/
|
|
39
39
|
function determineWidthToUse(columns, configuredWidth) {
|
|
40
|
-
const tableWidth = columns.map((c) => c.width).reduce((a, b) => a + b) + columns.length + 1;
|
|
40
|
+
const tableWidth = columns.map((c) => c.width).reduce((a, b) => a + b, 0) + columns.length + 1;
|
|
41
41
|
return tableWidth < configuredWidth ? configuredWidth : tableWidth;
|
|
42
42
|
}
|
|
43
43
|
function determineTruncatePosition(overflow) {
|
|
@@ -306,7 +306,7 @@ const createStdout = () => {
|
|
|
306
306
|
const stripped = stripAnsi(f);
|
|
307
307
|
return stripped !== '' && stripped !== '\n';
|
|
308
308
|
})
|
|
309
|
-
.at(-1);
|
|
309
|
+
.at(-1) ?? '';
|
|
310
310
|
return stdout;
|
|
311
311
|
};
|
|
312
312
|
class Output {
|
|
@@ -355,8 +355,12 @@ function renderTableInChunks(props) {
|
|
|
355
355
|
footerOutput.maybePrintLastFrame();
|
|
356
356
|
}
|
|
357
357
|
/**
|
|
358
|
-
*
|
|
359
|
-
*
|
|
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}
|
|
360
364
|
*/
|
|
361
365
|
export function printTable(options) {
|
|
362
366
|
if (options.data.length > 50_000) {
|
|
@@ -368,9 +372,30 @@ export function printTable(options) {
|
|
|
368
372
|
instance.unmount();
|
|
369
373
|
output.maybePrintLastFrame();
|
|
370
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
|
+
}
|
|
371
388
|
function Container(props) {
|
|
372
389
|
return (React.createElement(Box, { flexWrap: "wrap", flexDirection: props.direction ?? 'row', ...props }, props.children));
|
|
373
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
|
+
*/
|
|
374
399
|
export function printTables(tables, options) {
|
|
375
400
|
if (tables.reduce((acc, table) => acc + table.data.length, 0) > 30_000) {
|
|
376
401
|
throw new Error('The data is too large to print multiple tables. Please use `printTable` instead.');
|
package/lib/utils.js
CHANGED
|
@@ -67,7 +67,7 @@ export function getColumns(config, headings) {
|
|
|
67
67
|
};
|
|
68
68
|
});
|
|
69
69
|
const numberOfBorders = widths.length + 1;
|
|
70
|
-
const calculateTableWidth = (widths) => widths.map((w) => w.width + w.padding * 2).reduce((a, b) => a + b) + numberOfBorders;
|
|
70
|
+
const calculateTableWidth = (widths) => widths.map((w) => w.width + w.padding * 2).reduce((a, b) => a + b, 0) + numberOfBorders;
|
|
71
71
|
// If the table is too wide, reduce the width of the largest column as little as possible to fit the table.
|
|
72
72
|
// At most, it will reduce the width to the length of the column's header plus padding.
|
|
73
73
|
// If the table is still too wide, it will reduce the width of the next largest column and so on
|