mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-08-07 22:17:07 +02:00
draw multiline strings
This commit is contained in:
parent
dee3d33453
commit
25354099fe
3 changed files with 33 additions and 17 deletions
2
dizzle
2
dizzle
|
|
@ -1 +1 @@
|
||||||
Subproject commit 4424ef7fc3fd8bfbea1fb55b4922f7a8cfd2a8ef
|
Subproject commit 1f59b275a774871e23e8c08637862a11a83c2653
|
||||||
|
|
@ -357,17 +357,17 @@ impl_draw!(<S: Screen, T: Draw<S>,>|self: Align<T>, to: S|{
|
||||||
let XYWH(x0, y0, w0, h0) = to.area();
|
let XYWH(x0, y0, w0, h0) = to.area();
|
||||||
if let Some(XYWH(x, y, w, h)) = self.1.layout(to.area())? {
|
if let Some(XYWH(x, y, w, h)) = self.1.layout(to.area())? {
|
||||||
to.clip(match self.0 {
|
to.clip(match self.0 {
|
||||||
Some(NW) => XYWH(x0, y0, w, h),
|
Some(NW) => XYWH(x0, y0, w, h),
|
||||||
Some(N) => XYWH(x0 + w0.sub(w) / 2.into(), y0, w, h),
|
Some(N) => XYWH(x0 + w0.minus(w) / 2.into(), y0, w, h),
|
||||||
Some(NE) => XYWH((x0 + w0).sub(w), y0, w, h),
|
Some(NE) => XYWH((x0 + w0).minus(w), y0, w, h),
|
||||||
Some(W) => XYWH(x0, y0 + h0.sub(h) / 2.into(), w, h),
|
Some(W) => XYWH(x0, y0 + h0.minus(h) / 2.into(), w, h),
|
||||||
Some(C) => XYWH(x0 + w0.sub(w) / 2.into(), y0 + h0.sub(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).sub(w), y0 + h0.sub(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).sub(h), w, h),
|
Some(SW) => XYWH(x0, (y0 + h0).minus(h), w, h),
|
||||||
Some(S) => XYWH(x0 + w0.sub(w) / 2.into(), (y0 + h0).sub(h), w, h),
|
Some(S) => XYWH(x0 + w0.minus(w) / 2.into(), (y0 + h0).minus(h), w, h),
|
||||||
Some(SE) => XYWH((x0 + w0).sub(w), (y0 + h0).sub(h), w, h),
|
Some(SE) => XYWH((x0 + w0).minus(w), (y0 + h0).minus(h), w, h),
|
||||||
Some(X) => XYWH(x0 + w0.sub(w) / 2.into(), y, w, h),
|
Some(X) => XYWH(x0 + w0.minus(w) / 2.into(), y, w, h),
|
||||||
Some(Y) => XYWH(x, y0 + h0.sub(h) / 2.into(), w, h),
|
Some(Y) => XYWH(x, y0 + h0.minus(h) / 2.into(), w, h),
|
||||||
None => to.area()
|
None => to.area()
|
||||||
}, |to|self.1.draw(to))
|
}, |to|self.1.draw(to))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
26
src/text.rs
26
src/text.rs
|
|
@ -3,16 +3,32 @@ pub(crate) use ::unicode_width::*;
|
||||||
|
|
||||||
#[cfg(feature = "term")] mod impl_term {
|
#[cfg(feature = "term")] mod impl_term {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::*;
|
|
||||||
use ratatui::prelude::Position;
|
use ratatui::prelude::Position;
|
||||||
|
|
||||||
impl_draw!(|self: String, to: Tui|{self.as_str().draw(to)});
|
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: &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|{
|
impl Draw<Tui> for &str {
|
||||||
let XYWH(x, y, w, ..) = to.1.centered_xy([width_chars_max(to.w(), self), 1]);
|
fn layout (&self, area: XYWH<u16>) -> Perhaps<XYWH<u16>> {
|
||||||
to.text(&self, x, y, w)
|
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: TrimString<T>, to: Tui|{self.as_ref().draw(to)});
|
||||||
impl_draw!(<T: AsRef<str>,>|self: TrimStringRef<'_, T>, to: Tui|{
|
impl_draw!(<T: AsRef<str>,>|self: TrimStringRef<'_, T>, to: Tui|{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue