use gui::Mergeable;
pub use termion::event::Key;
#[derive(Clone, Debug)]
pub enum Event {
Updated,
Quit,
#[cfg(not(feature = "readline"))]
Key(Key, ()),
#[cfg(feature = "readline")]
Key(Key, Vec<u8>),
}
#[cfg(test)]
impl Event {
pub fn is_updated(&self) -> bool {
matches!(self, Self::Updated)
}
}
impl From<u8> for Event {
fn from(b: u8) -> Self {
#[cfg(not(feature = "readline"))]
{
Event::Key(Key::Char(char::from(b)), ())
}
#[cfg(feature = "readline")]
{
Event::Key(Key::Char(char::from(b)), vec![b])
}
}
}
impl Mergeable for Event {
fn merge_with(self, other: Self) -> Self {
match (&self, &other) {
(Self::Key(..), _) | (_, Self::Key(..)) => panic!(
"Attempting to merge incompatible events: {:?} & {:?}",
self, other
),
(Self::Updated, Self::Updated) => self,
(Self::Quit, _) | (_, Self::Quit) => Self::Quit,
}
}
}