index.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. declare namespace cliTruncate {
  2. interface Options {
  3. /**
  4. Position to truncate the string.
  5. @default 'end'
  6. */
  7. readonly position?: 'start' | 'middle' | 'end';
  8. /**
  9. Add a space between the text and the ellipsis.
  10. @default false
  11. @example
  12. ```
  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. cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
  30. //=> '…rainbow dragons'
  31. cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
  32. //=> 'unicorns…dragons'
  33. cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
  34. //=> 'unico…'
  35. ````
  36. */
  37. readonly preferTruncationOnSpace?: boolean;
  38. }
  39. }
  40. /**
  41. Truncate a string to a specific width in the terminal.
  42. @param text - Text to truncate.
  43. @param columns - Columns to occupy in the terminal.
  44. @example
  45. ```
  46. import cliTruncate = require('cli-truncate');
  47. cliTruncate('unicorn', 4);
  48. //=> 'uni…'
  49. // Truncate at different positions
  50. cliTruncate('unicorn', 4, {position: 'start'});
  51. //=> '…orn'
  52. cliTruncate('unicorn', 4, {position: 'middle'});
  53. //=> 'un…n'
  54. cliTruncate('\u001B[31municorn\u001B[39m', 4);
  55. //=> '\u001B[31muni\u001B[39m…'
  56. // Truncate Unicode surrogate pairs
  57. cliTruncate('uni\uD83C\uDE00corn', 5);
  58. //=> 'uni\uD83C\uDE00…'
  59. // Truncate fullwidth characters
  60. cliTruncate('안녕하세요', 3);
  61. //=> '안…'
  62. // Truncate the paragraph to the terminal width
  63. const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
  64. cliTruncate(paragraph, process.stdout.columns));
  65. //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
  66. ```
  67. */
  68. declare function cliTruncate(
  69. text: string,
  70. columns: number,
  71. options?: cliTruncate.Options
  72. ): string;
  73. export = cliTruncate;