use unicode_width::UnicodeWidthChar;
pub(crate) fn display_width(text: &str) -> usize {
text.chars().map(|glyph| glyph.width().unwrap_or(0)).sum()
}
pub(crate) fn fit_width_with(text: &str, max: usize, ellipsis: char) -> String {
if display_width(text) <= max {
return text.to_string();
}
let mut out = String::new();
let mut used = 0;
for glyph in text.chars() {
let width = glyph.width().unwrap_or(0);
if used + width > max.saturating_sub(1) {
break;
}
out.push(glyph);
used += width;
}
if max >= 1 {
out.push(ellipsis);
}
out
}
#[cfg(test)]
#[path = "tests/width_tests.rs"]
mod tests;