ANSIString provides utilities for working with strings that contain ANSI escape codes.
This is critical for correct word wrapping and layout calculations.
Constants
ANSI_PATTERN = /\e\[[0-9;]*m|\e\]8;[^\e]*\e\\/
ANSI escape code pattern (SGR codes + OSC 8 hyperlinks)
Class methods
Returns the display width of a single character.
Combining marks and zero-width characters occupy 0 columns.
All other printable characters occupy 1 column.
SourceChecks if a string contains ANSI escape codes.
SourceReturns the length of the longest line in a string (visible width).
SourceStrips all ANSI escape codes from a string.
SourceTruncates a string to the given visible width without breaking ANSI codes.
If the string is shorter than the width, returns it unchanged.
If longer, truncates and optionally appends a suffix.
SourceReturns the visible width of a string (excluding ANSI codes).
Correctly handles CJK double-width characters, emoji, and OSC 8 hyperlinks.
Source