diff --git a/engine/src/edn.rs b/engine/src/edn.rs deleted file mode 100644 index c7b7e813..00000000 --- a/engine/src/edn.rs +++ /dev/null @@ -1 +0,0 @@ -use crate::*; diff --git a/engine/src/input.rs b/engine/src/input.rs index 0cc8718a..90b6dbfe 100644 --- a/engine/src/input.rs +++ b/engine/src/input.rs @@ -1,6 +1,8 @@ use crate::*; mod handle; pub use self::handle::*; +mod command; pub use self::command::*; +mod event_map; pub use self::event_map::*; /// Current input state pub trait Input { diff --git a/src/command.rs b/engine/src/input/command.rs similarity index 50% rename from src/command.rs rename to engine/src/input/command.rs index 03f415d9..b640dc9b 100644 --- a/src/command.rs +++ b/engine/src/input/command.rs @@ -1,46 +1,4 @@ use crate::*; - -#[macro_export] macro_rules! keymap { - ( - $(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty - { $($key:expr => $handler:expr),* $(,)? } $(,)? - ) => { - pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap { - fallback: None, - bindings: &[ $(($key, &|$state|Some($handler)),)* ] - }; - input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); - }; - ( - $(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty - { $($key:expr => $handler:expr),* $(,)? }, $default:expr - ) => { - pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap { - fallback: Some(&|$state, $input|Some($default)), - bindings: &[ $(($key, &|$state|Some($handler)),)* ] - }; - input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); - }; -} - -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> -} -impl<'a, S, I: PartialEq, C> EventMap<'a, S, I, C> { - pub fn handle (&self, state: &S, input: &I) -> Option { - for (binding, handler) in self.bindings.iter() { - if input == binding { - return handler(state) - } - } - if let Some(fallback) = self.fallback { - fallback(state, input) - } else { - None - } - } -} pub trait Command: Send + Sync + Sized { fn execute (self, state: &mut S) -> Perhaps; fn delegate (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps { diff --git a/engine/src/input/event_map.rs b/engine/src/input/event_map.rs new file mode 100644 index 00000000..ee735217 --- /dev/null +++ b/engine/src/input/event_map.rs @@ -0,0 +1,43 @@ +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> +} +impl<'a, S, I: PartialEq, C> EventMap<'a, S, I, C> { + pub fn handle (&self, state: &S, input: &I) -> Option { + for (binding, handler) in self.bindings.iter() { + if input == binding { + return handler(state) + } + } + if let Some(fallback) = self.fallback { + fallback(state, input) + } else { + None + } + } +} + +#[macro_export] macro_rules! keymap { + ( + $(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty + { $($key:expr => $handler:expr),* $(,)? } $(,)? + ) => { + pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap { + fallback: None, + bindings: &[ $(($key, &|$state|Some($handler)),)* ] + }; + input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); + }; + ( + $(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty + { $($key:expr => $handler:expr),* $(,)? }, $default:expr + ) => { + pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap { + fallback: Some(&|$state, $input|Some($default)), + bindings: &[ $(($key, &|$state|Some($handler)),)* ] + }; + input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); + }; +} diff --git a/src/lib.rs b/src/lib.rs index 9f109b7c..678590ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,10 +12,9 @@ pub(crate) use ::tek_layout::{ *, tek_engine::{ Usually, Perhaps, - Engine, Size, Area, - Output, Content, Render, Thunk, render, - Input, Handle, handle, - kexp, kpat, + Output, Content, Render, Thunk, render, Engine, Size, Area, + Input, handle, Handle, command, Command, input_to_command, InputToCommand, + keymap, kexp, kpat, EventMap, tui::{ Tui, TuiIn, key, ctrl, shift, alt, @@ -57,7 +56,6 @@ pub mod arranger; pub use self::arranger::*; pub mod border; pub use self::border::*; pub mod clock; pub use self::clock::*; pub mod color; pub use self::color::*; -pub mod command; pub use self::command::*; pub mod field; pub use self::field::*; pub mod file; pub use self::file::*; pub mod focus; pub use self::focus::*;