From 78a3893cdf2ef8810d94ae212511fbb24a0bd704 Mon Sep 17 00:00:00 2001 From: Brady Fomegne Date: Tue, 24 Oct 2023 16:08:05 +0100 Subject: [PATCH] feat(preprocessor): implement inhibit mode --- engine/preprocessor/Cargo.toml | 3 + engine/preprocessor/src/lib.rs | 147 ++++++++++++++++++++++++++++++--- service/Cargo.toml | 3 +- 3 files changed, 140 insertions(+), 13 deletions(-) diff --git a/engine/preprocessor/Cargo.toml b/engine/preprocessor/Cargo.toml index 48d5926..b6780a4 100644 --- a/engine/preprocessor/Cargo.toml +++ b/engine/preprocessor/Cargo.toml @@ -10,6 +10,9 @@ readme = "README.md" authors = ["Brady Fomegne "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = [] +inhibit = [] [dependencies] keyboard-types = "0.7.0" diff --git a/engine/preprocessor/src/lib.rs b/engine/preprocessor/src/lib.rs index 44e50ce..dd009a9 100644 --- a/engine/preprocessor/src/lib.rs +++ b/engine/preprocessor/src/lib.rs @@ -29,6 +29,10 @@ //! let mut expecteds = VecDeque::from(vec![ //! Command::Pause, //! Command::KeyClick(Backspace), +//! #[cfg(feature = "inhibit")] +//! Command::Resume, +//! #[cfg(feature = "inhibit")] +//! Command::Pause, //! Command::KeyClick(Backspace), //! Command::CommitText("ç".to_owned()), //! Command::Resume, @@ -66,6 +70,8 @@ impl Preprocessor { } /// Cancel the previous operation. + /// NB: The inhibit feature don't manage the rollback + #[cfg(not(feature = "inhibit"))] fn rollback(&mut self) -> bool { self.stack.push_back(Command::KeyRelease(Key::Backspace)); @@ -94,9 +100,14 @@ impl Preprocessor { match (event.state, event.key) { (KeyState::Down, Key::Backspace) => { - self.pause(); - committed = self.rollback(); - self.resume(); + #[cfg(not(feature = "inhibit"))] + { + self.pause(); + committed = self.rollback(); + self.resume(); + } + #[cfg(feature = "inhibit")] + self.cursor.clear(); changed = true; } (KeyState::Down, Key::Character(character)) @@ -106,18 +117,25 @@ impl Preprocessor { .map(|e| e.is_alphanumeric() || e.is_ascii_punctuation()) .unwrap_or(false) => { + #[cfg(feature = "inhibit")] + self.pause(); + #[cfg(feature = "inhibit")] + self.stack.push_back(Command::KeyClick(Key::Backspace)); + let character = character.chars().next().unwrap(); if let Some(_in) = self.cursor.hit(character) { + #[cfg(not(feature = "inhibit"))] self.pause(); - let mut prev_cursor = self.cursor.clone(); prev_cursor.undo(); + #[cfg(not(feature = "inhibit"))] self.stack.push_back(Command::KeyClick(Key::Backspace)); // Remove the remaining code while let (None, 1.., ..) = prev_cursor.state() { prev_cursor.undo(); + #[cfg(not(feature = "inhibit"))] self.stack.push_back(Command::KeyClick(Key::Backspace)); } @@ -127,10 +145,13 @@ impl Preprocessor { } self.stack.push_back(Command::CommitText(_in)); + #[cfg(not(feature = "inhibit"))] self.resume(); committed = true; }; + #[cfg(feature = "inhibit")] + self.resume(); changed = true; } (KeyState::Down, Key::Shift | Key::CapsLock) => (), @@ -148,10 +169,13 @@ impl Preprocessor { pub fn commit(&mut self, text: &str) { self.pause(); + #[cfg(not(feature = "inhibit"))] while !self.cursor.is_empty() { self.stack.push_back(Command::KeyPress(Key::Backspace)); self.rollback(); } + #[cfg(feature = "inhibit")] + self.cursor.clear(); self.stack.push_back(Command::CommitText(text.to_owned())); self.resume(); // We clear the buffer @@ -204,23 +228,35 @@ mod tests { let data = utils::load_data("ccced ç\ncc ç"); let map = utils::build_map(data); let mut preprocessor = Preprocessor::new(map, 8); - webdriver::send_keys("\u{E00C}ccced") - .into_iter() - .for_each(|e| { - match e { - Event::Keyboard(e) => preprocessor.process(e), - _ => unimplemented!(), - }; - }); + webdriver::send_keys("ccced").into_iter().for_each(|e| { + match e { + Event::Keyboard(e) => preprocessor.process(e), + _ => unimplemented!(), + }; + }); let mut expecteds = VecDeque::from(vec![ + // c c Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ç".to_owned()), Command::Resume, + // c e d Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::KeyClick(Backspace), Command::CommitText("ç".to_owned()), @@ -246,7 +282,15 @@ mod tests { let mut expecteds = VecDeque::from(vec![ Command::Pause, + #[cfg(feature = "inhibit")] + Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, + #[cfg(not(feature = "inhibit"))] Command::KeyPress(Backspace), + #[cfg(not(feature = "inhibit"))] Command::KeyRelease(Backspace), Command::CommitText("word".to_owned()), Command::Resume, @@ -279,17 +323,23 @@ mod tests { preprocessor.clear_stack(); assert_eq!(preprocessor.get_input(), "ccced".to_owned()); preprocessor.process(backspace_event.clone()); + #[cfg(not(feature = "inhibit"))] assert_eq!(preprocessor.get_input(), "cc".to_owned()); + #[cfg(not(feature = "inhibit"))] preprocessor.process(backspace_event); assert_eq!(preprocessor.get_input(), "".to_owned()); let mut expecteds = VecDeque::from(vec![ Command::Pause, + #[cfg(not(feature = "inhibit"))] Command::KeyRelease(Backspace), Command::CommitText("ç".to_owned()), Command::Resume, + #[cfg(not(feature = "inhibit"))] Command::Pause, + #[cfg(not(feature = "inhibit"))] Command::KeyRelease(Backspace), + #[cfg(not(feature = "inhibit"))] Command::Resume, ]); @@ -319,72 +369,145 @@ mod tests { let mut expecteds = VecDeque::from(vec![ // Process + // u backspace Command::Pause, + #[cfg(feature = "inhibit")] + Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(not(feature = "inhibit"))] Command::KeyRelease(Backspace), + #[cfg(not(feature = "inhibit"))] Command::Resume, + // u u backspace Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ʉ".to_owned()), Command::Resume, + #[cfg(not(feature = "inhibit"))] Command::Pause, + #[cfg(not(feature = "inhibit"))] Command::KeyRelease(Backspace), + #[cfg(not(feature = "inhibit"))] + Command::Resume, + // u + #[cfg(feature = "inhibit")] + Command::Pause, + #[cfg(feature = "inhibit")] + Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] Command::Resume, + // c _ Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ç".to_owned()), Command::Resume, + // c e d Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::KeyClick(Backspace), Command::CommitText("ç".to_owned()), Command::Resume, + // u u Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ʉ".to_owned()), Command::Resume, + // a f 3 Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::KeyClick(Backspace), Command::CommitText("ʉ\u{304}ɑ\u{304}".to_owned()), Command::Resume, + // a f Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ɑ".to_owned()), Command::Resume, + // a f Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ɑ".to_owned()), Command::Resume, + // a f Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ɑ".to_owned()), Command::Resume, + // f Command::Pause, Command::KeyClick(Backspace), Command::KeyClick(Backspace), Command::CommitText("ɑɑ".to_owned()), Command::Resume, + // 3 Command::Pause, Command::KeyClick(Backspace), Command::KeyClick(Backspace), Command::KeyClick(Backspace), Command::CommitText("ɑ\u{304}ɑ\u{304}".to_owned()), Command::Resume, + // uu Command::Pause, Command::KeyClick(Backspace), + #[cfg(feature = "inhibit")] + Command::Resume, + #[cfg(feature = "inhibit")] + Command::Pause, Command::KeyClick(Backspace), Command::CommitText("ʉ".to_owned()), Command::Resume, + // 3 Command::Pause, Command::KeyClick(Backspace), Command::KeyClick(Backspace), diff --git a/service/Cargo.toml b/service/Cargo.toml index c151c43..a852445 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -19,12 +19,13 @@ path = "./src/main.rs" [features] default = ["rhai"] rhai = ["clafrica-config/rhai", "clafrica-translator/rhai"] +inhibit = ["clafrica-preprocessor/inhibit"] [dependencies] clap = { version = "4.4.6", features = ["derive"] } enigo = "0.1.3" clafrica-config = { version = "0.4.1", path = "../config", default-features = false } -clafrica-preprocessor = { version = "0.5.0", path = "../engine/preprocessor" } +clafrica-preprocessor = { version = "0.5.0", path = "../engine/preprocessor", default-features = false } clafrica-translator = { version = "0.0.1", path = "../engine/translator", default-features = false } rdev = "0.5.3"