Skip to content

Commit 8318916

Browse files
committed
rename key bindings API
- `KeyBindMatcher` -> `KeybindDispatcher` - `KeyBinds` -> `Keybinds` - `Keybind` -> `Keybind`
1 parent 0985b88 commit 8318916

File tree

8 files changed

+85
-85
lines changed

8 files changed

+85
-85
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "keybinds"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
edition = "2021"
55
authors = ["rhysd <[email protected]>"]
6-
description = "Platform&Framework-agnostic key bindings parser and matcher"
6+
description = "Platform&Framework-agnostic key bindings parser and dispatcher"
77
license = "MIT"
88
homepage = "https://github.com/rhysd/keybinds-rs#readme"
99
repository = "https://github.com/rhysd/keybinds-rs"

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ keybinds-rs
55
**THIS CRATE IS WORK IN PROGRESS YET. The first beta release is planned as 0.1.0. Until then, this
66
library can be buggy and have arbitrary breaking changes.**
77

8-
[keybinds-rs][crates-io] is a small Rust crate to define/parse/match key bindings.
8+
[keybinds-rs][crates-io] is a small Rust crate to define/parse/dispatch key bindings.
99

1010
- Provide a syntax to easily define key bindings in a configuration file like `Ctrl+A`
1111
- Support key sequences like `Ctrl+X Ctrl+S`
@@ -34,12 +34,12 @@ cargo add keybinds --features=serde
3434

3535
## Basic usage
3636

37-
This crate is platform-agnostic. Define key bindings by `KeyBinds` and build `KeyBindMatcher` instance with it.
37+
This crate is platform-agnostic. Define key bindings by `Keybinds` and build `KeybindDispatcher` instance with it.
3838
Pass each key input to the `trigger` method and it returns a triggered action. Key sequence and key combination
3939
can be parsed using `FromStr` trait. See the [API documentation][api-doc] for more details.
4040

4141
```rust
42-
use keybinds::{KeyBind, KeyBinds, KeyBindMatcher, KeyInput, Key, Mods};
42+
use keybinds::{KeybindDispatcher, KeyInput, Key, Mods};
4343

4444
// Actions triggered by key bindings
4545
#[derive(PartialEq, Eq, Debug)]
@@ -49,32 +49,32 @@ enum Action {
4949
ExitApp,
5050
}
5151

52-
// Create a matcher to trigger actions for upcoming key inputs
53-
let mut matcher = KeyBindMatcher::default();
52+
// Create a dispatcher to trigger actions for upcoming key inputs
53+
let mut dispatcher = KeybindDispatcher::default();
5454

5555
// Register key bindings to trigger the actions
5656

5757
// Key sequence "hello"
58-
matcher.bind("h e l l o", Action::SayHello).unwrap();
58+
dispatcher.bind("h e l l o", Action::SayHello).unwrap();
5959
// Key combination "Ctrl + Shift + Enter"
60-
matcher.bind("Ctrl+Shift+Enter", Action::OpenFile).unwrap();
60+
dispatcher.bind("Ctrl+Shift+Enter", Action::OpenFile).unwrap();
6161
// Sequence of key combinations
62-
matcher.bind("Ctrl+x Ctrl+c", Action::ExitApp).unwrap();
62+
dispatcher.bind("Ctrl+x Ctrl+c", Action::ExitApp).unwrap();
6363

6464
// Trigger `SayHello` action
65-
assert_eq!(matcher.trigger(KeyInput::from('h')), None);
66-
assert_eq!(matcher.trigger(KeyInput::from('e')), None);
67-
assert_eq!(matcher.trigger(KeyInput::from('l')), None);
68-
assert_eq!(matcher.trigger(KeyInput::from('l')), None);
69-
assert_eq!(matcher.trigger(KeyInput::from('o')), Some(&Action::SayHello));
65+
assert_eq!(dispatcher.trigger(KeyInput::from('h')), None);
66+
assert_eq!(dispatcher.trigger(KeyInput::from('e')), None);
67+
assert_eq!(dispatcher.trigger(KeyInput::from('l')), None);
68+
assert_eq!(dispatcher.trigger(KeyInput::from('l')), None);
69+
assert_eq!(dispatcher.trigger(KeyInput::from('o')), Some(&Action::SayHello));
7070

7171
// Trigger `OpenFile` action
72-
let action = matcher.trigger(KeyInput::new(Key::Enter, Mods::CTRL | Mods::SHIFT));
72+
let action = dispatcher.trigger(KeyInput::new(Key::Enter, Mods::CTRL | Mods::SHIFT));
7373
assert_eq!(action, Some(&Action::OpenFile));
7474

7575
// Trigger `ExitApp` action
76-
assert_eq!(matcher.trigger(KeyInput::new('x', Mods::CTRL)), None);
77-
assert_eq!(matcher.trigger(KeyInput::new('c', Mods::CTRL)), Some(&Action::ExitApp));
76+
assert_eq!(dispatcher.trigger(KeyInput::new('x', Mods::CTRL)), None);
77+
assert_eq!(dispatcher.trigger(KeyInput::new('c', Mods::CTRL)), Some(&Action::ExitApp));
7878
```
7979

