From 25354099fe3cde43a242d41fdb673c4fce5c943e Mon Sep 17 00:00:00 2001 From: facile pop culture reference Date: Tue, 28 Jul 2026 04:14:56 +0300 Subject: [PATCH] draw multiline strings --- dizzle | 2 +- src/draw/layout.rs | 22 +++++++++++----------- src/text.rs | 26 +++++++++++++++++++++----- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/dizzle b/dizzle index 4424ef7..1f59b27 160000 --- a/dizzle +++ b/dizzle @@ -1 +1 @@ -Subproject commit 4424ef7fc3fd8bfbea1fb55b4922f7a8cfd2a8ef +Subproject commit 1f59b275a774871e23e8c08637862a11a83c2653 diff --git a/src/draw/layout.rs b/src/draw/layout.rs index 0561f2a..28c9541 100644 --- a/src/draw/layout.rs +++ b/src/draw/layout.rs @@ -357,17 +357,17 @@ impl_draw!(,>|self: Align, to: S|{ let XYWH(x0, y0, w0, h0) = to.area(); 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(NW) => XYWH(x0, y0, 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 { diff --git a/src/text.rs b/src/text.rs index 3fd8806..9c00c64 100644 --- a/src/text.rs +++ b/src/text.rs @@ -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, to: Tui|{self.as_ref().draw(to)}); impl_draw!(|self: &std::sync::Arc, 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 for &str { + fn layout (&self, area: XYWH) -> Perhaps> { + 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 { + 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!(,>|self: TrimString, to: Tui|{self.as_ref().draw(to)}); impl_draw!(,>|self: TrimStringRef<'_, T>, to: Tui|{