Skip to content

Commit

Permalink
Experimental: Automatically restore mistyped words
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed Jul 8, 2023
1 parent 4d5cebc commit 36f5b34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
// be around 10 to 12.
const MAX_POSSIBLE_WORD_LENGTH: usize = 10;
const MAX_DUPLICATE_LENGTH: usize = 4;
const TONE_DUPLICATE_PATTERNS: [&str; 5] = ["ss", "ff", "jj", "rr", "xx"];
const TONE_DUPLICATE_PATTERNS: [&str; 6] = ["ss", "ff", "jj", "rr", "xx", "ww"];

pub static mut INPUT_STATE: Lazy<InputState> = Lazy::new(InputState::new);

Expand Down Expand Up @@ -184,6 +184,14 @@ impl InputState {
self.should_track = true;
}

pub fn get_typing_buffer(&self) -> &str {
&self.buffer
}

pub fn get_displaying_word(&self) -> &str {
&self.display_buffer
}

pub fn stop_tracking(&mut self) {
self.clear();
self.should_track = false;
Expand Down
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ fn do_transform_keys(handle: Handle, is_delete: bool) -> bool {
false
}

fn do_restore_word(handle: Handle) {
unsafe {
let backspace_count = INPUT_STATE.get_backspace_count(true);
debug!("Backspace count: {}", backspace_count);
_ = send_backspace(handle, backspace_count);
let typing_buffer = INPUT_STATE.get_typing_buffer();
_ = send_string(handle, typing_buffer);
debug!("Sent: {:?}", typing_buffer);
INPUT_STATE.replace(typing_buffer.to_owned());
}
}

fn event_handler(handle: Handle, keycode: Option<char>, modifiers: KeyModifier) -> bool {
unsafe {
match keycode {
Expand All @@ -54,6 +66,9 @@ fn event_handler(handle: Handle, keycode: Option<char>, modifiers: KeyModifier)
if INPUT_STATE.is_enabled() {
match keycode {
KEY_ENTER | KEY_TAB | KEY_SPACE | KEY_ESCAPE => {
if !vi::validation::is_valid_word(INPUT_STATE.get_displaying_word()) {
do_restore_word(handle);
}
INPUT_STATE.new_word();
}
KEY_DELETE => {
Expand Down

0 comments on commit 36f5b34

Please sign in to comment.