mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
84 lines
3 KiB
Rust
84 lines
3 KiB
Rust
use crate::*;
|
|
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)});
|
|
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)});
|
|
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));
|
|
|
|
/// 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].
|
|
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 = 1;
|
|
let mut chars = self.1.as_ref().chars();
|
|
while let Some(c) = chars.next() {
|
|
if width > self.0 || width > area.w() {
|
|
break
|
|
}
|
|
if let Some(cell) = target.buffer.cell_mut(Position {
|
|
x: area.x() + width - 1,
|
|
y: area.y()
|
|
}) {
|
|
cell.set_char(c);
|
|
}
|
|
width += c.width().unwrap_or(0) as u16;
|
|
}
|
|
}
|
|
}
|