cli-truncate 3.1.0 → 5.0.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.
- package/index.d.ts +4 -4
- package/index.js +15 -18
- package/package.json +12 -8
- package/readme.md +5 -17
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Options = {
|
|
2
2
|
/**
|
|
3
3
|
The position to truncate the string.
|
|
4
4
|
|
|
@@ -70,12 +70,12 @@ export interface Options {
|
|
|
70
70
|
//=> 'unico'
|
|
71
71
|
*/
|
|
72
72
|
readonly truncationCharacter?: string;
|
|
73
|
-
}
|
|
73
|
+
};
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
Truncate a string to a specific width in the terminal.
|
|
77
77
|
|
|
78
|
-
@param text -
|
|
78
|
+
@param text - The text to truncate.
|
|
79
79
|
@param columns - The number of columns to occupy in the terminal.
|
|
80
80
|
|
|
81
81
|
@example
|
|
@@ -105,7 +105,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
105
105
|
|
|
106
106
|
// Truncate the paragraph to the terminal width
|
|
107
107
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
108
|
-
cliTruncate(paragraph, process.stdout.columns)
|
|
108
|
+
cliTruncate(paragraph, process.stdout.columns);
|
|
109
109
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
110
110
|
```
|
|
111
111
|
*/
|
package/index.js
CHANGED
|
@@ -6,29 +6,26 @@ function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
|
6
6
|
return wantedIndex;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return wantedIndex - index;
|
|
9
|
+
const direction = shouldSearchRight ? 1 : -1;
|
|
10
|
+
|
|
11
|
+
for (let index = 0; index <= 3; index++) {
|
|
12
|
+
const finalIndex = wantedIndex + (index * direction);
|
|
13
|
+
if (string.charAt(finalIndex) === ' ') {
|
|
14
|
+
return finalIndex;
|
|
16
15
|
}
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
return wantedIndex;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
export default function cliTruncate(text, columns, options) {
|
|
23
|
-
|
|
24
|
-
position
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
21
|
+
export default function cliTruncate(text, columns, options = {}) {
|
|
22
|
+
const {
|
|
23
|
+
position = 'end',
|
|
24
|
+
space = false,
|
|
25
|
+
preferTruncationOnSpace = false,
|
|
26
|
+
} = options;
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
let {truncationCharacter} = options;
|
|
28
|
+
let {truncationCharacter = '…'} = options;
|
|
32
29
|
|
|
33
30
|
if (typeof text !== 'string') {
|
|
34
31
|
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
@@ -80,8 +77,8 @@ export default function cliTruncate(text, columns, options) {
|
|
|
80
77
|
|
|
81
78
|
return (
|
|
82
79
|
sliceAnsi(text, 0, half)
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
+ truncationCharacter
|
|
81
|
+
+ sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length)
|
|
85
82
|
);
|
|
86
83
|
}
|
|
87
84
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-truncate",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Truncate a string to a specific width in the terminal",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/cli-truncate",
|
|
@@ -11,9 +11,13 @@
|
|
|
11
11
|
"url": "https://sindresorhus.com"
|
|
12
12
|
},
|
|
13
13
|
"type": "module",
|
|
14
|
-
"exports":
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
15
19
|
"engines": {
|
|
16
|
-
"node": "
|
|
20
|
+
"node": ">=20"
|
|
17
21
|
},
|
|
18
22
|
"scripts": {
|
|
19
23
|
"test": "xo && ava && tsd"
|
|
@@ -37,12 +41,12 @@
|
|
|
37
41
|
"string"
|
|
38
42
|
],
|
|
39
43
|
"dependencies": {
|
|
40
|
-
"slice-ansi": "^
|
|
41
|
-
"string-width": "^
|
|
44
|
+
"slice-ansi": "^7.1.0",
|
|
45
|
+
"string-width": "^8.0.0"
|
|
42
46
|
},
|
|
43
47
|
"devDependencies": {
|
|
44
|
-
"ava": "^
|
|
45
|
-
"tsd": "^0.
|
|
46
|
-
"xo": "^
|
|
48
|
+
"ava": "^6.4.1",
|
|
49
|
+
"tsd": "^0.33.0",
|
|
50
|
+
"xo": "^1.2.2"
|
|
47
51
|
}
|
|
48
52
|
}
|
package/readme.md
CHANGED
|
@@ -6,8 +6,8 @@ Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code
|
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
|
|
9
|
+
```sh
|
|
10
|
+
npm install cli-truncate
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
@@ -41,7 +41,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
41
41
|
|
|
42
42
|
// Truncate the paragraph to the terminal width
|
|
43
43
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
44
|
-
cliTruncate(paragraph, process.stdout.columns)
|
|
44
|
+
cliTruncate(paragraph, process.stdout.columns);
|
|
45
45
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
46
46
|
```
|
|
47
47
|
|
|
@@ -53,7 +53,7 @@ cliTruncate(paragraph, process.stdout.columns));
|
|
|
53
53
|
|
|
54
54
|
Type: `string`
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
The text to truncate.
|
|
57
57
|
|
|
58
58
|
#### columns
|
|
59
59
|
|
|
@@ -69,7 +69,7 @@ Type: `object`
|
|
|
69
69
|
|
|
70
70
|
Type: `string`\
|
|
71
71
|
Default: `'end'`\
|
|
72
|
-
Values: `'start'
|
|
72
|
+
Values: `'start' | 'middle' | 'end'`
|
|
73
73
|
|
|
74
74
|
The position to truncate the string.
|
|
75
75
|
|
|
@@ -148,15 +148,3 @@ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
|
|
|
148
148
|
|
|
149
149
|
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
|
150
150
|
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
|
151
|
-
|
|
152
|
-
---
|
|
153
|
-
|
|
154
|
-
<div align="center">
|
|
155
|
-
<b>
|
|
156
|
-
<a href="https://tidelift.com/subscription/pkg/npm-cli-truncate?utm_source=npm-cli-truncate&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|
157
|
-
</b>
|
|
158
|
-
<br>
|
|
159
|
-
<sub>
|
|
160
|
-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
161
|
-
</sub>
|
|
162
|
-
</div>
|