| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Measure the width of a string when printed to the terminal
For ASCII, this is identical to the length of the string in bytes. However, there are 2 special cases:
The first case is handled by the unicode-width crate. This function extends that crate by ignoring ANSI escape codes.
ANSI codes are created using special character sequences in a string. These sequences start with the ESC character: '\x1b', followed by some other character to determine the type of the escape code. That second character determines how long the sequence continues:
An ST is a String Terminator and is given by the sequence ESC \ (or in Rust syntax '\x1b\x5c').
This is the subset of sequences that this library supports, since these are used by most applications that need this functionality. If you have a use case for other codes, please open an issue on the GitHub repository.
ansi-width does not parse the actual ANSI codes to improve performance, it can only skip the ANSI codes.
use ansi_width::ansi_width;
// ASCII string
assert_eq!(ansi_width("123456"), 6);
// Accents
assert_eq!(ansi_width("café"), 4);
// Emoji (2 crab emoji)
assert_eq!(ansi_width("🦀🦀"), 4);
// CJK characters (“Nǐ hǎo” or “Hello” in Chinese)
assert_eq!(ansi_width("你好"), 4);
// ANSI colors
assert_eq!(ansi_width("\u{1b}[31mRed\u{1b}[0m"), 3);
// ANSI hyperlink
assert_eq!(
ansi_width("\x1b]8;;http://example.com\x1b\\This is a link\x1b]8;;\x1b\\"),
14
);The information above is based on:
| Back | FazBrowse Home | New Git URL |