|
| 1 | +declare 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 | + |
| 12 | +/** |
| 13 | +Truncate a string to a specific width in the terminal. |
| 14 | +
|
| 15 | +@param input - Text to truncate. |
| 16 | +@param columns - Columns to occupy in the terminal. |
| 17 | +
|
| 18 | +@example |
| 19 | +``` |
| 20 | +import cliTruncate = require('cli-truncate'); |
| 21 | +
|
| 22 | +cliTruncate('unicorn', 4); |
| 23 | +//=> 'uni…' |
| 24 | +
|
| 25 | +// Truncate at different positions |
| 26 | +cliTruncate('unicorn', 4, {position: 'start'}); |
| 27 | +//=> '…orn' |
| 28 | +
|
| 29 | +cliTruncate('unicorn', 4, {position: 'middle'}); |
| 30 | +//=> 'un…n' |
| 31 | +
|
| 32 | +cliTruncate('\u001B[31municorn\u001B[39m', 4); |
| 33 | +//=> '\u001B[31muni\u001B[39m…' |
| 34 | +
|
| 35 | +// Truncate Unicode surrogate pairs |
| 36 | +cliTruncate('uni\uD83C\uDE00corn', 5); |
| 37 | +//=> 'uni\uD83C\uDE00…' |
| 38 | +
|
| 39 | +// Truncate fullwidth characters |
| 40 | +cliTruncate('안녕하세요', 3); |
| 41 | +//=> '안…' |
| 42 | +
|
| 43 | +// Truncate the paragraph to the terminal width |
| 44 | +const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.'; |
| 45 | +cliTruncate(paragraph, process.stdout.columns)); |
| 46 | +//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…' |
| 47 | +``` |
| 48 | +*/ |
| 49 | +declare function cliTruncate( |
| 50 | + input: string, |
| 51 | + columns: number, |
| 52 | + options?: cliTruncate.Options |
| 53 | +): string; |
| 54 | + |
| 55 | +export = cliTruncate; |
0 commit comments