tui: trim all strings

no newline or wrapping yet
This commit is contained in:
🪞👃🪞 2025-08-12 13:15:00 +03:00
parent ab1afa219f
commit 3298d6b6e1
2 changed files with 56 additions and 14 deletions

View file

@ -1,6 +1,7 @@
use crate::*;
use std::time::Duration;
use std::thread::{spawn, JoinHandle};
use unicode_width::*;
#[derive(Default)]
pub struct TuiOut {
pub buffer: Buffer,
@ -10,8 +11,12 @@ impl Output for TuiOut {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
#[inline] fn area (&self) -> [u16;4] { self.area }
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
#[inline] fn area (&self) -> [u16;4] {
self.area
}
#[inline] fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.area
}
#[inline] fn place <'t, T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &'t T) {
let last = self.area();
*self.area_mut() = area;
@ -63,10 +68,32 @@ impl TuiOut {
pub fn blit (
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
) {
let text = text.as_ref();
let buf = &mut self.buffer;
let text = text.as_ref();
let buf = &mut self.buffer;
let style = style.unwrap_or(Style::default());
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, text, style.unwrap_or(Style::default()));
buf.set_string(x, y, text, style);
}
}
/// Write a line of text
///
/// TODO: do a paragraph (handle newlines)
pub fn text (&mut self, text: &impl AsRef<str>, x0: u16, y: u16, max_width: u16) {
let text = text.as_ref();
let buf = &mut self.buffer;
let mut string_width: u16 = 0;
for character in text.chars() {
let x = x0 + string_width;
let character_width = character.width().unwrap_or(0) as u16;
string_width += character_width;
if string_width > max_width {
break
}
if let Some(cell) = buf.cell_mut(ratatui::prelude::Position { x, y }) {
cell.set_char(character);
} else {
break
}
}
}
pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {