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

10
Cargo.lock generated
View file

@ -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",

View file

@ -1,5 +1,5 @@
[workspace.package]
version = "0.5.2"
version = "0.6.0"
[workspace]
resolver = "2"

View file

@ -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 }

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].