8080
## Syntax for key sequence and combination
@@ -118,11 +118,11 @@ The serde support requires the `serde` feature enabled.
118118

119119
### Parsing key bindings configurations
120120

121-
`KeyBinds` implements serde's `Deserialize` trait. This is an example to parse key bindings with [toml][] crate.
121+
`Keybinds` implements serde's `Deserialize` trait. This is an example to parse key bindings with [toml][] crate.
122122

123123
```rust,ignore
124124
use serde::Deserialize;
125-
use keybinds::{KeyBinds, KeyBindMatcher, Key, Mods, KeyInput};
125+
use keybinds::{Keybinds, KeybindDispatcher, Key, Mods, KeyInput};
126126
127127
// Actions triggered by key bindings
128128
#[derive(Deserialize, PartialEq, Eq, Debug)]
@@ -134,8 +134,8 @@ enum Action {
134134
// Configuration file format of your application
135135
#[derive(Deserialize)]
136136
struct Config {
137-
// `KeyBinds` implements serde's `Deserialize`
138-
bindings: KeyBinds<Action>,
137+
// `Keybinds` implements serde's `Deserialize`
138+
bindings: Keybinds<Action>,
139139
}
140140
141141
let configuration = r#"
@@ -148,8 +148,8 @@ let configuration = r#"
148148
let config: Config = toml::from_str(configuration).unwrap();
149149
150150
// Use the key bindings parsed from the TOML input
151-
let mut matcher = KeyBindMatcher::new(config.bindings);
152-
let action = matcher.trigger(KeyInput::new(Key::Enter, Mods::CTRL | Mods::SHIFT));
151+
let mut dispatcher = KeybindDispatcher::new(config.bindings);
152+
let action = dispatcher.trigger(KeyInput::new(Key::Enter, Mods::CTRL | Mods::SHIFT));
153153
assert_eq!(action, Some(&Action::OpenFile));
154154
```
155155

examples/crossterm.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crossterm::event::{read, Event};
22
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
3-
use keybinds::{KeyBindMatcher, KeyInput};
3+
use keybinds::{KeyInput, KeybindDispatcher};
44
use std::io;
55

66
// Actions triggered by key bindings
@@ -13,14 +13,14 @@ enum Action {
1313
}
1414

