Skip to main content

truncate_measured

Function truncate_measured 

Source
pub fn truncate_measured(s: &str, max_cols: u16) -> (&str, u16)
Expand description

Splits s at the byte index where its display width reaches max_cols, returning the prefix’s own measured width alongside it.

Equivalent to let (prefix, _) = split_at_width(s, max_cols); (prefix, width(prefix)), except the width is read back from split_at_width’s own internal accounting instead of re-measuring prefix with a second pass over it: reach for this instead of that pair whenever the caller needs the truncated width right after truncating (which is every truncate call in this crate’s own callers), rather than only the truncated text.

§Examples

use retroglyph_core::text::truncate_measured;

assert_eq!(truncate_measured("hello world", 5), ("hello", 5));
assert_eq!(truncate_measured("a\u{4e2d}b", 2), ("a", 1)); // "\u{4e2d}" (CJK) doesn't fit