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.

45 lines
1.2 KiB

4 years ago
  1. # string-width [![Build Status](https://travis-ci.org/sindresorhus/string-width.svg?branch=master)](https://travis-ci.org/sindresorhus/string-width)
  2. > Get the visual width of a string - the number of columns required to display it
  3. Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
  4. Useful to be able to measure the actual width of command-line output.
  5. ## Install
  6. ```
  7. $ npm install string-width
  8. ```
  9. ## Usage
  10. ```js
  11. const stringWidth = require('string-width');
  12. stringWidth('古');
  13. //=> 2
  14. stringWidth('\u001b[1m古\u001b[22m');
  15. //=> 2
  16. stringWidth('a');
  17. //=> 1
  18. stringWidth('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
  19. // => 5
  20. ```
  21. ## Related
  22. - [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module
  23. - [string-length](https://github.com/sindresorhus/string-length) - Get the real length of a string
  24. - [widest-line](https://github.com/sindresorhus/widest-line) - Get the visual width of the widest line in a string
  25. ## License
  26. MIT © [Sindre Sorhus](https://sindresorhus.com)