Calculate the visual column width of a string, accounting for Unicode fullwidth characters and ignoring ANSI escape codes.
string-width is a Node.js library that calculates the visual width of a string in terminal columns, accounting for Unicode fullwidth characters and ignoring ANSI escape codes. It solves the problem of accurately measuring how much horizontal space a string will occupy in command-line interfaces, where characters like '古' take two columns instead of one.
Node.js developers building command-line tools, terminal UIs, or any application that requires precise text alignment and formatting in console output.
Developers choose string-width because it provides reliable, standards-compliant width calculations out of the box, handling edge cases like ambiguous-width characters and ANSI codes that native JavaScript methods miss.
Get the visual width of a string - the number of columns required to display it
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Accurately counts East Asian wide characters as two columns, adhering to Unicode Technical Report #11, ensuring proper alignment for multi-language CLI output.
Automatically ignores ANSI codes for colors and styles, so formatted text doesn't affect width calculations, crucial for colored terminal interfaces.
Configurable option to treat ambiguous-width characters as narrow or wide, allowing developers to match specific terminal or language contexts.
Offers a straightforward function with optional parameters, as shown in the README examples, making integration easy for CLI tools.
Designed exclusively for Node.js, so it cannot be used in browser-based applications or other JavaScript environments without significant modifications.
For projects without ANSI codes or fullwidth characters, using this library adds unnecessary dependency and processing compared to native string methods.
The default setting for ambiguous characters might not suit all use cases, requiring developers to understand and correctly configure the option based on their specific needs.