draw multiline strings

This commit is contained in:
facile pop culture reference 2026-07-28 04:14:56 +03:00
parent dee3d33453
commit 25354099fe
3 changed files with 33 additions and 17 deletions

2
dizzle

@ -1 +1 @@
Subproject commit 4424ef7fc3fd8bfbea1fb55b4922f7a8cfd2a8ef
Subproject commit 1f59b275a774871e23e8c08637862a11a83c2653

View file

@ -358,16 +358,16 @@ impl_draw!(<S: Screen, T: Draw<S>,>|self: Align<T>, to: S|{
if let Some(XYWH(x, y, w, h)) = self.1.layout(to.area())? {
to.clip(match self.0 {
Some(NW) => XYWH(x0, y0, w, h),
Some(N) => XYWH(x0 + w0.sub(w) / 2.into(), y0, w, h),
Some(NE) => XYWH((x0 + w0).sub(w), y0, w, h),
Some(W) => XYWH(x0, y0 + h0.sub(h) / 2.into(), w, h),
Some(C) => XYWH(x0 + w0.sub(w) / 2.into(), y0 + h0.sub(h) / 2.into(), w, h),
Some(E) => XYWH((x0 + w0).sub(w), y0 + h0.sub(h) / 2.into(), w, h),
Some(SW) => XYWH(x0, (y0 + h0).sub(h), w, h),
Some(S) => XYWH(x0 + w0.sub(w) / 2.into(), (y0 + h0).sub(h), w, h),
Some(SE) => XYWH((x0 + w0).sub(w), (y0 + h0).sub(h), w, h),
Some(X) => XYWH(x0 + w0.sub(w) / 2.into(), y, w, h),
Some(Y) => XYWH(x, y0 + h0.sub(h) / 2.into(), w, h),
Some(N) => XYWH(x0 + w0.minus(w) / 2.into(), y0, w, h),
Some(NE) => XYWH((x0 + w0).minus(w), y0, w, h),
Some(W) => XYWH(x0, y0 + h0.minus(h) / 2.into(), w, h),
Some(C) => XYWH(x0 + w0.minus(w) / 2.into(), y0 + h0.minus(h) / 2.into(), w, h),
Some(E) => XYWH((x0 + w0).minus(w), y0 + h0.minus(h) / 2.into(), w, h),
Some(SW) => XYWH(x0, (y0 + h0).minus(h), w, h),
Some(S) => XYWH(x0 + w0.minus(w) / 2.into(), (y0 + h0).minus(h), w, h),
Some(SE) => XYWH((x0 + w0).minus(w), (y0 + h0).minus(h), w, h),
Some(X) => XYWH(x0 + w0.minus(w) / 2.into(), y, w, h),
Some(Y) => XYWH(x, y0 + h0.minus(h) / 2.into(), w, h),
None => to.area()
}, |to|self.1.draw(to))
} else {

View file

@ -3,16 +3,32 @@ pub(crate) use ::unicode_width::*;
#[cfg(feature = "term")] mod impl_term {
use super::*;
use crate::*;
use ratatui::prelude::Position;
impl_draw!(|self: String, to: Tui|{self.as_str().draw(to)});
impl_draw!(|self: std::sync::Arc<str>, to: Tui|{self.as_ref().draw(to)});
impl_draw!(|self: &std::sync::Arc<str>, to: Tui|{self.as_ref().draw(to)});
impl_draw!(|self: &str, to: Tui|{
let XYWH(x, y, w, ..) = to.1.centered_xy([width_chars_max(to.w(), self), 1]);
to.text(&self, x, y, w)
});
impl Draw<Tui> for &str {
fn layout (&self, area: XYWH<u16>) -> Perhaps<XYWH<u16>> {
let XYWH(x, y, ..) = area;
let mut max_w = 0u16;
let mut max_h = 0u16;
for line in self.split("\n") {
max_h += 1;
max_w = max_w.max(line.len() as u16);
}
Ok(Some(XYWH(x, y, max_w, max_h)))
}
fn draw (self, to: &mut Tui) -> Drawn<u16> {
let area = self.layout(to.area())?.unwrap();
////let info = format!("{area:?}");
//to.text(&self, area.0, area.1, self.len() as u16)
for (index, line) in self.split("\n").enumerate() {
let _ = to.text(&line, area.0, area.1 + index as u16, width_chars_max(area.2, line) as u16)?;
}
Ok(Some(area))
}
}
impl_draw!(<T: AsRef<str>,>|self: TrimString<T>, to: Tui|{self.as_ref().draw(to)});
impl_draw!(<T: AsRef<str>,>|self: TrimStringRef<'_, T>, to: Tui|{