Skip to content

Commit

Permalink
fix(clafrica): conflict between translator and processor
Browse files Browse the repository at this point in the history
Before, it's was not really possible to use the translator and the
processor simultaneously.
  • Loading branch information
pythonbrad committed Sep 16, 2023
1 parent 23c1292 commit 6073300
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
1 change: 1 addition & 0 deletions clafrica/data/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ hi = "./scripts/hi.rhai"
hello = "hi"
heli = "helicopter"
hea = "health"
vuue = "vʉe"
39 changes: 21 additions & 18 deletions clafrica/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +102,31 @@ pub fn run(
.expect("We couldn't cancel the special function key");
is_special_pressed = false;

if let Some(predicate) = frontend.get_selected_predicate() {
processor.commit(&predicate.0, &predicate.1, &predicate.2);
if let Some((_code, _remaining_code, text)) = frontend.get_selected_predicate() {
processor.commit(text);
frontend.clear_predicates();
}
}
_ if is_special_pressed => (),
_ => {
let (changed, committed) = processor.process(event);
let (changed, _committed) = processor.process(event);

if changed {
let input = processor.get_input();

frontend.clear_predicates();

if !committed {
translator.translate(&input).iter().for_each(
|(code, remaining_code, texts, translated)| {
texts.iter().for_each(|text| {
if auto_commit && *translated {
processor.commit(code, remaining_code, text);
} else if !text.is_empty() {
frontend.add_predicate(code, remaining_code, text);
}
});
},
);
};
translator.translate(&input).iter().for_each(
|(code, remaining_code, texts, translated)| {
texts.iter().for_each(|text| {
if auto_commit && *translated {
processor.commit(text);
} else if !text.is_empty() {
frontend.add_predicate(code, remaining_code, text);
}
});
},
);

frontend.set_input(&input);
frontend.display();
Expand All @@ -152,7 +150,6 @@ mod tests {
$(
thread::sleep($delay);
rdev::simulate(&KeyPress($key)).unwrap();
thread::sleep($delay);
rdev::simulate(&KeyRelease($key)).unwrap();
)*
);
Expand Down Expand Up @@ -209,7 +206,7 @@ mod tests {

#[test]
fn test_simple() {
let typing_speed_ms = Duration::from_millis(300);
let typing_speed_ms = Duration::from_millis(500);

// To detect excessive backspace
const LIMIT: &str = "bbb";
Expand Down Expand Up @@ -279,5 +276,11 @@ mod tests {
input!(ControlRight, typing_speed_ms);
rdev::simulate(&KeyRelease(ControlLeft)).unwrap();
output!(textfield, format!("{LIMIT}uuɑαⱭⱭɑɑhihellohi"));
input!(Escape, typing_speed_ms);

// We verify that we don't have a conflict
// between the translator and the processor
input!(KeyV KeyU KeyU KeyE, typing_speed_ms);
output!(textfield, format!("{LIMIT}uuɑαⱭⱭɑɑhihellohivʉe"));
}
}
51 changes: 29 additions & 22 deletions clafrica/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ impl Processor {
}
}

fn rollback(&mut self) -> bool {
self.keyboard.key_up(Key::Backspace);

if let Some(out) = self.cursor.undo() {
(1..out.chars().count()).for_each(|_| self.keyboard.key_click(Key::Backspace));

// Clear the remaining code
while let (None, 1.., ..) = self.cursor.state() {
self.cursor.undo();
}

if let (Some(_in), ..) = self.cursor.state() {
self.keyboard.key_sequence(&_in);
}

true
} else {
false
}
}

pub fn process(&mut self, event: Event) -> (bool, bool) {
let character = event.name.and_then(|s| s.chars().next());
let is_valid = character
Expand All @@ -26,25 +47,9 @@ impl Processor {

match event.event_type {
EventType::KeyPress(E_Key::Backspace) => {
if let Some(out) = self.cursor.undo() {
self.pause();
self.keyboard.key_up(Key::Backspace);

(1..out.chars().count()).for_each(|_| self.keyboard.key_click(Key::Backspace));

// Clear the remaining code
while let (None, 1.., ..) = self.cursor.state() {
self.cursor.undo();
}

if let (Some(_in), ..) = self.cursor.state() {
self.keyboard.key_sequence(&_in);
}

self.resume();
committed = true;
}

self.pause();
committed = self.rollback();
self.resume();
changed = true;
}
EventType::KeyPress(
Expand Down Expand Up @@ -90,10 +95,12 @@ impl Processor {
(changed, committed)
}

pub fn commit(&mut self, code: &str, remaining_code: &str, text: &str) {
pub fn commit(&mut self, text: &str) {
self.pause();
(0..code.len() - remaining_code.len())
.for_each(|_| self.keyboard.key_click(Key::Backspace));
while !self.cursor.is_empty() {
self.keyboard.key_down(Key::Backspace);
self.rollback();
}
self.keyboard.key_sequence(text);
self.resume();
// We clear the buffer
Expand Down

0 comments on commit 6073300

Please sign in to comment.