@oclif/table 0.3.1 → 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/utils.js +23 -19
- package/package.json +1 -1
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) {
|