mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
0.3.0: TrimString and TrimStringRef
This commit is contained in:
parent
10a2d17b48
commit
a80443262c
4 changed files with 81 additions and 23 deletions
|
|
@ -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" }
|
||||
|
|
|
|||
50
tui/src/tui_content/tui_string.rs
Normal file
50
tui/src/tui_content/tui_string.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue