Nikolay Vitkov | 95ea045 | 2025-04-30 15:52:34 | [diff] [blame] | 1 | # string-width |
| 2 | |
| 3 | > Get the visual width of a string - the number of columns required to display it |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | Useful to be able to measure the actual width of command-line output. |
| 8 | |
| 9 | |
| 10 | ## Install |
| 11 | |
| 12 | ``` |
| 13 | $ npm install string-width |
| 14 | ``` |
| 15 | |
| 16 | |
| 17 | ## Usage |
| 18 | |
| 19 | ```js |
| 20 | const stringWidth = require('string-width'); |
| 21 | |
| 22 | stringWidth('a'); |
| 23 | //=> 1 |
| 24 | |
| 25 | stringWidth('古'); |
| 26 | //=> 2 |
| 27 | |
| 28 | stringWidth('\u001B[1m古\u001B[22m'); |
| 29 | //=> 2 |
| 30 | ``` |
| 31 | |
| 32 | |
| 33 | ## Related |
| 34 | |
| 35 | - [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module |
| 36 | - [string-length](https://github.com/sindresorhus/string-length) - Get the real length of a string |
| 37 | - [widest-line](https://github.com/sindresorhus/widest-line) - Get the visual width of the widest line in a string |
| 38 | |
| 39 | |
| 40 | --- |
| 41 | |
| 42 | <div align="center"> |
| 43 | <b> |
| 44 | <a href="https://tidelift.com/subscription/pkg/npm-string-width?utm_source=npm-string-width&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> |
| 45 | </b> |
| 46 | <br> |
| 47 | <sub> |
| 48 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. |
| 49 | </sub> |
| 50 | </div> |