@oclif/table 0.3.0 → 0.3.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/lib/table.js +3 -4
- package/lib/utils.js +23 -19
- package/package.json +1 -1
package/lib/table.js
CHANGED
|
@@ -340,11 +340,10 @@ function renderPlainTable(props) {
|
|
|
340
340
|
horizontalAlignment,
|
|
341
341
|
overflow,
|
|
342
342
|
padding,
|
|
343
|
-
value: column.column,
|
|
343
|
+
value: headings[column.column] ?? column.column,
|
|
344
344
|
width,
|
|
345
345
|
});
|
|
346
|
-
|
|
347
|
-
return `${acc}${' '.repeat(marginLeft)}${columnHeader}${' '.repeat(marginRight)}`;
|
|
346
|
+
return `${acc}${' '.repeat(marginLeft)}${text}${' '.repeat(marginRight)}`;
|
|
348
347
|
}, '');
|
|
349
348
|
console.log(headerString);
|
|
350
349
|
console.log('-'.repeat(headerString.length));
|
|
@@ -412,7 +411,7 @@ function Container(props) {
|
|
|
412
411
|
* @throws {Error} Throws an error if the total number of rows across all tables exceeds 30,000.
|
|
413
412
|
*/
|
|
414
413
|
export function printTables(tables, options) {
|
|
415
|
-
if (tables.reduce((acc, table) => acc + table.data.length, 0) >
|
|
414
|
+
if (tables.reduce((acc, table) => acc + table.data.length, 0) > 10_000) {
|
|
416
415
|
throw new Error('The data is too large to print multiple tables. Please use `printTable` instead.');
|
|
417
416
|
}
|
|
418
417
|
const output = new Output();
|
package/lib/utils.js
CHANGED
|
@@ -68,27 +68,31 @@ export function getColumns(config, headings) {
|
|
|
68
68
|
};
|
|
69
69
|
});
|
|
70
70
|
const numberOfBorders = widths.length + 1;
|
|
71
|
-
const calculateTableWidth = (widths) => widths.map((w) => w.width
|
|
72
|
-
// If the table is too wide, reduce the width of the largest column as little as possible to fit the table.
|
|
73
|
-
// At most, it will reduce the width to the length of the column's header plus padding.
|
|
74
|
-
// If the table is still too wide, it will reduce the width of the next largest column and so on
|
|
71
|
+
const calculateTableWidth = (widths) => widths.map((w) => w.width).reduce((a, b) => a + b, 0) + numberOfBorders;
|
|
75
72
|
let tableWidth = calculateTableWidth(widths);
|
|
76
73
|
const seen = new Set();
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
74
|
+
const reduceColumnWidths = (calcMinWidth) => {
|
|
75
|
+
// If the table is too wide, reduce the width of the largest column as little as possible to fit the table.
|
|
76
|
+
// If the table is still too wide, it will reduce the width of the next largest column and so on
|
|
77
|
+
while (tableWidth > maxWidth) {
|
|
78
|
+
const largestColumn = widths.filter((w) => !seen.has(w.key)).sort((a, b) => b.width - a.width)[0];
|
|
79
|
+
if (!largestColumn)
|
|
80
|
+
break;
|
|
81
|
+
if (seen.has(largestColumn.key))
|
|
82
|
+
break;
|
|
83
|
+
const minWidth = calcMinWidth(largestColumn);
|
|
84
|
+
const difference = tableWidth - maxWidth;
|
|
85
|
+
const newWidth = largestColumn.width - difference < minWidth ? minWidth : largestColumn.width - difference;
|
|
86
|
+
largestColumn.width = newWidth;
|
|
87
|
+
tableWidth = calculateTableWidth(widths);
|
|
88
|
+
seen.add(largestColumn.key);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
// At most, reduce the width to the length of the column's header plus padding.
|
|
92
|
+
reduceColumnWidths((col) => stripAnsi(String(headings[col.key])).length + col.padding * 2);
|
|
93
|
+
seen.clear();
|
|
94
|
+
// At most, reduce the width to the padding + 3
|
|
95
|
+
reduceColumnWidths((col) => col.padding * 2 + 3);
|
|
92
96
|
return widths;
|
|
93
97
|
}
|
|
94
98
|
export function getHeadings(config) {
|