Skip to content

Commit

Permalink
feat(wasm): simulate files
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Nov 3, 2024
1 parent 15c1f65 commit 25e23ae
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 87 deletions.
8 changes: 5 additions & 3 deletions parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,18 @@ pub fn new_from_file(p: &Path) -> MResult<Cfg> {
parse_cfg(p)
}

pub fn new_from_str(cfg_text: &str, file_content: Option<String>) -> MResult<Cfg> {
pub fn new_from_str(cfg_text: &str, file_content: HashMap<String, String>) -> MResult<Cfg> {
let mut s = ParserState::default();
let icfg = parse_cfg_raw_string(
cfg_text,
&mut s,
&PathBuf::from("configuration"),
&mut FileContentProvider {
get_file_content_fn: &mut move |_| match &file_content {
get_file_content_fn: &mut move |fname| match file_content
.get(fname.to_string_lossy().as_ref())
{
Some(s) => Ok(s.clone()),
None => Err("include is not supported".into()),
None => Err("File is not known".into()),
},
},
DEF_LOCAL_KEYS,
Expand Down
2 changes: 1 addition & 1 deletion src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl Kanata {
Ok(Arc::new(Mutex::new(Self::new(args)?)))
}

pub fn new_from_str(cfg: &str, file_content: Option<String>) -> Result<Self> {
pub fn new_from_str(cfg: &str, file_content: HashMap<String, String>) -> Result<Self> {
let cfg = match cfg::new_from_str(cfg, file_content) {
Ok(c) => c,
Err(e) => {
Expand Down
12 changes: 6 additions & 6 deletions src/kanata/output_logic/zippychord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl ZchState {
kb.release_key(OsCode::KEY_RIGHTALT)?;
}

for key_to_send in &a.zch_output {
for key_to_send in a.zch_output.iter().copied() {
#[cfg(feature = "interception_driver")]
{
// Note: every 5 keys on Windows Interception, do a sleep because
Expand All @@ -372,12 +372,12 @@ impl ZchState {

let typed_osc = match key_to_send {
ZchOutput::Lowercase(osc) => {
type_osc(*osc, kb, &self.zchd)?;
type_osc(osc, kb, &self.zchd)?;
osc
}
ZchOutput::Uppercase(osc) => {
maybe_press_sft_during_activation(released_sft, kb, &self.zchd)?;
type_osc(*osc, kb, &self.zchd)?;
type_osc(osc, kb, &self.zchd)?;
maybe_release_sft_during_activation(released_sft, kb, &self.zchd)?;
osc
}
Expand All @@ -392,21 +392,21 @@ impl ZchState {
// always released at the beginning and pressed at the end if it was
// previously being held.
kb.press_key(OsCode::KEY_RIGHTALT)?;
type_osc(*osc, kb, &self.zchd)?;
type_osc(osc, kb, &self.zchd)?;
kb.release_key(OsCode::KEY_RIGHTALT)?;
osc
}
ZchOutput::ShiftAltGr(osc) => {
kb.press_key(OsCode::KEY_RIGHTALT)?;
maybe_press_sft_during_activation(released_sft, kb, &self.zchd)?;
type_osc(*osc, kb, &self.zchd)?;
type_osc(osc, kb, &self.zchd)?;
maybe_release_sft_during_activation(released_sft, kb, &self.zchd)?;
kb.release_key(OsCode::KEY_RIGHTALT)?;
osc
}
};

if *typed_osc == OsCode::KEY_BACKSPACE {
if typed_osc == OsCode::KEY_BACKSPACE {
self.zchd.zchd_characters_to_delete_on_next_activation -= 1;
// Improvement: there are many other keycodes that might be sent that
// aren't printable. But for now, just include backspace.
Expand Down
13 changes: 9 additions & 4 deletions src/tests/sim_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{
str_to_oscode, Kanata,
};

use rustc_hash::FxHashMap;

mod block_keys_tests;
mod capsword_sim_tests;
mod chord_sim_tests;
Expand All @@ -24,17 +26,20 @@ mod unmod_sim_tests;
mod zippychord_sim_tests;

fn simulate<S: AsRef<str>>(cfg: S, sim: S) -> String {
simulate_with_file_content(cfg, sim, None)
simulate_with_file_content(cfg, sim, Default::default())
}

fn simulate_with_file_content<S: AsRef<str>>(cfg: S, sim: S, file_content: Option<S>) -> String {
fn simulate_with_file_content<S: AsRef<str>>(
cfg: S,
sim: S,
file_content: FxHashMap<String, String>,
) -> String {
init_log();
let _lk = match CFG_PARSE_LOCK.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let mut k = Kanata::new_from_str(cfg.as_ref(), file_content.map(|s| s.as_ref().to_owned()))
.expect("failed to parse cfg");
let mut k = Kanata::new_from_str(cfg.as_ref(), file_content).expect("failed to parse cfg");
for pair in sim.as_ref().split_whitespace() {
match pair.split_once(':') {
Some((kind, val)) => match kind {
Expand Down
Loading

0 comments on commit 25e23ae

Please sign in to comment.