@oclif/table 0.2.0 → 0.2.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 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) {
@@ -60,6 +60,9 @@ export function formatTextWithMargins({ horizontalAlignment, overflow, padding,
60
60
  function calculateMargins(spaces) {
61
61
  let marginLeft;
62
62
  let marginRight;
63
+ if (spaces <= 0 || Number.isNaN(spaces)) {
64
+ return { marginLeft: 0, marginRight: 0 };
65
+ }
63
66
  if (horizontalAlignment === 'left') {
64
67
  marginLeft = padding;
65
68
  marginRight = spaces - marginLeft;
@@ -72,7 +75,11 @@ export function formatTextWithMargins({ horizontalAlignment, overflow, padding,
72
75
  marginRight = padding;
73
76
  marginLeft = spaces - marginRight;
74
77
  }
75
- return { marginLeft, marginRight };
78
+ return {
79
+ // Ensure that the margin is never negative
80
+ marginLeft: Math.max(0, marginLeft),
81
+ marginRight: Math.max(0, marginRight),
82
+ };
76
83
  }
77
84
  // Some terminals don't play nicely with zero-width characters, so we replace them with spaces.
78
85
  // https://github.com/sindresorhus/terminal-link/issues/18
@@ -87,7 +94,11 @@ export function formatTextWithMargins({ horizontalAlignment, overflow, padding,
87
94
  };
88
95
  }
89
96
  if (overflow === 'wrap') {
90
- const wrappedText = wrapAnsi(valueWithNoZeroWidthChars, spaceForText, { hard: true, trim: true, wordWrap: true });
97
+ const wrappedText = wrapAnsi(valueWithNoZeroWidthChars, spaceForText, {
98
+ hard: true,
99
+ trim: true,
100
+ wordWrap: true,
101
+ }).replace(/^\n/, ''); // remove leading newline (wrapAnsi adds it to emojis)
91
102
  const { marginLeft, marginRight } = calculateMargins(width - determineWidthOfWrappedText(stripAnsi(wrappedText)));
92
103
  const lines = wrappedText.split('\n').map((line, idx) => {
93
104
  const { marginLeft: lineSpecificLeftMargin, marginRight: lineSpecificRightMargin } = calculateMargins(width - stripAnsi(line).length);
@@ -306,7 +317,7 @@ const createStdout = () => {
306
317
  const stripped = stripAnsi(f);
307
318
  return stripped !== '' && stripped !== '\n';
308
319
  })
309
- .at(-1);
320
+ .at(-1) ?? '';
310
321
  return stdout;
311
322
  };
312
323
  class Output {
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
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.2.0",
4
+ "version": "0.2.2",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/table/issues",
7
7
  "dependencies": {