1515
fn main() -> io::Result<()> {
16-
// Create a matcher to trigger actions for upcoming key inputs
17-
let mut matcher = KeyBindMatcher::default();
16+
// Create a dispatcher to trigger actions for upcoming key inputs
17+
let mut dispatcher = KeybindDispatcher::default();
1818

1919
// Key bindings to trigger the actions
20-
matcher.bind("h i", Action::SayHi).unwrap();
21-
matcher.bind("Left", Action::MoveLeft).unwrap();
22-
matcher.bind("Ctrl+p", Action::Paste).unwrap();
23-
matcher.bind("Ctrl+x Ctrl+c", Action::ExitApp).unwrap();
20+
dispatcher.bind("h i", Action::SayHi).unwrap();
21+
dispatcher.bind("Left", Action::MoveLeft).unwrap();
22+
dispatcher.bind("Ctrl+p", Action::Paste).unwrap();
23+
dispatcher.bind("Ctrl+x Ctrl+c", Action::ExitApp).unwrap();
2424

2525
println!("Type Ctrl+X → Ctrl+C to exit");
2626
enable_raw_mode()?;
@@ -30,8 +30,8 @@ fn main() -> io::Result<()> {
3030
// Can convert crossterm's `KeyEvent` into `KeyInput`
3131
println!("Key input `{:?}`\r", KeyInput::from(event));
3232

33-
// `KeyBindMatcher::trigger` accepts crossterm's `KeyEvent`
34-
if let Some(action) = matcher.trigger(event) {
33+
// `KeybindDispatcher::trigger` accepts crossterm's `KeyEvent`
34+
if let Some(action) = dispatcher.trigger(event) {
3535
println!("Triggered action `{action:?}`\r");
3636
if action == &Action::ExitApp {
3737
break;

examples/termwiz.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use keybinds::{KeyBindMatcher, KeyInput};
1+
use keybinds::{KeyInput, KeybindDispatcher};
22
use termwiz::caps::Capabilities;
33
use termwiz::cell::AttributeChange;
44
use termwiz::color::{AnsiColor, ColorAttribute};
@@ -17,14 +17,14 @@ enum Action {
1717
}
1818

1919
fn main() -> Result<(), Error> {
20-
// Create a matcher to trigger actions for upcoming key inputs
21-
let mut matcher = KeyBindMatcher::default();
20+
// Create an action dispatcher to trigger actions for upcoming key inputs
21+
let mut dispatcher = KeybindDispatcher::default();
2222

2323
// Key bindings to trigger the actions
24-
matcher.bind("h i", Action::SayHi).unwrap();
25-
matcher.bind("Left", Action::MoveLeft).unwrap();
26-
matcher.bind("Ctrl+p", Action::Paste).unwrap();
27-
matcher.bind("Ctrl+x Ctrl+c", Action::ExitApp).unwrap();
24+
dispatcher.bind("h i", Action::SayHi).unwrap();
25+
dispatcher.bind("Left", Action::MoveLeft).unwrap();
26+
dispatcher.bind("Ctrl+p", Action::Paste).unwrap();
27+
dispatcher.bind("Ctrl+x Ctrl+c", Action::ExitApp).unwrap();
2828

2929
let caps = Capabilities::new_from_env()?;
3030
let terminal = new_terminal(caps)?;
@@ -43,7 +43,7 @@ fn main() -> Result<(), Error> {
4343
};
4444

4545
// Trigger action by matching the key input
46-
let action = matcher.trigger(&input);
46+
let action = dispatcher.trigger(&input);
4747

4848
buf.add_change(Change::CursorPosition {
4949
x: Position::Absolute(0),

examples/winit.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use keybinds::winit::WinitEventConverter;
2-
use keybinds::KeyBindMatcher;
2+
use keybinds::KeybindDispatcher;
33
use winit::application::ApplicationHandler;
44
use winit::event::WindowEvent;
55
use winit::event_loop::{ActiveEventLoop, EventLoop};
@@ -16,23 +16,23 @@ enum Action {
1616

1717
struct App {
1818
window: Option<Window>,
19-
matcher: KeyBindMatcher<Action>,
19+
dispatcher: KeybindDispatcher<Action>,
2020
converter: WinitEventConverter,
2121
}
2222

2323
impl Default for App {
2424
fn default() -> Self {
25-
let mut matcher = KeyBindMatcher::default();
25+
let mut dispatcher = KeybindDispatcher::default();
2626

2727
// Key bindings to trigger the actions
28-
matcher.bind("h i", Action::SayHi).unwrap();
29-
matcher.bind("Mod+m", Action::ToggleMaximized).unwrap();
30-
matcher.bind("Mod+Shift+t", Action::ToggleTheme).unwrap();
31-
matcher.bind("Mod+x Mod+c", Action::Exit).unwrap();
28+
dispatcher.bind("h i", Action::SayHi).unwrap();
29+
dispatcher.bind("Mod+m", Action::ToggleMaximized).unwrap();
30+
dispatcher.bind("Mod+Shift+t", Action::ToggleTheme).unwrap();
31+
dispatcher.bind("Mod+x Mod+c", Action::Exit).unwrap();
3232

3333
Self {
3434
window: None,
35-
matcher,
35+
dispatcher,
3636
converter: WinitEventConverter::default(),
3737
}
3838
}
@@ -50,7 +50,7 @@ impl ApplicationHandler for App {
5050
let input = self.converter.convert(&event);
5151

5252
// Check if the converted key input triggers some action
53-
if let Some(action) = self.matcher.trigger(input) {
53+
if let Some(action) = self.dispatcher.trigger(input) {
5454
println!("Action: {action:?}");
5555

5656
match action {

src/lib.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,12 @@ impl From<char> for KeySeq {
274274
}
275275

276276
#[derive(Clone, PartialEq, Eq, Debug)]
277-
pub struct KeyBind<A> {
277+
pub struct Keybind<A> {
278278
seq: KeySeq,
279279
action: A,
280280
}
281281

282-
impl<A> KeyBind<A> {
282+
impl<A> Keybind<A> {
283283
pub fn multiple(seq: KeySeq, action: A) -> Self {
284284
Self { seq, action }
285285
}
@@ -290,20 +290,20 @@ impl<A> KeyBind<A> {
290290
}
291291

292292
#[derive(Clone, PartialEq, Eq, Debug)]
293-
pub struct KeyBinds<A>(Vec<KeyBind<A>>);
293+
pub struct Keybinds<A>(Vec<Keybind<A>>);
294294

295-
impl<A> Default for KeyBinds<A> {
295+
impl<A> Default for Keybinds<A> {
296296
fn default() -> Self {
297297
Self(vec![])
298298
}
299299
}
300300

301-
impl<A> KeyBinds<A> {
302-
pub fn new(v: Vec<KeyBind<A>>) -> Self {
301+
impl<A> Keybinds<A> {
302+
pub fn new(v: Vec<Keybind<A>>) -> Self {
303303
Self(v)
304304
}
305305

306-
pub fn find(&self, seq: &[KeyInput]) -> Match<&KeyBind<A>> {
306+
pub fn find(&self, seq: &[KeyInput]) -> Match<&Keybind<A>> {
307307
let mut saw_prefix = false;
308308
for bind in self.0.iter() {
309309
match bind.seq.matches(seq) {
@@ -320,21 +320,21 @@ impl<A> KeyBinds<A> {
320320
}
321321
}
322322

323-
pub struct KeyBindMatcher<A> {
324-
binds: KeyBinds<A>,
323+
pub struct KeybindDispatcher<A> {
324+
binds: Keybinds<A>,
325325
ongoing: Vec<KeyInput>,
326326
last_input: Option<Instant>,
327327
timeout: Duration,
328328
}
329329

330-
impl<A> Default for KeyBindMatcher<A> {
330+
impl<A> Default for KeybindDispatcher<A> {
331331
fn default() -> Self {
332-
Self::new(KeyBinds::default())
332+
Self::new(Keybinds::default())
333333
}
334334
}
335335

336-
impl<A> KeyBindMatcher<A> {
337-
pub fn new(binds: KeyBinds<A>) -> Self {
336+
impl<A> KeybindDispatcher<A> {
337+
pub fn new(binds: Keybinds<A>) -> Self {
338338
Self {
339339
binds,
340340
ongoing: vec![],
@@ -345,7 +345,7 @@ impl<A> KeyBindMatcher<A> {
345345

346346
pub fn add<K: Into<KeySeq>>(&mut self, key: K, action: A) {
347347
let seq = key.into();
348-
self.binds.0.push(KeyBind::multiple(seq, action));
348+
self.binds.0.push(Keybind::multiple(seq, action));
349349
}
350350

351351
pub fn bind(&mut self, key: &str, action: A) -> Result<()> {
@@ -453,19 +453,19 @@ mod tests {
453453
#[test]
454454
fn handle_input() {
455455
let binds = vec![
456-
KeyBind::single(KeyInput::new('a', Mods::NONE), A::Action1),
457-
KeyBind::single(KeyInput::new('a', Mods::CTRL | Mods::SHIFT), A::Action2),
458-
KeyBind::multiple(
456+
Keybind::single(KeyInput::new('a', Mods::NONE), A::Action1),
457+
Keybind::single(KeyInput::new('a', Mods::CTRL | Mods::SHIFT), A::Action2),
458+
Keybind::multiple(
459459
KeySeq::new(vec![
460460
KeyInput::new('b', Mods::NONE),
461461
KeyInput::new('c', Mods::NONE),
462462
]),
463463
A::Action3,
464464
),
465-
KeyBind::single(KeyInput::new(Key::Up, Mods::NONE), A::Action4),
465+
Keybind::single(KeyInput::new(Key::Up, Mods::NONE), A::Action4),
466466
];
467467

468-
let mut keybinds = KeyBindMatcher::new(KeyBinds(binds.clone()));
468+
let mut keybinds = KeybindDispatcher::new(Keybinds(binds.clone()));
469469

470470
for bind in binds {
471471
keybinds.reset();
@@ -481,8 +481,8 @@ mod tests {
481481

482482
#[test]
483483
fn discard_ongoing_nothing_matched() {
484-
let binds = vec![KeyBind::single(KeyInput::new('a', Mods::NONE), A::Action1)];
485-
let mut keybinds = KeyBindMatcher::new(KeyBinds(binds.clone()));
484+
let binds = vec![Keybind::single(KeyInput::new('a', Mods::NONE), A::Action1)];
485+
let mut keybinds = KeybindDispatcher::new(Keybinds(binds.clone()));
486486

487487
assert_eq!(keybinds.trigger(KeyInput::from('x')), None);
488488
assert_eq!(keybinds.trigger(KeyInput::from('y')), None);

0 commit comments

Comments
 (0)