diff --git a/examples/mode_01.rs b/examples/mode_01.rs index 39031c4..0ad5241 100644 --- a/examples/mode_01.rs +++ b/examples/mode_01.rs @@ -44,6 +44,14 @@ impl Interpret for State { } } } +fn try_to_u8 (src: Perhaps<&str>) -> Perhaps { + use std::str::FromStr; + if let Some(src) = src? { + Ok(Some(u8::from_str(src)?)) + } else { + Ok(None) + } +} impl Interpret>> for State { fn interpret_word (&self, to: &mut Tui, sym: &impl Language) -> Perhaps> { match sym.src()? { diff --git a/src/draw.rs b/src/draw.rs index 36690f8..9d27ad3 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -13,16 +13,20 @@ use crate::*; /// } /// impl Screen for TestOut { /// type Unit = u16; -/// fn show (&mut self, _: impl Draw) -> Perhaps> -/// { println!("placed"); Ok(None) } -/// fn area (&self) -> XYWH -/// { Default::default() } +/// fn show (&mut self, _: impl Draw) -> Perhaps> { +/// println!("placed"); +/// Ok(None) +/// } +/// fn area (&self) -> XYWH { +/// Default::default() +/// } /// fn clip ( /// &mut self, /// area: impl Into>>, /// draw: impl FnOnce(&mut Self)->T -/// ) -> T -/// { draw(self } +/// ) -> T { +/// draw(self) +/// } /// } /// /// impl_draw!(|self: String, to: TestOut|{ diff --git a/src/draw/layout.rs b/src/draw/layout.rs index 9074158..ecc1316 100644 --- a/src/draw/layout.rs +++ b/src/draw/layout.rs @@ -160,10 +160,14 @@ pub trait Layout: Draw + 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> { +/// 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> { __(PhantomData), @@ -171,7 +175,6 @@ pub enum Full> { H(I), WH(I), } - impl_draw!(,>|self: Full, to: T|{ let XYWH(x0, y0, w0, h0) = to.area(); match self { @@ -197,10 +200,14 @@ impl_draw!(,>|self: Full, 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> { +/// 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, X: Into>> { __(PhantomData), @@ -208,7 +215,6 @@ pub enum Push, X: Into>> { Y(I, X), XY(I, X, X), } - impl_draw!(, X: Into>,>|self: Push, to: T|{ match self { Self::__(_) => unreachable!(), @@ -234,10 +240,14 @@ impl_draw!(, X: Into>,>|self: Push Result<(), Box> { +/// 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, X: Into>> { __(PhantomData), @@ -245,7 +255,6 @@ pub enum Pull, X: Into>> { Y(I, X), XY(I, X, X), } - impl_draw!(, X: Into>,>|self: Pull, _to: T|{ todo!() }); @@ -253,10 +262,14 @@ impl_draw!(, X: Into>,>|self: Pull Result<(), Box> { +/// 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, X: Into>> { __(PhantomData), @@ -264,7 +277,6 @@ pub enum Min, X: Into>> { H(I, X), WH(I, X, X), } - impl_draw!(, X: Into>,>|self: Min, _to: T|{ todo!() }); @@ -272,10 +284,13 @@ impl_draw!(, X: Into>,>|self: Min /// 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> { +/// 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, X: Into>> { __(PhantomData), @@ -283,7 +298,6 @@ pub enum Max, X: Into>> { H(I, X), WH(I, X, X), } - impl_draw!(, X: Into>,>|self: Max, to: T|{ let area: XYWH = to.area(); let (item, area) = match self { @@ -318,7 +332,6 @@ pub enum Exact, X: Into>> { H(I, X), WH(I, X, X), } - impl_draw!(, X: Into>,>|self: Exact, to: T|{ let area: XYWH = to.area(); let (item, area) = match self { @@ -347,13 +360,11 @@ pub enum Pad, X: Into>> { H(I, X), WH(I, X, X), } - impl_draw!(, X: Into>,>|self: Pad, _to: T|{ todo!() }); pub struct Align(Option, T); - impl_draw!(,>|self: Align, to: S|{ use Azimuth::*; let XYWH(x0, y0, w0, h0) = to.area(); @@ -405,13 +416,11 @@ pub struct Area>( pub Option>, pub T ); - impl_draw!(,>|self: Area, to: S|{ to.clip(self.0, |to|self.1.draw(to)) }); pub struct Origin(Option, T); - impl_draw!(,>|self: Origin, _to: S|{ todo!() });