@oclif/table 0.1.12 → 0.1.13

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.d.ts CHANGED
@@ -1,5 +1,16 @@
1
1
  import React from 'react';
2
- import { CellProps, ContainerProps, TableOptions } from './types.js';
2
+ import { CellProps, ContainerProps, HorizontalAlignment, Overflow, TableOptions } from './types.js';
3
+ export declare function formatTextWithMargins({ horizontalAlignment, overflow, padding, value, width, }: {
4
+ overflow: Overflow;
5
+ value: unknown;
6
+ width: number;
7
+ padding: number;
8
+ horizontalAlignment: HorizontalAlignment;
9
+ }): {
10
+ text: string;
11
+ marginLeft: number;
12
+ marginRight: number;
13
+ };
3
14
  export declare function Table<T extends Record<string, unknown>>(props: TableOptions<T>): React.JSX.Element;
4
15
  /**
5
16
  * Renders the header of a table.
package/lib/table.js CHANGED
@@ -6,7 +6,7 @@ import React from 'react';
6
6
  import stripAnsi from 'strip-ansi';
7
7
  import wrapAnsi from 'wrap-ansi';
8
8
  import { BORDER_SKELETONS } from './skeletons.js';
9
- import { allKeysInCollection, getColumns, getHeadings, intersperse, maybeStripAnsi, sortData } from './utils.js';
9
+ import { allKeysInCollection, determineWidthOfWrappedText, getColumns, getHeadings, intersperse, maybeStripAnsi, sortData, } from './utils.js';
10
10
  /**
11
11
  * Determines the configured width based on the provided width value.
12
12
  * If no width is provided, it returns the width of the current terminal.
@@ -39,10 +39,6 @@ function determineWidthToUse(columns, configuredWidth) {
39
39
  const tableWidth = columns.map((c) => c.width).reduce((a, b) => a + b) + columns.length + 1;
40
40
  return tableWidth < configuredWidth ? configuredWidth : tableWidth;
41
41
  }
42
- function determineWidthOfWrappedText(text) {
43
- const lines = text.split('\n');
44
- return lines.reduce((max, line) => Math.max(max, line.length), 0);
45
- }
46
42
  function determineTruncatePosition(overflow) {
47
43
  switch (overflow) {
48
44
  case 'truncate-middle': {
@@ -59,7 +55,7 @@ function determineTruncatePosition(overflow) {
59
55
  }
60
56
  }
61
57
  }
62
- function formatTextWithMargins({ horizontalAlignment, overflow, padding, value, width, }) {
58
+ export function formatTextWithMargins({ horizontalAlignment, overflow, padding, value, width, }) {
63
59
  function calculateMargins(spaces) {
64
60
  let marginLeft;
65
61
  let marginRight;
@@ -93,28 +89,21 @@ function formatTextWithMargins({ horizontalAlignment, overflow, padding, value,
93
89
  const wrappedText = wrapAnsi(valueWithNoZeroWidthChars, spaceForText, { hard: true, trim: true, wordWrap: true });
94
90
  const { marginLeft, marginRight } = calculateMargins(width - determineWidthOfWrappedText(stripAnsi(wrappedText)));
95
91
  const lines = wrappedText.split('\n').map((line, idx) => {
96
- const { marginLeft: lineSpecificLeftMargin } = calculateMargins(width - stripAnsi(line).length);
92
+ const { marginLeft: lineSpecificLeftMargin, marginRight: lineSpecificRightMargin } = calculateMargins(width - stripAnsi(line).length);
97
93
  if (horizontalAlignment === 'left') {
98
- if (idx === 0) {
99
- // if it's the first line, only add margin to the right side (The left margin will be applied later)
100
- return `${line}${' '.repeat(marginRight)}`;
101
- }
102
- // if left alignment, add the overall margin to the left side and right sides
103
- return `${' '.repeat(marginLeft)}${line}${' '.repeat(marginRight)}`;
94
+ return idx === 0
95
+ ? `${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`
96
+ : `${' '.repeat(marginLeft)}${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`;
104
97
  }
105
98
  if (horizontalAlignment === 'center') {
106
- if (idx === 0) {
107
- // if it's the first line, only add margin to the right side (The left margin will be applied later)
108
- return `${line}${' '.repeat(marginRight)}`;
109
- }
110
- // if center alignment, add line specific margin to the left side and the overall margin to the right side
111
- return `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(marginRight)}`;
99
+ return idx === 0
100
+ ? `${' '.repeat(lineSpecificLeftMargin - marginLeft)}${line}${' '.repeat(lineSpecificRightMargin)}`
101
+ : `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`;
112
102
  }
113
103
  // right alignment
114
- if (idx === 0) {
115
- return `${' '.repeat(Math.max(0, lineSpecificLeftMargin - marginLeft))}${line}${' '.repeat(marginRight)}`;
116
- }
117
- return `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(marginRight)}`;
104
+ return idx === 0
105
+ ? `${' '.repeat(Math.max(0, lineSpecificLeftMargin - marginLeft))}${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`
106
+ : `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`;
118
107
  });
119
108
  return {
120
109
  marginLeft,
@@ -122,7 +111,9 @@ function formatTextWithMargins({ horizontalAlignment, overflow, padding, value,
122
111
  text: lines.join('\n'),
123
112
  };
124
113
  }
125
- const text = cliTruncate(valueWithNoZeroWidthChars, spaceForText, { position: determineTruncatePosition(overflow) });
114
+ const text = cliTruncate(valueWithNoZeroWidthChars.replaceAll('\n', ' '), spaceForText, {
115
+ position: determineTruncatePosition(overflow),
116
+ });
126
117
  const spaces = width - stripAnsi(text).length;
127
118
  return {
128
119
  text,
package/lib/utils.d.ts CHANGED
@@ -10,6 +10,7 @@ import { Column, Config, Sort } from './types.js';
10
10
  export declare function intersperse<T, I>(intersperser: (index: number) => I, elements: T[]): (T | I)[];
11
11
  export declare function sortData<T extends Record<string, unknown>>(data: T[], sort?: Sort<T> | undefined): T[];
12
12
  export declare function allKeysInCollection<T extends Record<string, unknown>>(data: T[]): (keyof T)[];
13
+ export declare function determineWidthOfWrappedText(text: string): number;
13
14
  export declare function getColumns<T extends Record<string, unknown>>(config: Config<T>, headings: Partial<T>): Column<T>[];
14
15
  export declare function getHeadings<T extends Record<string, unknown>>(config: Config<T>): Partial<T>;
15
16
  export declare function maybeStripAnsi<T extends Record<string, unknown>[]>(data: T, noStyle: boolean): T;
package/lib/utils.js CHANGED
@@ -37,6 +37,10 @@ export function allKeysInCollection(data) {
37
37
  }
38
38
  return [...keys];
39
39
  }
40
+ export function determineWidthOfWrappedText(text) {
41
+ const lines = text.split('\n');
42
+ return lines.reduce((max, line) => Math.max(max, line.length), 0);
43
+ }
40
44
  export function getColumns(config, headings) {
41
45
  const { columns, horizontalAlignment, maxWidth, overflow, verticalAlignment } = config;
42
46
  const widths = columns.map((propsOrKey) => {
@@ -48,7 +52,7 @@ export function getColumns(config, headings) {
48
52
  const value = data[key];
49
53
  if (value === undefined || value === null)
50
54
  return 0;
51
- return stripAnsi(String(value).replaceAll('​', ' ')).length;
55
+ return determineWidthOfWrappedText(stripAnsi(String(value).replaceAll('​', ' ')));
52
56
  });
53
57
  const header = String(headings[key]).length;
54
58
  const width = Math.max(...data, header) + padding * 2;
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.12",
4
+ "version": "0.1.13",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/table/issues",
7
7
  "dependencies": {