Skip to content

Commit

Permalink
feat: overload bind_key Rhai methods
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Apr 19, 2024
1 parent 148470f commit 52a323f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 18 deletions.
12 changes: 6 additions & 6 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ fn open_file_menu(dir) {
// TODO could we hit recursion depth limit when opening many dirs?
open_file_menu(child_dir);
};
keymap.bind_key_for_special_candidate("enter", child_dir, "View directory", prog, false);
keymap.bind_key_for_special_candidate("enter", child_dir, "View directory", prog);
}
keymap.bind_key_for_special_candidate("enter", "..", "View directory", || {
open_file_menu(dir + "/..");
} , false);
});

s::open_menu("file_selection", keymap);
}
Expand All @@ -27,14 +27,14 @@ s::load_language("data/json_lang.ron");
s::open_doc("data/pokemon.json");

let keymap = new_keymap();
keymap.bind_key("h", "Say Hello", || print("hello world!"), true);
keymap.bind_key("q", "Quit", || s::abort(), true);
keymap.bind_key("h", "Say Hello", || print("hello world!"));
keymap.bind_key("q", "Quit", || s::abort());
keymap.bind_key("o", "Open", || {
open_file_menu(s::current_dir());
}, true);
});

let file_selection_keymap = new_keymap();
file_selection_keymap.bind_key_for_regular_candidates("enter", "Open file", |path| s::open_doc(path), true);
file_selection_keymap.bind_key_for_regular_candidates("enter", "Open file", |path| s::open_doc(path));

let layer = new_layer("default");
layer.add_menu_keymap("file_selection", file_selection_keymap);
Expand Down
87 changes: 75 additions & 12 deletions src/keymap/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,25 @@ impl rhai::CustomType for Keymap {
fn build(mut builder: rhai::TypeBuilder<Self>) {
use std::str::FromStr;

fn parse_key(key_str: &str) -> Result<Key, Box<rhai::EvalAltResult>> {
Key::from_str(key_str).map_err(|err| error!(Keymap, "{err}: {key_str}").into())
}

// TODO add the other bind methods
builder
.with_name("Keymap")
.with_fn("new_keymap", Keymap::new)
.with_fn(
"bind_key",
|keymap: &mut Keymap,
key_str: &str,
hint: String,
prog: rhai::FnPtr|
-> Result<(), Box<rhai::EvalAltResult>> {
keymap.bind_key(parse_key(key_str)?, hint, prog, true);
Ok(())
},
)
.with_fn(
"bind_key",
|keymap: &mut Keymap,
Expand All @@ -470,9 +485,7 @@ impl rhai::CustomType for Keymap {
prog: rhai::FnPtr,
close_menu: bool|
-> Result<(), Box<rhai::EvalAltResult>> {
let key =
Key::from_str(key_str).map_err(|err| error!(Keymap, "{err}: {key_str}"))?;
keymap.bind_key(key, hint, prog, close_menu);
keymap.bind_key(parse_key(key_str)?, hint, prog, close_menu);
Ok(())
},
)
Expand All @@ -485,9 +498,31 @@ impl rhai::CustomType for Keymap {
prog: rhai::FnPtr,
close_menu: bool|
-> Result<(), Box<rhai::EvalAltResult>> {
let key =
Key::from_str(key_str).map_err(|err| error!(Keymap, "{err}: {key_str}"))?;
keymap.bind_key_for_special_candidate(key, candidate, hint, prog, close_menu);
keymap.bind_key_for_special_candidate(
parse_key(key_str)?,
candidate,
hint,
prog,
close_menu,
);
Ok(())
},
)
.with_fn(
"bind_key_for_special_candidate",
|keymap: &mut Keymap,
key_str: &str,
candidate: String,
hint: String,
prog: rhai::FnPtr|
-> Result<(), Box<rhai::EvalAltResult>> {
keymap.bind_key_for_special_candidate(
parse_key(key_str)?,
candidate,
hint,
prog,
true,
);
Ok(())
},
)
Expand All @@ -506,9 +541,23 @@ impl rhai::CustomType for Keymap {
prog: rhai::FnPtr,
close_menu: bool|
-> Result<(), Box<rhai::EvalAltResult>> {
let key =
Key::from_str(key_str).map_err(|err| error!(Keymap, "{err}: {key_str}"))?;
keymap.bind_key_for_regular_candidates(key, hint, prog, close_menu);
keymap.bind_key_for_regular_candidates(
parse_key(key_str)?,
hint,
prog,
close_menu,
);
Ok(())
},
)
.with_fn(
"bind_key_for_regular_candidates",
|keymap: &mut Keymap,
key_str: &str,
hint: String,
prog: rhai::FnPtr|
-> Result<(), Box<rhai::EvalAltResult>> {
keymap.bind_key_for_regular_candidates(parse_key(key_str)?, hint, prog, true);
Ok(())
},
)
Expand All @@ -520,9 +569,23 @@ impl rhai::CustomType for Keymap {
prog: rhai::FnPtr,
close_menu: bool|
-> Result<(), Box<rhai::EvalAltResult>> {
let key =
Key::from_str(key_str).map_err(|err| error!(Keymap, "{err}: {key_str}"))?;
keymap.bind_key_for_custom_candidate(key, hint, prog, close_menu);
keymap.bind_key_for_custom_candidate(
parse_key(key_str)?,
hint,
prog,
close_menu,
);
Ok(())
},
)
.with_fn(
"bind_key_for_custom_candidate",
|keymap: &mut Keymap,
key_str: &str,
hint: String,
prog: rhai::FnPtr|
-> Result<(), Box<rhai::EvalAltResult>> {
keymap.bind_key_for_custom_candidate(parse_key(key_str)?, hint, prog, true);
Ok(())
},
);
Expand Down

0 comments on commit 52a323f

Please sign in to comment.