Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ pub(crate) enum Action {
Jump,

/// Climb or move up
#[key("up", symbol = "↑", help = "move up")]
#[key("up", symbol = "↑", help = "up")]
Up,

/// Drop or crouch down
#[key("down", symbol = "↓", help = "move down")]
#[key("down", symbol = "↓", help = "down")]
Down,

/// Move leftward
#[key("left", symbol = "←", help = "move left")]
#[key("left", symbol = "←", help = "left")]
Left,

/// Move rightward
#[key("right", symbol = "→", help = "move right")]
#[key("right", symbol = "→", help = "right")]
Right,

/// Exit or pause game
Expand Down
Binary file modified examples/reload.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 25 additions & 34 deletions keymap_derive/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ mod tests {

struct Wrapper(keymap_parser::Node);

fn wrap(s: &str) -> Vec<Wrapper> {
keymap_parser::parse_seq(s)
.unwrap()
.into_iter()
.map(Wrapper)
.collect()
}

impl ToKeyMap for Wrapper {
fn to_keymap(&self) -> Result<KeyMap, Error> {
Ok(self.0.clone())
Expand Down Expand Up @@ -156,45 +164,28 @@ mod tests {
fn test_bound_payload_extraction() {
let config = Action::keymap_config();

// When we press '1', it matches @digit, and we should extract '1'
let keys = keymap_parser::parse_seq("1")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound_action = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound_action, Action::Digit('1'));

// When we press 'A', it matches @any, and we should extract 'A'
let keys = keymap_parser::parse_seq("A")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound_action = config.get_bound_seq(&keys).unwrap();

assert_eq!(bound_action, Action::Jump('A'));
[
("1", Action::Digit('1')),
("A", Action::Jump('A')),
("enter", Action::Create),
]
.into_iter()
.for_each(|(input, expected)| {
assert_eq!(expected, config.get_bound_seq(&wrap(input)).unwrap());
});

// When we press 'Q', it matches @any, and we should extract 'Q'
let keys = keymap_parser::parse_seq("Q")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let nodes = keys.iter().map(|k| k.0.clone()).collect::<Vec<_>>();
// get_bound_item_by_keymaps also returns the item
let nodes = wrap("Q").into_iter().map(|w| w.0).collect::<Vec<_>>();
let (bound_action, item) = config.get_bound_item_by_keymaps(&nodes).unwrap();

assert_eq!(bound_action, Action::Jump('Q'));
assert_eq!(item.description, "Jump with char argument");

// Standard keys should extract as well using get_bound_seq
let keys = keymap_parser::parse_seq("enter")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound_action = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound_action, Action::Create);
// Key::Space extracted as ' ' via @any
let space = vec![Wrapper(keymap_parser::Node::new(
0,
keymap_parser::node::Key::Space,
))];
assert_eq!(Action::Jump(' '), config.get_bound_seq(&space).unwrap());
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl KeyGroupValue for char {
fn from_keymap_node(node: &KeyMap) -> Self {
match node.key {
Key::Char(c) => c,
Key::Space => ' ',
_ => '\0',
}
}
Expand Down
Loading