mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-03 21:40:44 +02:00
This commit is contained in:
parent
b0fbe3c173
commit
9dbf4fcab5
9 changed files with 1101 additions and 1169 deletions
39
src/text.rs
39
src/text.rs
|
|
@ -1,3 +1,4 @@
|
|||
use crate::draw::Draw;
|
||||
pub(crate) use ::unicode_width::*;
|
||||
|
||||
/// Displays an owned [str]-like with fixed maximum width.
|
||||
|
|
@ -41,3 +42,41 @@ pub(crate) fn width_chars_max (max: u16, text: impl AsRef<str>) -> u16 {
|
|||
}
|
||||
return width
|
||||
}
|
||||
|
||||
#[cfg(feature = "term")] mod impl_term {
|
||||
use super::*;
|
||||
use crate::draw::XYWH;
|
||||
use ratatui::prelude::{Buffer, Position};
|
||||
impl<'a, T> Draw<Buffer> for TrimStringRef<'a, T> {
|
||||
fn draw (&self, to: &mut Buffer) {
|
||||
let XYWH(x, y, w, ..) = XYWH(to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h());
|
||||
to.text(&self, x, y, w)
|
||||
}
|
||||
}
|
||||
impl<T> Draw<Buffer> for TrimString<T> {
|
||||
fn draw (&self, to: &mut Buffer) { self.as_ref().draw(to) }
|
||||
}
|
||||
impl<'a, T: AsRef<str>> Draw<Buffer> for TrimString<T> {
|
||||
fn draw (&self, to: &mut Buffer) { Draw::draw(&self.as_ref(), to) }
|
||||
}
|
||||
impl<T: AsRef<str>> Draw<Buffer> for TrimStringRef<'_, T> {
|
||||
fn draw (&self, target: &mut Buffer) {
|
||||
let area = target.area();
|
||||
let mut buf = target.buffer.write().unwrap();
|
||||
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) = buf.cell_mut(Position {
|
||||
x: area.x() + width - 1,
|
||||
y: area.y()
|
||||
}) {
|
||||
cell.set_char(c);
|
||||
}
|
||||
width += c.width().unwrap_or(0) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue