| 1 |
[](https://crates.io/crates/ansi-width) |
| 2 |
[](https://discord.gg/wQVJbvJ) |
| 3 |
[](https://github.com/uutils/ansi-width/blob/main/LICENSE) |
| 4 |
[](https://deps.rs/repo/github/uutils/ansi-width) |
| 5 |
|
| 6 |
[](https://codecov.io/gh/uutils/ansi-width) |
| 7 |
|
| 8 |
# ANSI width |
| 9 |
|
| 10 |
Measure the width of a string when printed to the terminal |
| 11 |
|
| 12 |
For ASCII, this is identical to the length of the string in bytes. However, |
| 13 |
there are 2 special cases: |
| 14 |
|
| 15 |
- Many unicode characters (CJK, emoji, etc.) span multiple columns. |
| 16 |
- ANSI escape codes should be ignored. |
| 17 |
|
| 18 |
The first case is handled by the `unicode-width` crate. This function extends |
| 19 |
that crate by ignoring ANSI escape codes. |
| 20 |
|
| 21 |
## Limitations |
| 22 |
|
| 23 |
- We cannot know the width of a `TAB` character in the terminal emulator. |
| 24 |
- Backspace is also treated as zero width. |
| 25 |
|
| 26 |
## A Primer on ANSI escape codes (and how this crate works) |
| 27 |
|
| 28 |
ANSI codes are created using special character sequences in a string. These |
| 29 |
sequences start with the ESC character: `'\x1b'`, followed by some other |
| 30 |
character to determine the type of the escape code. That second character |
| 31 |
determines how long the sequence continues: |
| 32 |
|
| 33 |
- `ESC [`: until a character in the range `'\x40'..='\x7E'` is found. |
| 34 |
- `ESC ]`: until an `ST` is found. |
| 35 |
|
| 36 |
An `ST` is a String Terminator and is given by the sequence `ESC \` (or in Rust |
| 37 |
syntax `'\x1b\x5c'`). |
| 38 |
|
| 39 |
This is the subset of sequences that this library supports, since these are used |
| 40 |
by most applications that need this functionality. If you have a use case for |
| 41 |
other codes, please open an issue on the |
| 42 |
[GitHub repository](https://github.com/uutils/ansi-width). |
| 43 |
|
| 44 |
`ansi-width` does not parse the actual ANSI codes to improve performance, it can |
| 45 |
only skip the ANSI codes. |
| 46 |
|
| 47 |
## Examples |
| 48 |
|
| 49 |
```rust |
| 50 |
use ansi_width::ansi_width; |
| 51 |
|
| 52 |
// ASCII string |
| 53 |
assert_eq!(ansi_width("123456"), 6); |
| 54 |
|
| 55 |
// Accents |
| 56 |
assert_eq!(ansi_width("café"), 4); |
| 57 |
|
| 58 |
// Emoji (2 crab emoji) |
| 59 |
assert_eq!(ansi_width("🦀🦀"), 4); |
| 60 |
|
| 61 |
// CJK characters (“Nǐ hǎo” or “Hello” in Chinese) |
| 62 |
assert_eq!(ansi_width("你好"), 4); |
| 63 |
|
| 64 |
// ANSI colors |
| 65 |
assert_eq!(ansi_width("\u{1b}[31mRed\u{1b}[0m"), 3); |
| 66 |
|
| 67 |
// ANSI hyperlink |
| 68 |
assert_eq!( |
| 69 |
ansi_width("\x1b]8;;http://example.com\x1b\\This is a link\x1b]8;;\x1b\\"), |
| 70 |
14 |
| 71 |
); |
| 72 |
``` |
| 73 |
|
| 74 |
## Alternatives |
| 75 |
|
| 76 |
- [`str::len`](https://doc.rust-lang.org/std/primitive.str.html#method.len): Returns only the length in bytes and therefore only works for |
| 77 |
ASCII characters. |
| 78 |
- [`unicode-width`](https://crates.io/crates/unicode-width): Does not take ANSI |
| 79 |
characters into account by design (see |
| 80 |
[this issue](https://github.com/unicode-rs/unicode-width/issues/24)). This |
| 81 |
might be what you want if you don't care about ANSI codes. `unicode-width` is |
| 82 |
used internally by this crate as well. |
| 83 |
- [`textwrap::core::display_width`](https://docs.rs/textwrap/latest/textwrap/core/fn.display_width.html): |
| 84 |
Very similar functionality to this crate and it also supports hyperlinks since version 0.16.1. The |
| 85 |
advantage of this crate is that it does not require pulling in the rest of `textwrap`'s functionality |
| 86 |
(even though that functionality is excellent if you need it). |
| 87 |
- [`console::measure_text_width`](https://docs.rs/console/latest/console/fn.measure_text_width.html): |
| 88 |
Similar to `textwrap` and very well-tested. However, it constructs a new |
| 89 |
string internally without ANSI codes first and then measures the width of |
| 90 |
that. The parsing is more robust than this crate though. |
| 91 |
|
| 92 |
## References |
| 93 |
|
| 94 |
The information above is based on: |
| 95 |
|
| 96 |
- <https://en.wikipedia.org/wiki/ANSI_escape_code> |
| 97 |
- <https://www.ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf> |