Skip to content

visible_len

The number of characters a terminal shows for text: its length once escape sequences are removed.

Use it to pad or truncate styled text to a column width. It counts chars, not display cells, so wide characters (CJK, some emoji) count as one and combining marks count too; use a width-aware crate when you need exact terminal columns.

use helpers4::ansi::visible_len;

Cargo feature ansi (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features ansi

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["ansi"] }
pub fn visible_len(text: &str) -> usize
ParameterTypeDescription
text&strThe text, possibly containing escape sequences.

usize — The number of visible characters.

use helpers4::ansi::visible_len;

assert_eq!(visible_len("\u{1b}[1;31merror\u{1b}[0m"), 5);
assert_eq!(visible_len("plain"), 5);

src/ansi/visible_len.rs