You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.d.ts 2.7KB

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