use unicode_width::UnicodeWidthStr;
#[must_use]
pub fn display_width(s: &str) -> usize {
UnicodeWidthStr::width(s)
}
#[must_use]
pub fn truncate_end(text: &str, max_width: usize) -> String {
let width = display_width(text);
if width <= max_width {
return text.to_string();
}
if max_width <= 3 {
return ".".repeat(max_width);
}
let target_width = max_width - 3; let mut result = String::new();
let mut current_width = 0;
for ch in text.chars() {
let ch_width = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
if current_width + ch_width > target_width {
break;
}
result.push(ch);
current_width += ch_width;
}
result.push_str("...");
result
}
#[must_use]
pub fn truncate_start(text: &str, max_width: usize) -> String {
let width = display_width(text);
if width <= max_width {
return text.to_string();
}
if max_width <= 3 {
return ".".repeat(max_width);
}
let target_width = max_width - 3;
let mut result = String::new();
let mut current_width = 0;
for ch in text.chars().rev() {
let ch_width = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
if current_width + ch_width > target_width {
break;
}
result.insert(0, ch);
current_width += ch_width;
}
let mut final_result = String::from("...");
final_result.push_str(&result);
final_result
}
#[cfg(test)]
#[path = "ui_tests.rs"]
mod tests;