wip: doctest layouts

This commit is contained in:
facile pop culture reference 2026-07-30 17:44:40 +03:00
parent 5ca329292f
commit cd5b0cc113
3 changed files with 57 additions and 36 deletions

View file

@ -44,6 +44,14 @@ impl Interpret<Tui, Color> for State {
}
}
}
fn try_to_u8 (src: Perhaps<&str>) -> Perhaps<u8> {
use std::str::FromStr;
if let Some(src) = src? {
Ok(Some(u8::from_str(src)?))
} else {
Ok(None)
}
}
impl Interpret<Tui, Option<XYWH<u16>>> for State {
fn interpret_word (&self, to: &mut Tui, sym: &impl Language) -> Perhaps<XYWH<u16>> {
match sym.src()? {

View file

@ -13,16 +13,20 @@ use crate::*;
/// }
/// impl Screen for TestOut {
/// type Unit = u16;
/// fn show (&mut self, _: impl Draw<Self>) -> Perhaps<XYWH<u16>>
/// { println!("placed"); Ok(None) }
/// fn area (&self) -> XYWH<Self::Unit>
/// { Default::default() }
/// fn show (&mut self, _: impl Draw<Self>) -> Perhaps<XYWH<u16>> {
/// println!("placed");
/// Ok(None)
/// }
/// fn area (&self) -> XYWH<Self::Unit> {
/// Default::default()
/// }
/// fn clip <T> (
/// &mut self,
/// area: impl Into<Option<XYWH<u16>>>,
/// draw: impl FnOnce(&mut Self)->T
/// ) -> T
/// { draw(self }
/// ) -> T {
/// draw(self)
/// }
/// }
///
/// impl_draw!(|self: String, to: TestOut|{

View file

@ -160,10 +160,14 @@ pub trait Layout<S: Screen>: Draw<S> + Sized {
/// Use whole drawing area along one or both axes.
///
/// ```
/// use tengri::Layout;
/// let _ = "".full_w();
/// let _ = "".full_h();
/// let _ = "".full_wh();
/// # fn doctest_layout_full () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(0u16, 0, 80, 25);
/// assert_eq!("1".layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// assert_eq!("1".full_w().layout(area)?, Some(XYWH(0u16, 0, 80, 1)));
/// assert_eq!("1".full_h().layout(area)?, Some(XYWH(0u16, 0, 1, 25)));
/// assert_eq!("1".full_wh().layout(area)?, Some(XYWH(0u16, 0, 80, 25)));
/// # Ok(()) }
/// ```
pub enum Full<T: Screen, I: Draw<T>> {
__(PhantomData<T>),
@ -171,7 +175,6 @@ pub enum Full<T: Screen, I: Draw<T>> {
H(I),
WH(I),
}
impl_draw!(<T: Screen, I: Draw<T>,>|self: Full<T, I>, to: T|{
let XYWH(x0, y0, w0, h0) = to.area();
match self {
@ -197,10 +200,14 @@ impl_draw!(<T: Screen, I: Draw<T>,>|self: Full<T, I>, to: T|{
/// Move content in the positive direction of one or both axes.
///
/// ```
/// use tengri::Layout;
/// let _ = "".push_x(1);
/// let _ = "".push_y(1);
/// let _ = "".push_xy(1, 1);
/// # fn doctest_layout_push () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(0u16, 0, 80, 25);
/// assert_eq!("1".layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// assert_eq!("1".push_x(1).layout(area)?, Some(XYWH(1u16, 0, 1, 1)));
/// assert_eq!("1".push_y(1).layout(area)?, Some(XYWH(0u16, 1, 1, 1)));
/// assert_eq!("1".push_xy(1, 1).layout(area)?, Some(XYWH(1u16, 1, 1, 1)));
/// # Ok(()) }
/// ```
pub enum Push<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
@ -208,7 +215,6 @@ pub enum Push<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
Y(I, X),
XY(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Push<T, I, X>, to: T|{
match self {
Self::__(_) => unreachable!(),
@ -234,10 +240,14 @@ impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Push<T, I, X
/// Move content in the negative direction of one or both axes.
///
/// ```
/// use tengri::Layout;
/// let _ = "".pull_x(1);
/// let _ = "".pull_y(1);
/// let _ = "".pull_xy(1, 1);
/// # fn doctest_layout_pull () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(1u16, 1, 80, 25);
/// assert_eq!("1".layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// assert_eq!("1".pull_x(1).layout(area)?, Some(XYWH(0u16, 1, 1, 1)));
/// assert_eq!("1".pull_y(1).layout(area)?, Some(XYWH(1u16, 0, 1, 1)));
/// assert_eq!("1".pull_xy(1, 1).layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// # }
/// ```
pub enum Pull<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
@ -245,7 +255,6 @@ pub enum Pull<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
Y(I, X),
XY(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Pull<T, I, X>, _to: T|{
todo!()
});
@ -253,10 +262,14 @@ impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Pull<T, I, X
/// Only draw content if area is above a certain size.
///
/// ```
/// use tengri::Layout;
/// let _ = "".min_w(1);
/// let _ = "".min_h(1);
/// let _ = "".min_wh(1, 1);
/// # fn doctest_layout_min () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(1u16, 1, 80, 25);
/// assert_eq!("1".min_w(5).layout(area)?, Some(XYWH(1u16, 1, 5, 1)));
/// assert_eq!("1".min_h(5).layout(area)?, Some(XYWH(1u16, 1, 1, 5)));
/// assert_eq!("1".min_wh(5, 5).layout(area)?, Some(XYWH(1u16, 1, 5, 5)));
/// assert_eq!("123456".min_w(5).layout(area)?, Some(XYWH(1u16, 1, 6, 1)));
/// # Ok(()) }
/// ```
pub enum Min<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
@ -264,7 +277,6 @@ pub enum Min<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Min<T, I, X>, _to: T|{
todo!()
});
@ -272,10 +284,13 @@ impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Min<T, I, X>
/// Set maximum size of of drawing area.
///
/// ```
/// use tengri::Layout;
/// let _ = "".max_w(1);
/// let _ = "".max_h(1);
/// let _ = "".max_wh(1, 1);
/// # fn doctest_layout_max () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(1u16, 1, 80, 25);
/// assert_eq!("12345".max_w(1).layout(area)?, Some(XYWH(1u16, 1, 1, 1)));
/// assert_eq!("12345".max_h(1).layout(area)?, Some(XYWH(1u16, 1, 1, 1)));
/// assert_eq!("12345".max_wh(1, 1).layout(area)?, Some(XYWH(1u16, 1, 5, 1)));
/// # Ok(()) }
/// ```
pub enum Max<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
@ -283,7 +298,6 @@ pub enum Max<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Max<T, I, X>, to: T|{
let area: XYWH<T::Unit> = to.area();
let (item, area) = match self {
@ -318,7 +332,6 @@ pub enum Exact<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Exact<T, I, X>, to: T|{
let area: XYWH<T::Unit> = to.area();
let (item, area) = match self {
@ -347,13 +360,11 @@ pub enum Pad<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Pad<T, I, X>, _to: T|{
todo!()
});
pub struct Align<T>(Option<Azimuth>, T);
impl_draw!(<S: Screen, T: Draw<S>,>|self: Align<T>, to: S|{
use Azimuth::*;
let XYWH(x0, y0, w0, h0) = to.area();
@ -405,13 +416,11 @@ pub struct Area<S: Screen, T: Draw<S>>(
pub Option<XYWH<S::Unit>>,
pub T
);
impl_draw!(<S: Screen, T: Draw<S>,>|self: Area<S, T>, to: S|{
to.clip(self.0, |to|self.1.draw(to))
});
pub struct Origin<T>(Option<Azimuth>, T);
impl_draw!(<S: Screen, T: Draw<S>,>|self: Origin<T>, _to: S|{
todo!()
});