minicrossterm/src/event/filter.rs
Nathan West f2fca91b30 Added basic trait implementations to all Commands (#363)
- Added basic trait implementations to all Commands
- Added `Debug` to all public-facing types
- Added `Clone` to several public-facing types, where relevant
- Added `Eq` to all `PartialEq` types
- Added `Debug` and `Clone` to a few internal types
- `ResetColor` uses `&'static str` instead of `String`
- Updated crossterm-winapi dependency
2020-01-28 20:20:26 +01:00

77 lines
1.9 KiB
Rust

use crate::event::InternalEvent;
/// Interface for filtering an `InternalEvent`.
pub(crate) trait Filter: Send + Sync + 'static {
/// Returns whether the given event fulfills the filter.
fn eval(&self, event: &InternalEvent) -> bool;
}
#[cfg(unix)]
#[derive(Debug, Clone)]
pub(crate) struct CursorPositionFilter;
#[cfg(unix)]
impl Filter for CursorPositionFilter {
fn eval(&self, event: &InternalEvent) -> bool {
if let InternalEvent::CursorPosition(_, _) = *event {
true
} else {
false
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct EventFilter;
impl Filter for EventFilter {
#[cfg(unix)]
fn eval(&self, event: &InternalEvent) -> bool {
if let InternalEvent::Event(_) = *event {
true
} else {
false
}
}
#[cfg(windows)]
fn eval(&self, _: &InternalEvent) -> bool {
true
}
}
#[derive(Debug, Clone)]
pub(crate) struct InternalEventFilter;
impl Filter for InternalEventFilter {
fn eval(&self, _: &InternalEvent) -> bool {
true
}
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::{
super::Event, CursorPositionFilter, EventFilter, Filter, InternalEvent, InternalEventFilter,
};
#[test]
fn test_cursor_position_filter_filters_cursor_position() {
assert!(!CursorPositionFilter.eval(&InternalEvent::Event(Event::Resize(10, 10))));
assert!(CursorPositionFilter.eval(&InternalEvent::CursorPosition(0, 0)));
}
#[test]
fn test_event_filter_filters_events() {
assert!(EventFilter.eval(&InternalEvent::Event(Event::Resize(10, 10))));
assert!(!EventFilter.eval(&InternalEvent::CursorPosition(0, 0)));
}
#[test]
fn test_event_filter_filters_internal_events() {
assert!(InternalEventFilter.eval(&InternalEvent::Event(Event::Resize(10, 10))));
assert!(InternalEventFilter.eval(&InternalEvent::CursorPosition(0, 0)));
}
}