From b8ad5dae9f6da3cd9ec664afd9cced4be2cadf07 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 6 Apr 2025 18:59:50 +0300 Subject: [PATCH] 0.6.0: add trim_string --- Cargo.lock | 10 +++++----- Cargo.toml | 2 +- tui/src/tui_content.rs | 10 ++++------ tui/src/tui_content/tui_string.rs | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3972207..c575065 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -934,7 +934,7 @@ dependencies = [ [[package]] name = "tengri" -version = "0.5.2" +version = "0.6.0" dependencies = [ "tengri_dsl", "tengri_input", @@ -944,7 +944,7 @@ dependencies = [ [[package]] name = "tengri_dsl" -version = "0.5.2" +version = "0.6.0" dependencies = [ "itertools 0.14.0", "konst", @@ -955,7 +955,7 @@ dependencies = [ [[package]] name = "tengri_input" -version = "0.5.2" +version = "0.6.0" dependencies = [ "tengri_dsl", "tengri_tui", @@ -963,7 +963,7 @@ dependencies = [ [[package]] name = "tengri_output" -version = "0.5.2" +version = "0.6.0" dependencies = [ "proptest", "proptest-derive", @@ -974,7 +974,7 @@ dependencies = [ [[package]] name = "tengri_tui" -version = "0.5.2" +version = "0.6.0" dependencies = [ "atomic_float", "better-panic", diff --git a/Cargo.toml b/Cargo.toml index d075434..b17bef8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace.package] -version = "0.5.2" +version = "0.6.0" [workspace] resolver = "2" diff --git a/tui/src/tui_content.rs b/tui/src/tui_content.rs index 88014ca..0b15a95 100644 --- a/tui/src/tui_content.rs +++ b/tui/src/tui_content.rs @@ -1,12 +1,10 @@ use crate::*; -use ratatui::prelude::Position; macro_rules! impl_content_layout_render { - ( - $Output:ty: |$self:ident: $Struct:ty, $to:ident| - layout = $layout:expr; - render = $render:expr - ) => { + ($Output:ty: |$self:ident: $Struct:ty, $to:ident| + layout = $layout:expr; + render = $render:expr) => + { impl Content<$Output> for $Struct { fn layout (&$self, $to: [u16;4]) -> [u16;4] { $layout } fn render (&$self, $to: &mut $Output) { $render } diff --git a/tui/src/tui_content/tui_string.rs b/tui/src/tui_content/tui_string.rs index 73983c6..92b91fc 100644 --- a/tui/src/tui_content/tui_string.rs +++ b/tui/src/tui_content/tui_string.rs @@ -20,6 +20,22 @@ impl_content_layout_render!(TuiOut: |self: Arc, 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) -> 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].