0.3.0: TrimString and TrimStringRef

This commit is contained in:
🪞👃🪞 2025-03-18 01:04:25 +02:00
parent 10a2d17b48
commit a80443262c
4 changed files with 81 additions and 23 deletions

35
Cargo.lock generated
View file

@ -286,14 +286,14 @@ dependencies = [
[[package]]
name = "getrandom"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0"
dependencies = [
"cfg-if",
"libc",
"wasi 0.13.3+wasi-0.2.2",
"windows-targets",
"r-efi",
"wasi 0.14.2+wasi-0.2.4",
]
[[package]]
@ -680,6 +680,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "rand"
version = "0.8.5"
@ -920,7 +926,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "488960f40a3fd53d72c2a29a58722561dee8afdd175bd88e3db4677d7b2ba600"
dependencies = [
"fastrand",
"getrandom 0.3.1",
"getrandom 0.3.2",
"once_cell",
"rustix 1.0.2",
"windows-sys 0.59.0",
@ -928,7 +934,7 @@ dependencies = [
[[package]]
name = "tengri"
version = "0.1.2"
version = "0.3.0"
dependencies = [
"tengri_dsl",
"tengri_input",
@ -938,7 +944,7 @@ dependencies = [
[[package]]
name = "tengri_dsl"
version = "0.1.2"
version = "0.3.0"
dependencies = [
"itertools 0.14.0",
"konst",
@ -949,7 +955,7 @@ dependencies = [
[[package]]
name = "tengri_input"
version = "0.1.2"
version = "0.3.0"
dependencies = [
"tengri_dsl",
"tengri_tui",
@ -957,7 +963,7 @@ dependencies = [
[[package]]
name = "tengri_output"
version = "0.1.2"
version = "0.3.0"
dependencies = [
"proptest",
"proptest-derive",
@ -968,7 +974,7 @@ dependencies = [
[[package]]
name = "tengri_tui"
version = "0.1.2"
version = "0.3.0"
dependencies = [
"atomic_float",
"better-panic",
@ -982,6 +988,7 @@ dependencies = [
"tengri_dsl",
"tengri_input",
"tengri_output",
"unicode-width 0.2.0",
]
[[package]]
@ -1077,9 +1084,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
@ -1257,9 +1264,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "wit-bindgen-rt"
version = "0.33.0"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]

View file

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

View file

@ -5,14 +5,15 @@ description = "UI metaframework, Ratatui backend."
version = { workspace = true }
[dependencies]
palette = { version = "0.7.6", features = [ "random" ] }
rand = "0.8.5"
crossterm = "0.28.1"
ratatui = { version = "0.29.0", features = [ "unstable-widget-ref", "underline-color" ] }
better-panic = "0.3.0"
konst = { version = "0.3.16", features = [ "rust_1_83" ] }
atomic_float = "1"
quanta = "0.12.3"
palette = { version = "0.7.6", features = [ "random" ] }
rand = "0.8.5"
crossterm = "0.28.1"
ratatui = { version = "0.29.0", features = [ "unstable-widget-ref", "underline-color" ] }
better-panic = "0.3.0"
konst = { version = "0.3.16", features = [ "rust_1_83" ] }
atomic_float = "1"
quanta = "0.12.3"
unicode-width = "0.2"
tengri_input = { path = "../input" }
tengri_output = { path = "../output" }

View file

@ -0,0 +1,50 @@
use crate::*;
use crate::ratatui::prelude::Position;
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
/// Displays an owned [str]-like with fixed maximum width.
///
/// Width is computed using [unicode_width].
pub struct TrimString<T: AsRef<str>>(pub u16, pub T);
impl<'a, T: AsRef<str>> TrimString<T> {
fn as_ref (&self) -> TrimStringRef<'_, T> {
TrimStringRef(self.0, &self.1)
}
}
impl<'a, T: AsRef<str>> Content<TuiOut> for TrimString<T> {
fn layout (&self, to: [u16; 4]) -> [u16;4] {
Content::layout(&self.as_ref(), to)
}
fn render (&self, to: &mut TuiOut) {
Content::render(&self.as_ref(), to)
}
}
/// Displays a borrowed [str]-like with fixed maximum width
///
/// Width is computed using [unicode_width].
pub struct TrimStringRef<'a, T: AsRef<str>>(pub u16, pub &'a T);
impl<T: AsRef<str>> Content<TuiOut> for TrimStringRef<'_, T> {
fn layout (&self, to: [u16; 4]) -> [u16;4] {
[to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h()]
}
fn render (&self, target: &mut TuiOut) {
let area = target.area();
let mut width: u16 = 0;
let mut chars = self.1.as_ref().chars();
while let Some(c) = chars.next() {
width += c.width().unwrap_or(0) as u16;
if width > self.0 || width > area.w() {
break
}
if let Some(cell) = target.buffer.cell_mut(Position {
x: area.x() + width,
y: area.y()
}) {
cell.set_char(c);
}
}
}
}