Skip to content

Commit

Permalink
feat: Add allowed_words list to bypass typo restoration feature
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed May 30, 2024
1 parent 20c857f commit 47673df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct ConfigStore {
macro_table: BTreeMap<String, String>,
is_auto_toggle_enabled: bool,
is_gox_mode_enabled: bool,
allowed_words: Vec<String>,
}

fn parse_vec_string(line: String) -> Vec<String> {
Expand Down Expand Up @@ -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,
"{} = {}",
Expand Down Expand Up @@ -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();
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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";
5 changes: 5 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 47673df

Please sign in to comment.