cli-truncate 3.0.0 → 3.1.0

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.
Files changed (4) hide show
  1. package/index.d.ts +20 -0
  2. package/index.js +13 -16
  3. package/package.json +1 -1
  4. package/readme.md +20 -0
package/index.d.ts CHANGED
@@ -50,6 +50,26 @@ export interface Options {
50
50
  ````
51
51
  */
52
52
  readonly preferTruncationOnSpace?: boolean;
53
+
54
+ /**
55
+ The character to use at the breaking point.
56
+
57
+ @default '…'
58
+
59
+ @example
60
+ ```
61
+ import cliTruncate from 'cli-truncate';
62
+
63
+ cliTruncate('unicorns', 5, {position: 'end'});
64
+ //=> 'unic…'
65
+
66
+ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
67
+ //=> 'unic.'
68
+
69
+ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
70
+ //=> 'unico'
71
+ */
72
+ readonly truncationCharacter?: string;
53
73
  }
54
74
 
55
75
  /**
package/index.js CHANGED
@@ -23,12 +23,12 @@ export default function cliTruncate(text, columns, options) {
23
23
  options = {
24
24
  position: 'end',
25
25
  preferTruncationOnSpace: false,
26
+ truncationCharacter: '…',
26
27
  ...options,
27
28
  };
28
29
 
29
30
  const {position, space, preferTruncationOnSpace} = options;
30
- let ellipsis = '…';
31
- let ellipsisWidth = 1;
31
+ let {truncationCharacter} = options;
32
32
 
33
33
  if (typeof text !== 'string') {
34
34
  throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
@@ -43,7 +43,7 @@ export default function cliTruncate(text, columns, options) {
43
43
  }
44
44
 
45
45
  if (columns === 1) {
46
- return ellipsis;
46
+ return truncationCharacter;
47
47
  }
48
48
 
49
49
  const length = stringWidth(text);
@@ -55,21 +55,19 @@ export default function cliTruncate(text, columns, options) {
55
55
  if (position === 'start') {
56
56
  if (preferTruncationOnSpace) {
57
57
  const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
58
- return ellipsis + sliceAnsi(text, nearestSpace, length).trim();
58
+ return truncationCharacter + sliceAnsi(text, nearestSpace, length).trim();
59
59
  }
60
60
 
61
61
  if (space === true) {
62
- ellipsis += ' ';
63
- ellipsisWidth = 2;
62
+ truncationCharacter += ' ';
64
63
  }
65
64
 
66
- return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length);
65
+ return truncationCharacter + sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
67
66
  }
68
67
 
69
68
  if (position === 'middle') {
70
69
  if (space === true) {
71
- ellipsis = ` ${ellipsis} `;
72
- ellipsisWidth = 3;
70
+ truncationCharacter = ` ${truncationCharacter} `;
73
71
  }
74
72
 
75
73
  const half = Math.floor(columns / 2);
@@ -77,28 +75,27 @@ export default function cliTruncate(text, columns, options) {
77
75
  if (preferTruncationOnSpace) {
78
76
  const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
79
77
  const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
80
- return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + ellipsis + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
78
+ return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
81
79
  }
82
80
 
83
81
  return (
84
82
  sliceAnsi(text, 0, half)
85
- + ellipsis
86
- + sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
83
+ + truncationCharacter
84
+ + sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length)
87
85
  );
88
86
  }
89
87
 
90
88
  if (position === 'end') {
91
89
  if (preferTruncationOnSpace) {
92
90
  const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
93
- return sliceAnsi(text, 0, nearestSpace) + ellipsis;
91
+ return sliceAnsi(text, 0, nearestSpace) + truncationCharacter;
94
92
  }
95
93
 
96
94
  if (space === true) {
97
- ellipsis = ` ${ellipsis}`;
98
- ellipsisWidth = 2;
95
+ truncationCharacter = ` ${truncationCharacter}`;
99
96
  }
100
97
 
101
- return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
98
+ return sliceAnsi(text, 0, columns - stringWidth(truncationCharacter)) + truncationCharacter;
102
99
  }
103
100
 
104
101
  throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cli-truncate",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Truncate a string to a specific width in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/cli-truncate",
package/readme.md CHANGED
@@ -124,6 +124,26 @@ cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncation
124
124
  //=> 'uni…ns'
125
125
  ```
126
126
 
127
+ ##### truncationCharacter
128
+
129
+ Type: `string`\
130
+ Default: `…`
131
+
132
+ The character to use at the breaking point.
133
+
134
+ ```js
135
+ import cliTruncate from 'cli-truncate';
136
+
137
+ cliTruncate('unicorns', 5, {position: 'end'});
138
+ //=> 'unic…'
139
+
140
+ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
141
+ //=> 'unic.'
142
+
143
+ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
144
+ //=> 'unico'
145
+ ```
146
+
127
147
  ## Related
128
148
 
129
149
  - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes