66f43d0b44dafed4dd0d6ea8d1ea25cc223c74fb71f0d9c18307452355080aa39cb6f1f2ddc3557104a85822ee4c9a2c7c4a30222b0585cad11234e9b1df89 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # cli-truncate [![Build Status](https://travis-ci.org/sindresorhus/cli-truncate.svg?branch=master)](https://travis-ci.org/sindresorhus/cli-truncate)
  2. > Truncate a string to a specific width in the terminal
  3. 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.
  4. ## Install
  5. ```
  6. $ npm install cli-truncate
  7. ```
  8. ## Usage
  9. ```js
  10. const cliTruncate = require('cli-truncate');
  11. cliTruncate('unicorn', 4);
  12. //=> 'uni…'
  13. // Truncate at different positions
  14. cliTruncate('unicorn', 4, {position: 'start'});
  15. //=> '…orn'
  16. cliTruncate('unicorn', 4, {position: 'middle'});
  17. //=> 'un…n'
  18. cliTruncate('unicorns rainbow dragons', 6, {position: 'end'})
  19. //=> 'unico…'
  20. cliTruncate('\u001B[31municorn\u001B[39m', 4);
  21. //=> '\u001B[31muni\u001B[39m…'
  22. // Truncate Unicode surrogate pairs
  23. cliTruncate('uni\uD83C\uDE00corn', 5);
  24. //=> 'uni\uD83C\uDE00…'
  25. // Truncate fullwidth characters
  26. cliTruncate('안녕하세요', 3);
  27. //=> '안…'
  28. // Truncate the paragraph to the terminal width
  29. const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
  30. cliTruncate(paragraph, process.stdout.columns));
  31. //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
  32. ```
  33. ## API
  34. ### cliTruncate(text, columns, options?)
  35. #### text
  36. Type: `string`
  37. Text to truncate.
  38. #### columns
  39. Type: `number`
  40. Columns to occupy in the terminal.
  41. #### options
  42. Type: `object`
  43. ##### position
  44. Type: `string`\
  45. Default: `'end'`\
  46. Values: `'start'` `'middle'` `'end'`
  47. Position to truncate the string.
  48. ##### space
  49. Type: `boolean`\
  50. Default: `false`
  51. Add a space between the text and the ellipsis.
  52. ```js
  53. cliTruncate('unicorns', 5, {space: false});
  54. //=> 'unic…'
  55. cliTruncate('unicorns', 5, {space: true});
  56. //=> 'uni …'
  57. cliTruncate('unicorns', 6, {position: 'start', space: true});
  58. //=> '… orns'
  59. cliTruncate('unicorns', 7, {position: 'middle', space: true});
  60. //=> 'uni … s'
  61. ```
  62. ##### preferTruncationOnSpace
  63. Type: `boolean`\
  64. Default: `false`
  65. Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
  66. ```js
  67. cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
  68. //=> '…rainbow dragons'
  69. // without preferTruncationOnSpace
  70. cliTruncate('unicorns rainbow dragons', 20, {position: 'start'})
  71. //=> '…rns rainbow dragons'
  72. cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true})
  73. //=> 'unicorns…dragons'
  74. cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true})
  75. //=> 'unico…'
  76. // preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
  77. cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true})
  78. //=> 'uni…ns'
  79. ```
  80. ## Related
  81. - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
  82. - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
  83. ---
  84. <div align="center">
  85. <b>
  86. <a href="https://tidelift.com/subscription/pkg/npm-cli-truncate?utm_source=npm-cli-truncate&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  87. </b>
  88. <br>
  89. <sub>
  90. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  91. </sub>
  92. </div>