Skip to content

Commit b4fef38

Browse files
committed
add minimal example
1 parent 57b8544 commit b4fef38

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "keybinds"
3-
version = "0.0.3"
3+
version = "0.0.4"
44
edition = "2021"
55
authors = ["rhysd <[email protected]>"]
66
description = "Platform&Framework-agnostic key bindings parser and dispatcher"
@@ -17,6 +17,9 @@ include = [
1717
"/README.md",
1818
]
1919

20+
[[example]]
21+
name = "minimal"
22+
2023
[[example]]
2124
name = "serde"
2225
required-features = ["serde"]

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ library can be buggy and have arbitrary breaking changes.**
1010

1111
- Provide a syntax to easily define key bindings in a configuration file like `Ctrl+a`
1212
- Support key sequences like `Ctrl+x Ctrl+s`
13-
- Support to parse/generate the key bindings configuration using [serde][] optionally ([example](./examples/serde))
14-
- Core API independent from any platforms and frameworks with minimal dependencies
13+
- Support to parse/generate the key bindings configuration using [serde][] optionally ([example](./examples/serde.rs))
14+
- Core API independent from any platforms and frameworks with minimal dependencies ([example](./examples/minimal.rs))
1515
- Support several platforms and frameworks as optional features
1616
- [crossterm][] ([example](./examples/crossterm.rs))
1717
- [termwiz][] ([example](./examples/termwiz.rs))
@@ -109,11 +109,6 @@ Here are some examples of key sequences:
109109
- `h e l l o`
110110
- `Ctrl+x Ctrl+c`
111111

112-
## [serde][] support
113-
114-
See the document for `serde` module.
115-
The serde support requires the `serde` feature enabled.
116-
117112
## License
118113

119114
This crate is licensed under [the MIT license](./LICENSE.txt).

examples/minimal.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use keybinds::{Key, KeyInput, KeybindDispatcher};
2+
use std::io::{self, Read};
3+
4+
// Actions dispatched by key bindings
5+
#[derive(Debug)]
6+
enum Action {
7+
SayHello,
8+
ExitApp,
9+
}
10+
11+
fn main() -> io::Result<()> {
12+
// Create a dispatcher to dispatch actions for upcoming key inputs
13+
let mut dispatcher = KeybindDispatcher::default();
14+
15+
// Register key bindings to dispatch the actions
16+
dispatcher.bind("h e l l o", Action::SayHello).unwrap();
17+
dispatcher.bind("Esc", Action::ExitApp).unwrap();
18+
19+
println!("Type inputs and send it by hitting Enter key. Send Esc to exit");
20+
for b in io::stdin().bytes() {
21+
// Convert your key input into `KeyInput` struct
22+
let input = match b? {
23+
b'\x1b' => KeyInput::from(Key::Esc),
24+
b => KeyInput::from(b as char),
25+
};
26+
println!("Key input: {input:?}");
27+
28+
// Try to dispatch action by `dispatch` method
29+
if let Some(action) = dispatcher.dispatch(input) {
30+
println!("Dispatched action: {action:?}");
31+
match action {
32+
Action::SayHello => println!("Hello!"),
33+
Action::ExitApp => break,
34+
}
35+
}
36+
}
37+
38+
Ok(())
39+
}

0 commit comments

Comments
 (0)