UNPKG

2.3 kBTypeScriptView Raw
1declare namespace cliTruncate {
2 interface Options {
3 /**
4 Position to truncate the string.
5
6 @default 'end'
7 */
8 readonly position?: 'start' | 'middle' | 'end';
9
10 /**
11 Add a space between the text and the ellipsis.
12
13 @default false
14
15 @example
16 ```
17 cliTruncate('unicorns', 5, {position: 'end', space: true});
18 //=> 'uni …'
19
20 cliTruncate('unicorns', 5, {position: 'end', space: false});
21 //=> 'unic…'
22
23 cliTruncate('unicorns', 6, {position: 'start', space: true});
24 //=> '… orns'
25
26 cliTruncate('unicorns', 7, {position: 'middle', space: true});
27 //=> 'uni … s'
28 ```
29 */
30 readonly space?: boolean;
31
32 /**
33 Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
34
35 @default false
36
37 @example
38 ```
39 cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
40 //=> '…rainbow dragons'
41
42 cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
43 //=> 'unicorns…dragons'
44
45 cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
46 //=> 'unico…'
47 ````
48 */
49 readonly preferTruncationOnSpace?: boolean;
50 }
51}
52
53/**
54Truncate a string to a specific width in the terminal.
55
56@param text - Text to truncate.
57@param columns - Columns to occupy in the terminal.
58
59@example
60```
61import cliTruncate = require('cli-truncate');
62
63cliTruncate('unicorn', 4);
64//=> 'uni…'
65
66// Truncate at different positions
67cliTruncate('unicorn', 4, {position: 'start'});
68//=> '…orn'
69
70cliTruncate('unicorn', 4, {position: 'middle'});
71//=> 'un…n'
72
73cliTruncate('\u001B[31municorn\u001B[39m', 4);
74//=> '\u001B[31muni\u001B[39m…'
75
76// Truncate Unicode surrogate pairs
77cliTruncate('uni\uD83C\uDE00corn', 5);
78//=> 'uni\uD83C\uDE00…'
79
80// Truncate fullwidth characters
81cliTruncate('안녕하세요', 3);
82//=> '안…'
83
84// Truncate the paragraph to the terminal width
85const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
86cliTruncate(paragraph, process.stdout.columns));
87//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
88```
89*/
90declare function cliTruncate(
91 text: string,
92 columns: number,
93 options?: cliTruncate.Options
94): string;
95
96export = cliTruncate;