tui: trim all strings

no newline or wrapping yet
This commit is contained in:
🪞👃🪞 2025-08-12 13:15:00 +03:00
parent ab1afa219f
commit 3298d6b6e1
2 changed files with 56 additions and 14 deletions

View file

@ -3,22 +3,37 @@ use crate::ratatui::prelude::Position;
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
impl_content_layout_render!(TuiOut: |self: &str, to|
layout = to.center_xy([self.chars().count() as u16, 1]);
render = {let [x, y, ..] = Content::layout(self, to.area());
to.blit(self, x, y, None)});
layout = to.center_xy([width_chars_max(to.w(), self), 1]);
render = {let [x, y, w, ..] = Content::layout(self, to.area());
to.text(self, x, y, w)});
impl_content_layout_render!(TuiOut: |self: String, to|
layout = to.center_xy([self.chars().count() as u16, 1]);
render = {let [x, y, ..] = Content::layout(self, to.area());
to.blit(self, x, y, None)});
layout = Content::<TuiOut>::layout(&self.as_str(), to);
render = Content::<TuiOut>::render(&self.as_str(), to));
impl_content_layout_render!(TuiOut: |self: Arc<str>, to|
layout = Content::<TuiOut>::layout(&self.as_ref(), to);
render = Content::<TuiOut>::render(&self.as_ref(), to));
impl_content_layout_render!(TuiOut: |self: std::sync::RwLock<String>, to|
layout = Content::<TuiOut>::layout(&self.read().unwrap(), to);
render = Content::<TuiOut>::render(&self.read().unwrap(), to));
impl_content_layout_render!(TuiOut: |self: std::sync::RwLockReadGuard<'_, String>, to|
layout = Content::<TuiOut>::layout(&**self, to);
render = Content::<TuiOut>::render(&**self, to));
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));
fn width_chars_max (max: u16, text: impl AsRef<str>) -> u16 {
let mut width: u16 = 0;
let mut chars = text.as_ref().chars();
while let Some(c) = chars.next() {
width += c.width().unwrap_or(0) as u16;
if width > max {
break
}
}
return width
}
/// Trim string with [unicode_width].
pub fn trim_string (max_width: usize, input: impl AsRef<str>) -> String {