Crokey
Crokey helps incorporate configurable keybindings in crossterm based terminal applications by providing functions
- parsing key combinations from strings
- describing key combinations in strings
- parsing key combinations at compile time
Parse a string
Those strings are usually provided by a configuration file.
use ;
assert_eq!;
assert_eq!;
Use key event "literals" thanks to procedural macros
Those key events are parsed at compile time and have zero runtime cost.
They're efficient and convenient for matching events or defining hardcoded keybindings.
match key_event
Complete example in /examples/print_key
:
Display a string with a configurable format
use *;
use ;
// The default format
let format = default;
assert_eq!;
assert_eq!;
// A more compact format
let format = default
.with_implicit_shift
.with_control;
assert_eq!;
assert_eq!;
Deserialize keybindings using Serde
With the "serde" feature enabled, you can read configuration files in a direct way:
use {
crokey::*,
crossterm::event::KeyEvent,
serde::Deserialize,
std::collections::HashMap,
};
#[derive(Deserialize)]
struct Config {
keybindings: HashMap<CroKey, String>,
}
static CONFIG_HJSON: &str = r#"
{
keybindings: {
a: aardvark
shift-b: babirussa
ctrl-k: koala
alt-j: jaguar
}
}
"#;
let config: Config = deser_hjson::from_str(CONFIG_HJSON).unwrap();
let key_event: KeyEvent = key!(shift-b);
assert_eq!(
config.keybindings.get(&key_event.into()).unwrap(),
"babirussa",
);
You can use any Serde compatible format such as JSON or TOML.
The CroKey
wrapper type may be convenient as it implements FromStr
,
Deserialize
, and Display
, but its use is optional. The "deser_keybindings" example
uses TOML and demonstrates how to have KeyEvent
keys in the map instead of Crokey
.