@cjser/cli-truncate 6.0.0-cjser.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/dist-cjser/index.cjs +160 -0
- package/index.d.ts +118 -0
- package/index.js +168 -0
- package/license +9 -0
- package/package.json +83 -0
- package/readme.md +158 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// packages/@cjser/cli-truncate/index.js
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
default: () => cliTruncate
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
var import_slice_ansi = __toESM(require("@cjser/slice-ansi"), 1);
|
|
36
|
+
var import_string_width = __toESM(require("@cjser/string-width"), 1);
|
|
37
|
+
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
38
|
+
if (string.charAt(wantedIndex) === " ") {
|
|
39
|
+
return wantedIndex;
|
|
40
|
+
}
|
|
41
|
+
const direction = shouldSearchRight ? 1 : -1;
|
|
42
|
+
for (let index = 0; index <= 3; index++) {
|
|
43
|
+
const finalIndex = wantedIndex + index * direction;
|
|
44
|
+
if (string.charAt(finalIndex) === " ") {
|
|
45
|
+
return finalIndex;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return wantedIndex;
|
|
49
|
+
}
|
|
50
|
+
function cliTruncate(text, columns, options = {}) {
|
|
51
|
+
const {
|
|
52
|
+
position = "end",
|
|
53
|
+
space = false,
|
|
54
|
+
preferTruncationOnSpace = false
|
|
55
|
+
} = options;
|
|
56
|
+
let { truncationCharacter = "\u2026" } = options;
|
|
57
|
+
if (typeof text !== "string") {
|
|
58
|
+
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
59
|
+
}
|
|
60
|
+
if (typeof columns !== "number") {
|
|
61
|
+
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
|
|
62
|
+
}
|
|
63
|
+
if (columns < 1) {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
const length = (0, import_string_width.default)(text);
|
|
67
|
+
if (length <= columns) {
|
|
68
|
+
return text;
|
|
69
|
+
}
|
|
70
|
+
if (columns === 1) {
|
|
71
|
+
return truncationCharacter;
|
|
72
|
+
}
|
|
73
|
+
const ANSI = {
|
|
74
|
+
ESC: 27,
|
|
75
|
+
LEFT_BRACKET: 91,
|
|
76
|
+
LETTER_M: 109
|
|
77
|
+
};
|
|
78
|
+
const isSgrParameter = (code) => code >= 48 && code <= 57 || code === 59;
|
|
79
|
+
function leadingSgrSpanEndIndex(string) {
|
|
80
|
+
let index = 0;
|
|
81
|
+
while (index + 2 < string.length && string.codePointAt(index) === ANSI.ESC && string.codePointAt(index + 1) === ANSI.LEFT_BRACKET) {
|
|
82
|
+
let j = index + 2;
|
|
83
|
+
while (j < string.length && isSgrParameter(string.codePointAt(j))) {
|
|
84
|
+
j++;
|
|
85
|
+
}
|
|
86
|
+
if (j < string.length && string.codePointAt(j) === ANSI.LETTER_M) {
|
|
87
|
+
index = j + 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
return index;
|
|
93
|
+
}
|
|
94
|
+
function trailingSgrSpanStartIndex(string) {
|
|
95
|
+
let start = string.length;
|
|
96
|
+
while (start > 1 && string.codePointAt(start - 1) === ANSI.LETTER_M) {
|
|
97
|
+
let j = start - 2;
|
|
98
|
+
while (j >= 0 && isSgrParameter(string.codePointAt(j))) {
|
|
99
|
+
j--;
|
|
100
|
+
}
|
|
101
|
+
if (j >= 1 && string.codePointAt(j - 1) === ANSI.ESC && string.codePointAt(j) === ANSI.LEFT_BRACKET) {
|
|
102
|
+
start = j - 1;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
return start;
|
|
108
|
+
}
|
|
109
|
+
function appendWithInheritedStyleFromEnd(visible, suffix) {
|
|
110
|
+
const start = trailingSgrSpanStartIndex(visible);
|
|
111
|
+
if (start === visible.length) {
|
|
112
|
+
return visible + suffix;
|
|
113
|
+
}
|
|
114
|
+
return visible.slice(0, start) + suffix + visible.slice(start);
|
|
115
|
+
}
|
|
116
|
+
function prependWithInheritedStyleFromStart(prefix, visible) {
|
|
117
|
+
const end = leadingSgrSpanEndIndex(visible);
|
|
118
|
+
if (end === 0) {
|
|
119
|
+
return prefix + visible;
|
|
120
|
+
}
|
|
121
|
+
return visible.slice(0, end) + prefix + visible.slice(end);
|
|
122
|
+
}
|
|
123
|
+
if (position === "start") {
|
|
124
|
+
if (preferTruncationOnSpace) {
|
|
125
|
+
const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
|
|
126
|
+
const right2 = (0, import_slice_ansi.default)(text, nearestSpace, length).trim();
|
|
127
|
+
return prependWithInheritedStyleFromStart(truncationCharacter, right2);
|
|
128
|
+
}
|
|
129
|
+
if (space) {
|
|
130
|
+
truncationCharacter += " ";
|
|
131
|
+
}
|
|
132
|
+
const right = (0, import_slice_ansi.default)(text, length - columns + (0, import_string_width.default)(truncationCharacter), length);
|
|
133
|
+
return prependWithInheritedStyleFromStart(truncationCharacter, right);
|
|
134
|
+
}
|
|
135
|
+
if (position === "middle") {
|
|
136
|
+
if (space) {
|
|
137
|
+
truncationCharacter = ` ${truncationCharacter} `;
|
|
138
|
+
}
|
|
139
|
+
const half = Math.floor(columns / 2);
|
|
140
|
+
if (preferTruncationOnSpace) {
|
|
141
|
+
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
|
142
|
+
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
|
|
143
|
+
return (0, import_slice_ansi.default)(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + (0, import_slice_ansi.default)(text, spaceNearSecondBreakPoint, length).trim();
|
|
144
|
+
}
|
|
145
|
+
return (0, import_slice_ansi.default)(text, 0, half) + truncationCharacter + (0, import_slice_ansi.default)(text, length - (columns - half) + (0, import_string_width.default)(truncationCharacter), length);
|
|
146
|
+
}
|
|
147
|
+
if (position === "end") {
|
|
148
|
+
if (preferTruncationOnSpace) {
|
|
149
|
+
const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
|
|
150
|
+
const left2 = (0, import_slice_ansi.default)(text, 0, nearestSpace);
|
|
151
|
+
return appendWithInheritedStyleFromEnd(left2, truncationCharacter);
|
|
152
|
+
}
|
|
153
|
+
if (space) {
|
|
154
|
+
truncationCharacter = ` ${truncationCharacter}`;
|
|
155
|
+
}
|
|
156
|
+
const left = (0, import_slice_ansi.default)(text, 0, columns - (0, import_string_width.default)(truncationCharacter));
|
|
157
|
+
return appendWithInheritedStyleFromEnd(left, truncationCharacter);
|
|
158
|
+
}
|
|
159
|
+
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
|
160
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export type Options = {
|
|
2
|
+
/**
|
|
3
|
+
The position to truncate the string.
|
|
4
|
+
|
|
5
|
+
@default 'end'
|
|
6
|
+
*/
|
|
7
|
+
readonly position?: 'start' | 'middle' | 'end';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
Add a space between the text and the ellipsis.
|
|
11
|
+
|
|
12
|
+
@default false
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import cliTruncate from '@cjser/cli-truncate';
|
|
17
|
+
|
|
18
|
+
cliTruncate('unicorns', 5, {position: 'end', space: true});
|
|
19
|
+
//=> 'uni …'
|
|
20
|
+
|
|
21
|
+
cliTruncate('unicorns', 5, {position: 'end', space: false});
|
|
22
|
+
//=> 'unic…'
|
|
23
|
+
|
|
24
|
+
cliTruncate('unicorns', 6, {position: 'start', space: true});
|
|
25
|
+
//=> '… orns'
|
|
26
|
+
|
|
27
|
+
cliTruncate('unicorns', 7, {position: 'middle', space: true});
|
|
28
|
+
//=> 'uni … s'
|
|
29
|
+
```
|
|
30
|
+
*/
|
|
31
|
+
readonly space?: boolean;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
|
|
35
|
+
|
|
36
|
+
@default false
|
|
37
|
+
|
|
38
|
+
@example
|
|
39
|
+
```
|
|
40
|
+
import cliTruncate from '@cjser/cli-truncate';
|
|
41
|
+
|
|
42
|
+
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
|
|
43
|
+
//=> '…rainbow dragons'
|
|
44
|
+
|
|
45
|
+
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
|
|
46
|
+
//=> 'unicorns…dragons'
|
|
47
|
+
|
|
48
|
+
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
|
|
49
|
+
//=> 'unico…'
|
|
50
|
+
````
|
|
51
|
+
*/
|
|
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 '@cjser/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;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
Truncate a string to a specific width in the terminal.
|
|
77
|
+
|
|
78
|
+
@param text - The text to truncate.
|
|
79
|
+
@param columns - The number of columns to occupy in the terminal.
|
|
80
|
+
|
|
81
|
+
@example
|
|
82
|
+
```
|
|
83
|
+
import cliTruncate from '@cjser/cli-truncate';
|
|
84
|
+
|
|
85
|
+
cliTruncate('unicorn', 4);
|
|
86
|
+
//=> 'uni…'
|
|
87
|
+
|
|
88
|
+
// Truncate at different positions
|
|
89
|
+
cliTruncate('unicorn', 4, {position: 'start'});
|
|
90
|
+
//=> '…orn'
|
|
91
|
+
|
|
92
|
+
cliTruncate('unicorn', 4, {position: 'middle'});
|
|
93
|
+
//=> 'un…n'
|
|
94
|
+
|
|
95
|
+
cliTruncate('\u001B[31municorn\u001B[39m', 4);
|
|
96
|
+
//=> '\u001B[31muni…\u001B[39m'
|
|
97
|
+
|
|
98
|
+
// Note: When truncating styled text (ANSI escapes), the truncation character inherits the style at the breaking point for `position: 'start'` and `position: 'end'`. This does not apply to `position: 'middle'`.
|
|
99
|
+
|
|
100
|
+
// Truncate Unicode surrogate pairs
|
|
101
|
+
cliTruncate('uni\uD83C\uDE00corn', 5);
|
|
102
|
+
//=> 'uni\uD83C\uDE00…'
|
|
103
|
+
|
|
104
|
+
// Truncate fullwidth characters
|
|
105
|
+
cliTruncate('안녕하세요', 3);
|
|
106
|
+
//=> '안…'
|
|
107
|
+
|
|
108
|
+
// Truncate the paragraph to the terminal width
|
|
109
|
+
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
110
|
+
cliTruncate(paragraph, process.stdout.columns);
|
|
111
|
+
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
112
|
+
```
|
|
113
|
+
*/
|
|
114
|
+
export default function cliTruncate(
|
|
115
|
+
text: string,
|
|
116
|
+
columns: number,
|
|
117
|
+
options?: Options,
|
|
118
|
+
): string;
|
package/index.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import sliceAnsi from '@cjser/slice-ansi';
|
|
2
|
+
import stringWidth from '@cjser/string-width';
|
|
3
|
+
|
|
4
|
+
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
5
|
+
if (string.charAt(wantedIndex) === ' ') {
|
|
6
|
+
return wantedIndex;
|
|
7
|
+
}
|
|
8
|
+
|
|
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;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return wantedIndex;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default function cliTruncate(text, columns, options = {}) {
|
|
22
|
+
const {
|
|
23
|
+
position = 'end',
|
|
24
|
+
space = false,
|
|
25
|
+
preferTruncationOnSpace = false,
|
|
26
|
+
} = options;
|
|
27
|
+
|
|
28
|
+
let {truncationCharacter = '…'} = options;
|
|
29
|
+
|
|
30
|
+
if (typeof text !== 'string') {
|
|
31
|
+
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (typeof columns !== 'number') {
|
|
35
|
+
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (columns < 1) {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const length = stringWidth(text);
|
|
43
|
+
|
|
44
|
+
if (length <= columns) {
|
|
45
|
+
return text;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (columns === 1) {
|
|
49
|
+
return truncationCharacter;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ANSI escape sequence constants
|
|
53
|
+
const ANSI = {
|
|
54
|
+
ESC: 27,
|
|
55
|
+
LEFT_BRACKET: 91,
|
|
56
|
+
LETTER_M: 109,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const isSgrParameter = code => (code >= 48 && code <= 57) || code === 59; // 0-9 or ;
|
|
60
|
+
|
|
61
|
+
function leadingSgrSpanEndIndex(string) {
|
|
62
|
+
let index = 0;
|
|
63
|
+
while (index + 2 < string.length && string.codePointAt(index) === ANSI.ESC && string.codePointAt(index + 1) === ANSI.LEFT_BRACKET) {
|
|
64
|
+
let j = index + 2;
|
|
65
|
+
while (j < string.length && isSgrParameter(string.codePointAt(j))) {
|
|
66
|
+
j++;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (j < string.length && string.codePointAt(j) === ANSI.LETTER_M) {
|
|
70
|
+
index = j + 1;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return index;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function trailingSgrSpanStartIndex(string) {
|
|
81
|
+
let start = string.length;
|
|
82
|
+
while (start > 1 && string.codePointAt(start - 1) === ANSI.LETTER_M) {
|
|
83
|
+
let j = start - 2;
|
|
84
|
+
while (j >= 0 && isSgrParameter(string.codePointAt(j))) {
|
|
85
|
+
j--;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (j >= 1 && string.codePointAt(j - 1) === ANSI.ESC && string.codePointAt(j) === ANSI.LEFT_BRACKET) {
|
|
89
|
+
start = j - 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return start;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function appendWithInheritedStyleFromEnd(visible, suffix) {
|
|
100
|
+
const start = trailingSgrSpanStartIndex(visible);
|
|
101
|
+
if (start === visible.length) {
|
|
102
|
+
return visible + suffix;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return visible.slice(0, start) + suffix + visible.slice(start);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function prependWithInheritedStyleFromStart(prefix, visible) {
|
|
109
|
+
const end = leadingSgrSpanEndIndex(visible);
|
|
110
|
+
if (end === 0) {
|
|
111
|
+
return prefix + visible;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return visible.slice(0, end) + prefix + visible.slice(end);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (position === 'start') {
|
|
118
|
+
if (preferTruncationOnSpace) {
|
|
119
|
+
const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
|
|
120
|
+
const right = sliceAnsi(text, nearestSpace, length).trim();
|
|
121
|
+
return prependWithInheritedStyleFromStart(truncationCharacter, right);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (space) {
|
|
125
|
+
truncationCharacter += ' ';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const right = sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
|
|
129
|
+
return prependWithInheritedStyleFromStart(truncationCharacter, right);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (position === 'middle') {
|
|
133
|
+
if (space) {
|
|
134
|
+
truncationCharacter = ` ${truncationCharacter} `;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const half = Math.floor(columns / 2);
|
|
138
|
+
|
|
139
|
+
if (preferTruncationOnSpace) {
|
|
140
|
+
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
|
141
|
+
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
|
|
142
|
+
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
sliceAnsi(text, 0, half)
|
|
147
|
+
+ truncationCharacter
|
|
148
|
+
+ sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length)
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (position === 'end') {
|
|
153
|
+
if (preferTruncationOnSpace) {
|
|
154
|
+
const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
|
|
155
|
+
const left = sliceAnsi(text, 0, nearestSpace);
|
|
156
|
+
return appendWithInheritedStyleFromEnd(left, truncationCharacter);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (space) {
|
|
160
|
+
truncationCharacter = ` ${truncationCharacter}`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const left = sliceAnsi(text, 0, columns - stringWidth(truncationCharacter));
|
|
164
|
+
return appendWithInheritedStyleFromEnd(left, truncationCharacter);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
|
168
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cjser/cli-truncate",
|
|
3
|
+
"version": "6.0.0-cjser.2",
|
|
4
|
+
"description": "Truncate a string to a specific width in the terminal",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://code.moenext.com/3rdeye/cjser.git"
|
|
9
|
+
},
|
|
10
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Sindre Sorhus",
|
|
13
|
+
"email": "sindresorhus@gmail.com",
|
|
14
|
+
"url": "https://sindresorhus.com"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"require": "./dist-cjser/index.cjs",
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "xo && ava && tsd"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"index.js",
|
|
31
|
+
"index.d.ts",
|
|
32
|
+
"dist-cjser"
|
|
33
|
+
],
|
|
34
|
+
"keywords": [
|
|
35
|
+
"truncate",
|
|
36
|
+
"ellipsis",
|
|
37
|
+
"text",
|
|
38
|
+
"limit",
|
|
39
|
+
"slice",
|
|
40
|
+
"cli",
|
|
41
|
+
"terminal",
|
|
42
|
+
"term",
|
|
43
|
+
"shell",
|
|
44
|
+
"width",
|
|
45
|
+
"ansi",
|
|
46
|
+
"string"
|
|
47
|
+
],
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@cjser/slice-ansi": "9.0.0-cjser.2",
|
|
50
|
+
"@cjser/string-width": "8.2.1-cjser.2"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"ava": "^7.0.0",
|
|
54
|
+
"tsd": "^0.33.0",
|
|
55
|
+
"xo": "^2.0.2"
|
|
56
|
+
},
|
|
57
|
+
"types": "./index.d.ts",
|
|
58
|
+
"main": "./dist-cjser/index.cjs",
|
|
59
|
+
"cjser": {
|
|
60
|
+
"sourceVersion": "6.0.0",
|
|
61
|
+
"cjserVersion": 2,
|
|
62
|
+
"original": {
|
|
63
|
+
"name": "cli-truncate",
|
|
64
|
+
"version": "6.0.0",
|
|
65
|
+
"exports": {
|
|
66
|
+
"types": "./index.d.ts",
|
|
67
|
+
"default": "./index.js"
|
|
68
|
+
},
|
|
69
|
+
"repository": "sindresorhus/cli-truncate",
|
|
70
|
+
"dependencies": {
|
|
71
|
+
"slice-ansi": "^9.0.0",
|
|
72
|
+
"string-width": "^8.2.0"
|
|
73
|
+
},
|
|
74
|
+
"files": [
|
|
75
|
+
"index.js",
|
|
76
|
+
"index.d.ts"
|
|
77
|
+
],
|
|
78
|
+
"scripts": {
|
|
79
|
+
"test": "xo && ava && tsd"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# cli-truncate
|
|
2
|
+
|
|
3
|
+
> Truncate a string to a specific width in the terminal
|
|
4
|
+
|
|
5
|
+
Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk). It also supports Unicode surrogate pairs and fullwidth characters.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install cli-truncate
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import cliTruncate from 'cli-truncate';
|
|
17
|
+
|
|
18
|
+
cliTruncate('unicorn', 4);
|
|
19
|
+
//=> 'uni…'
|
|
20
|
+
|
|
21
|
+
// Truncate at different positions
|
|
22
|
+
cliTruncate('unicorn', 4, {position: 'start'});
|
|
23
|
+
//=> '…orn'
|
|
24
|
+
|
|
25
|
+
cliTruncate('unicorn', 4, {position: 'middle'});
|
|
26
|
+
//=> 'un…n'
|
|
27
|
+
|
|
28
|
+
cliTruncate('unicorns rainbow dragons', 6, {position: 'end'});
|
|
29
|
+
//=> 'unico…'
|
|
30
|
+
|
|
31
|
+
cliTruncate('\u001B[31municorn\u001B[39m', 4);
|
|
32
|
+
//=> '\u001B[31muni…\u001B[39m'
|
|
33
|
+
|
|
34
|
+
> [!NOTE]
|
|
35
|
+
> When truncating styled text (ANSI escapes), the truncation character inherits the style at the breaking point for `position: 'start'` and `position: 'end'`. This does not apply to `position: 'middle'`.
|
|
36
|
+
|
|
37
|
+
// Truncate Unicode surrogate pairs
|
|
38
|
+
cliTruncate('uni\uD83C\uDE00corn', 5);
|
|
39
|
+
//=> 'uni\uD83C\uDE00…'
|
|
40
|
+
|
|
41
|
+
// Truncate fullwidth characters
|
|
42
|
+
cliTruncate('안녕하세요', 3);
|
|
43
|
+
//=> '안…'
|
|
44
|
+
|
|
45
|
+
// Truncate the paragraph to the terminal width
|
|
46
|
+
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
47
|
+
cliTruncate(paragraph, process.stdout.columns);
|
|
48
|
+
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
### cliTruncate(text, columns, options?)
|
|
54
|
+
|
|
55
|
+
#### text
|
|
56
|
+
|
|
57
|
+
Type: `string`
|
|
58
|
+
|
|
59
|
+
The text to truncate.
|
|
60
|
+
|
|
61
|
+
#### columns
|
|
62
|
+
|
|
63
|
+
Type: `number`
|
|
64
|
+
|
|
65
|
+
The number of columns to occupy in the terminal.
|
|
66
|
+
|
|
67
|
+
#### options
|
|
68
|
+
|
|
69
|
+
Type: `object`
|
|
70
|
+
|
|
71
|
+
##### position
|
|
72
|
+
|
|
73
|
+
Type: `string`\
|
|
74
|
+
Default: `'end'`\
|
|
75
|
+
Values: `'start' | 'middle' | 'end'`
|
|
76
|
+
|
|
77
|
+
The position to truncate the string.
|
|
78
|
+
|
|
79
|
+
##### space
|
|
80
|
+
|
|
81
|
+
Type: `boolean`\
|
|
82
|
+
Default: `false`
|
|
83
|
+
|
|
84
|
+
Add a space between the text and the ellipsis.
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import cliTruncate from 'cli-truncate';
|
|
88
|
+
|
|
89
|
+
cliTruncate('unicorns', 5, {space: false});
|
|
90
|
+
//=> 'unic…'
|
|
91
|
+
|
|
92
|
+
cliTruncate('unicorns', 5, {space: true});
|
|
93
|
+
//=> 'uni …'
|
|
94
|
+
|
|
95
|
+
cliTruncate('unicorns', 6, {position: 'start', space: true});
|
|
96
|
+
//=> '… orns'
|
|
97
|
+
|
|
98
|
+
cliTruncate('unicorns', 7, {position: 'middle', space: true});
|
|
99
|
+
//=> 'uni … s'
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
##### preferTruncationOnSpace
|
|
103
|
+
|
|
104
|
+
Type: `boolean`\
|
|
105
|
+
Default: `false`
|
|
106
|
+
|
|
107
|
+
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
import cliTruncate from 'cli-truncate';
|
|
111
|
+
|
|
112
|
+
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
|
|
113
|
+
//=> '…rainbow dragons'
|
|
114
|
+
|
|
115
|
+
// Without preferTruncationOnSpace
|
|
116
|
+
cliTruncate('unicorns rainbow dragons', 20, {position: 'start'});
|
|
117
|
+
//=> '…rns rainbow dragons'
|
|
118
|
+
|
|
119
|
+
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
|
|
120
|
+
//=> 'unicorns…dragons'
|
|
121
|
+
|
|
122
|
+
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
|
|
123
|
+
//=> 'unico…'
|
|
124
|
+
|
|
125
|
+
// preferTruncationOnSpace has no effect if space isn't found within 3 characters
|
|
126
|
+
cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true});
|
|
127
|
+
//=> 'uni…ns'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
##### truncationCharacter
|
|
131
|
+
|
|
132
|
+
Type: `string`\
|
|
133
|
+
Default: `…`
|
|
134
|
+
|
|
135
|
+
The character to use at the breaking point.
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
import cliTruncate from 'cli-truncate';
|
|
139
|
+
|
|
140
|
+
cliTruncate('unicorns', 5, {position: 'end'});
|
|
141
|
+
//=> 'unic…'
|
|
142
|
+
|
|
143
|
+
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
|
|
144
|
+
//=> 'unic.'
|
|
145
|
+
|
|
146
|
+
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
|
|
147
|
+
//=> 'unico'
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Related
|
|
151
|
+
|
|
152
|
+
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
|
153
|
+
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
|
154
|
+
|
|
155
|
+
## cjser
|
|
156
|
+
|
|
157
|
+
This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
|
|
158
|
+
Original repository: https://github.com/sindresorhus/cli-truncate
|