use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
pub(crate) const DEFAULT_TERM_WIDTH: usize = 80;
pub(super) const MIN_TITLE_WIDTH: usize = 10;
fn char_width(c: char) -> usize {
UnicodeWidthChar::width(c).unwrap_or(0)
}
pub(super) fn display_width(s: &str) -> usize {
UnicodeWidthStr::width(s)
}
pub(super) fn truncate(s: &str, max: usize) -> String {
if display_width(s) <= max {
return s.to_string();
}
if max == 0 {
return String::new();
}
let budget = max - 1;
let mut width = 0;
let mut out = String::new();
for c in s.chars() {
let w = char_width(c);
if width + w > budget {
break;
}
width += w;
out.push(c);
}
out.push('…');
out
}