diff --git a/edn/src/lib.rs b/edn/src/lib.rs index 114fd675..3b815d7d 100644 --- a/edn/src/lib.rs +++ b/edn/src/lib.rs @@ -30,8 +30,8 @@ mod edn_token; pub use self::edn_token::*; } #[cfg(test)] #[test] fn test_edn_layout () -> Result<(), ParseError> { - EdnItem::::read_all(include_str!("../tek/examples/edn01.edn"))?; - EdnItem::::read_all(include_str!("../tek/examples/edn02.edn"))?; + EdnItem::::read_all(include_str!("../../tek/examples/edn01.edn"))?; + EdnItem::::read_all(include_str!("../../tek/examples/edn02.edn"))?; //panic!("{layout:?}"); //let content = >::from(&layout); Ok(()) diff --git a/input/src/engine.rs b/input/src/engine.rs deleted file mode 100644 index 6c3c78b8..00000000 --- a/input/src/engine.rs +++ /dev/null @@ -1,13 +0,0 @@ -/// Event source -pub trait Input: Send + Sync + Sized { - /// Type of input event - type Event; - /// Result of handling input - type Handled; - /// Currently handled event - fn event (&self) -> &Self::Event; - /// Whether component should exit - fn is_done (&self) -> bool; - /// Mark component as done - fn done (&self); -} diff --git a/input/src/event_map.rs b/input/src/event_map.rs index bab7d934..4a7e2605 100644 --- a/input/src/event_map.rs +++ b/input/src/event_map.rs @@ -1,3 +1,5 @@ +use crate::*; + pub struct EventMap<'a, S, I: PartialEq, C> { pub bindings: &'a [(I, &'a dyn Fn(&S) -> Option)], pub fallback: Option<&'a dyn Fn(&S, &I) -> Option> @@ -40,3 +42,32 @@ impl<'a, S, I: PartialEq, C> EventMap<'a, S, I, C> { input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); }; } +#[macro_export] macro_rules! input_to_command { + (<$($l:lifetime),+> $Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => { + impl<$($l),+> InputToCommand<$Input, $State> for $Command { + fn input_to_command ($state: &$State, $input: &$Input) -> Option { + Some($handler) + } + } + }; + ($Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => { + impl InputToCommand<$Input, $State> for $Command { + fn input_to_command ($state: &$State, $input: &$Input) -> Option { + Some($handler) + } + } + } +} + +pub trait InputToCommand: Command + Sized { + fn input_to_command (state: &S, input: &I) -> Option; + fn execute_with_state (state: &mut S, input: &I) -> Perhaps { + Ok(if let Some(command) = Self::input_to_command(state, input) { + let _undo = command.execute(state)?; + Some(true) + } else { + None + }) + } +} + diff --git a/input/src/input.rs b/input/src/input.rs index ea6bbc5e..615223ba 100644 --- a/input/src/input.rs +++ b/input/src/input.rs @@ -1,30 +1,14 @@ use crate::*; -#[macro_export] macro_rules! input_to_command { - (<$($l:lifetime),+> $Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => { - impl<$($l),+> InputToCommand<$Input, $State> for $Command { - fn input_to_command ($state: &$State, $input: &$Input) -> Option { - Some($handler) - } - } - }; - ($Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => { - impl InputToCommand<$Input, $State> for $Command { - fn input_to_command ($state: &$State, $input: &$Input) -> Option { - Some($handler) - } - } - } +/// Event source +pub trait Input: Send + Sync + Sized { + /// Type of input event + type Event; + /// Result of handling input + type Handled; + /// Currently handled event + fn event (&self) -> &Self::Event; + /// Whether component should exit + fn is_done (&self) -> bool; + /// Mark component as done + fn done (&self); } - -pub trait InputToCommand: Command + Sized { - fn input_to_command (state: &S, input: &I) -> Option; - fn execute_with_state (state: &mut S, input: &I) -> Perhaps { - Ok(if let Some(command) = Self::input_to_command(state, input) { - let _undo = command.execute(state)?; - Some(true) - } else { - None - }) - } -} - diff --git a/input/src/lib.rs b/input/src/lib.rs index b9798d08..6800ab87 100644 --- a/input/src/lib.rs +++ b/input/src/lib.rs @@ -1,44 +1,25 @@ #![feature(associated_type_defaults)] //mod component; pub use self::component::*; -mod engine; pub use self::engine::*; mod input; pub use self::input::*; mod handle; pub use self::handle::*; mod command; pub use self::command::*; mod event_map; pub use self::event_map::*; -pub(crate) use std::marker::PhantomData; +/// Standard error trait. pub(crate) use std::error::Error; - /// Standard result type. pub(crate) type Usually = Result>; - /// Standard optional result type. pub(crate) type Perhaps = Result, Box>; -#[cfg(test)] #[test] fn test_dimensions () { - assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]); -} - -#[cfg(test)] #[test] fn test_stub_engine () -> Usually<()> { +#[cfg(test)] #[test] fn test_stub_input () -> Usually<()> { use crate::*; - struct TestEngine(bool); struct TestInput(bool); - struct TestOutput([u16;4]); enum TestEvent { Test1 } - impl Engine for TestEngine { - type Input = TestInput; - type Handled = (); - type Output = TestOutput; - type Unit = u16; - type Size = [u16;2]; - type Area = [u16;4]; - fn exited (&self) -> bool { - self.0 - } - } - impl Input for TestInput { + impl Input for TestInput { type Event = TestEvent; + type Handled = (); fn event (&self) -> &Self::Event { &TestEvent::Test1 } @@ -47,21 +28,5 @@ pub(crate) type Perhaps = Result, Box>; } fn done (&self) {} } - impl Output for TestOutput { - fn area (&self) -> [u16;4] { - self.0 - } - fn area_mut (&mut self) -> &mut [u16;4] { - &mut self.0 - } - fn place (&mut self, _: [u16;4], _: &impl Render) { - () - } - } - impl Content for String { - fn render (&self, to: &mut TestOutput) { - to.area_mut().set_w(self.len() as u16); - } - } Ok(()) } diff --git a/output/src/lib.rs b/output/src/lib.rs index c8c614e2..ffee4381 100644 --- a/output/src/lib.rs +++ b/output/src/lib.rs @@ -35,23 +35,55 @@ pub type Usually = Result>; /// Standard optional result type. pub type Perhaps = Result, Box>; +#[cfg(test)] #[test] fn test_stub_output () -> Usually<()> { + use crate::*; + struct TestOutput([u16;4]); + impl Output for TestOutput { + type Unit = u16; + type Size = [u16;2]; + type Area = [u16;4]; + fn area (&self) -> [u16;4] { + self.0 + } + fn area_mut (&mut self) -> &mut [u16;4] { + &mut self.0 + } + fn place (&mut self, _: [u16;4], _: &impl Render) { + () + } + } + impl Content for String { + fn render (&self, to: &mut TestOutput) { + to.area_mut().set_w(self.len() as u16); + } + } + Ok(()) +} + +#[cfg(test)] #[test] fn test_dimensions () { + use crate::*; + assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]); +} + #[cfg(test)] #[test] fn test_layout () -> Usually<()> { - use ::tek_tui::Tui; + use ::tek_tui::{*, tek_output::*}; let area: [u16;4] = [10, 10, 20, 20]; let unit = (); assert_eq!(Content::::layout(&unit, area), [20, 20, 0, 0]); - assert_eq!(Fill::::x(unit).layout(area), [10, 20, 20, 0]); - assert_eq!(Fill::::y(unit).layout(area), [20, 10, 0, 20]); - assert_eq!(Fill::::xy(unit).layout(area), area); + assert_eq!(Content::::layout(&Fill::<()>::x(unit), area), [10, 20, 20, 0]); + assert_eq!(Render::::layout(&Fill::<()>::x(unit), area), [10, 20, 20, 0]); + + assert_eq!(Fill::<()>::y(unit).layout(area), [20, 10, 0, 20]); + assert_eq!(Fill::<()>::xy(unit).layout(area), area); assert_eq!(Fixed::::x(4, unit).layout(area), [18, 20, 4, 0]); assert_eq!(Fixed::::y(4, unit).layout(area), [20, 18, 0, 4]); assert_eq!(Fixed::::xy(4, 4, unit).layout(area), [18, 18, 4, 4]); - let four = ||Fixed::::xy(4, 4, unit); + let four = ||Fixed::::xy(4, 4, unit); assert_eq!(Align::nw(four()).layout(area), [10, 10, 4, 4]); assert_eq!(Align::n(four()).layout(area), [18, 10, 4, 4]); @@ -62,7 +94,7 @@ pub type Perhaps = Result, Box>; assert_eq!(Align::sw(four()).layout(area), [10, 26, 4, 4]); assert_eq!(Align::w(four()).layout(area), [10, 18, 4, 4]); - let two_by_four = ||Fixed::::xy(4, 2, unit); + let two_by_four = ||Fixed::::xy(4, 2, unit); assert_eq!(Align::nw(two_by_four()).layout(area), [10, 10, 4, 2]); assert_eq!(Align::n(two_by_four()).layout(area), [18, 10, 4, 2]);