use std::time::Instant;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputMode {
Normal,
Search,
}
#[derive(Debug, Clone)]
pub struct InputState {
mode: InputMode,
pending_key: Option<char>,
pending_key_time: Option<Instant>,
}
impl Default for InputState {
fn default() -> Self {
Self {
mode: InputMode::Normal,
pending_key: None,
pending_key_time: None,
}
}
}
impl InputState {
pub fn new() -> Self {
Self::default()
}
pub fn mode(&self) -> InputMode {
self.mode
}
pub fn pending_key(&self) -> Option<char> {
self.pending_key
}
pub fn pending_key_time(&self) -> Option<Instant> {
self.pending_key_time
}
pub fn enter_search(&mut self) {
self.mode = InputMode::Search;
}
pub fn exit_search(&mut self) {
self.mode = InputMode::Normal;
}
pub fn is_searching(&self) -> bool {
self.mode == InputMode::Search
}
pub fn set_pending_key(
&mut self,
key: char,
) {
self.pending_key = Some(key);
self.pending_key_time = Some(Instant::now());
}
pub fn clear_pending_key(&mut self) {
self.pending_key = None;
self.pending_key_time = None;
}
pub fn is_pending_key_expired(&self) -> bool {
if let (Some(_), Some(time)) = (self.pending_key, self.pending_key_time) {
time.elapsed() > std::time::Duration::from_secs(2)
} else {
false
}
}
}
#[cfg(test)]
#[path = "input_tests.rs"]
mod input_tests;