0.6.0: add trim_string

This commit is contained in:
🪞👃🪞 2025-04-06 18:59:50 +03:00
parent ea01deb854
commit b8ad5dae9f
4 changed files with 26 additions and 12 deletions

View file

@ -20,6 +20,22 @@ impl_content_layout_render!(TuiOut: |self: Arc<str>, to|
layout = to.center_xy([self.chars().count() as u16, 1]);
render = to.blit(self, to.area.x(), to.area.y(), None));
/// Trim string with [unicode_width].
pub fn trim_string (max_width: usize, input: impl AsRef<str>) -> String {
let input = input.as_ref();
let mut output = Vec::with_capacity(input.len());
let mut width: usize = 1;
let mut chars = input.chars();
while let Some(c) = chars.next() {
if width > max_width {
break
}
output.push(c);
width += c.width().unwrap_or(0);
}
return output.into_iter().collect()
}
/// Displays an owned [str]-like with fixed maximum width.
///
/// Width is computed using [unicode_width].