From 47673df91db09f99188c0e482c349dbb4ab7b0af Mon Sep 17 00:00:00 2001 From: Huy Tran Date: Thu, 30 May 2024 11:34:51 -0700 Subject: [PATCH] feat: Add allowed_words list to bypass typo restoration feature --- src/config.rs | 16 ++++++++++++++++ src/input.rs | 5 +++++ src/main.rs | 9 ++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index a40a2d7..9e62f64 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,7 @@ pub struct ConfigStore { macro_table: BTreeMap, is_auto_toggle_enabled: bool, is_gox_mode_enabled: bool, + allowed_words: Vec, } fn parse_vec_string(line: String) -> Vec { @@ -63,6 +64,12 @@ impl ConfigStore { writeln!(file, "{} = {}", TYPING_METHOD_CONFIG_KEY, self.method)?; writeln!(file, "{} = {}", VN_APPS_CONFIG_KEY, self.vn_apps.join(","))?; writeln!(file, "{} = {}", EN_APPS_CONFIG_KEY, self.en_apps.join(","))?; + writeln!( + file, + "{} = {}", + ALLOWED_WORDS_CONFIG_KEY, + self.allowed_words.join(",") + )?; writeln!( file, "{} = {}", @@ -94,6 +101,7 @@ impl ConfigStore { macro_table: BTreeMap::new(), is_auto_toggle_enabled: false, is_gox_mode_enabled: false, + allowed_words: vec!["đc".to_string()], }; let config_path = ConfigStore::get_config_path(); @@ -107,6 +115,9 @@ impl ConfigStore { TYPING_METHOD_CONFIG_KEY => config.method = right.to_string(), VN_APPS_CONFIG_KEY => config.vn_apps = parse_vec_string(right.to_string()), EN_APPS_CONFIG_KEY => config.en_apps = parse_vec_string(right.to_string()), + ALLOWED_WORDS_CONFIG_KEY => { + config.allowed_words = parse_vec_string(right.to_string()) + } AUTOS_TOGGLE_ENABLED_CONFIG_KEY => { config.is_auto_toggle_enabled = matches!(right.trim(), "true") } @@ -176,6 +187,10 @@ impl ConfigStore { self.save(); } + pub fn is_allowed_word(&self, word: &str) -> bool { + self.allowed_words.contains(&word.to_string()) + } + pub fn is_auto_toggle_enabled(&self) -> bool { self.is_auto_toggle_enabled } @@ -231,3 +246,4 @@ const MACRO_ENABLED_CONFIG_KEY: &str = "is_macro_enabled"; const AUTOS_TOGGLE_ENABLED_CONFIG_KEY: &str = "is_auto_toggle_enabled"; const MACROS_CONFIG_KEY: &str = "macros"; const GOX_MODE_CONFIG_KEY: &str = "is_gox_mode_enabled"; +const ALLOWED_WORDS_CONFIG_KEY: &str = "allowed_words"; diff --git a/src/input.rs b/src/input.rs index 6d35259..e20be84 100644 --- a/src/input.rs +++ b/src/input.rs @@ -467,4 +467,9 @@ impl InputState { pub fn save_previous_modifiers(&mut self, modifiers: KeyModifier) { self.previous_modifiers = modifiers; } + + pub fn is_allowed_word(&self, word: &str) -> bool { + let config = CONFIG_MANAGER.lock().unwrap(); + return config.is_allowed_word(word); + } } diff --git a/src/main.rs b/src/main.rs index 354526e..272dc36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,11 @@ fn do_transform_keys(handle: Handle, is_delete: bool) -> bool { fn do_restore_word(handle: Handle) { unsafe { + println!( + "Restoring. Remove typed: {} - Send original: {}", + INPUT_STATE.get_displaying_word(), + INPUT_STATE.get_typing_buffer() + ); let backspace_count = INPUT_STATE.get_backspace_count(true); debug!("Backspace count: {}", backspace_count); _ = send_backspace(handle, backspace_count); @@ -152,10 +157,12 @@ fn event_handler( let is_valid_word = vi::validation::is_valid_word( INPUT_STATE.get_displaying_word(), ); + let is_allowed_word = INPUT_STATE + .is_allowed_word(INPUT_STATE.get_displaying_word()); let is_transformed_word = !INPUT_STATE .get_typing_buffer() .eq(INPUT_STATE.get_displaying_word()); - if is_transformed_word && !is_valid_word { + if is_transformed_word && !is_valid_word && !is_allowed_word { do_restore_word(handle); }