Move key binding groups to config crate

This commit is contained in:
Joscha 2023-04-27 15:31:49 +02:00
parent 5a0efd69e4
commit e1c3a463b2
7 changed files with 33 additions and 7 deletions

View file

@ -1,95 +0,0 @@
use cove_macro::Group;
use crate::KeyBinding;
#[derive(Debug, Group)]
pub struct General {
/// Quit cove.
pub exit: KeyBinding,
/// Abort/close.
pub abort: KeyBinding,
/// Confirm.
pub confirm: KeyBinding,
/// Show this help.
pub help: KeyBinding,
/// Show log.
pub log: KeyBinding,
}
#[derive(Debug, Group)]
pub struct Scroll {
/// Scroll up one line.
pub up_line: KeyBinding,
/// Scroll down one line.
pub down_line: KeyBinding,
/// Scroll up half a screen.
pub up_half: KeyBinding,
/// Scroll down half a screen.
pub down_half: KeyBinding,
/// Scroll up a full screen.
pub up_full: KeyBinding,
/// Scroll down a full screen.
pub down_full: KeyBinding,
}
#[derive(Debug, Group)]
pub struct Cursor {
/// Move cursor up.
pub up: KeyBinding,
/// Move cursor down.
pub down: KeyBinding,
/// Move cursor to top.
pub to_top: KeyBinding,
/// Move cursor to bottom.
pub to_bottom: KeyBinding,
/// Center cursor.
pub center: KeyBinding,
}
#[derive(Debug, Group)]
pub struct TreeCursor {
/// Move cursor to above sibling.
pub to_above_sibling: KeyBinding,
/// Move cursor to below sibling.
pub to_below_sibling: KeyBinding,
/// Move cursor to parent.
pub to_parent: KeyBinding,
/// Move cursor to root.
pub to_root: KeyBinding,
/// Move cursor to previous message.
pub to_prev_message: KeyBinding,
/// Move cursor to next message.
pub to_next_message: KeyBinding,
}
#[derive(Debug, Group)]
pub struct EditorCursor {
/// Move cursor left.
pub left: KeyBinding,
/// Move cursor right.
pub right: KeyBinding,
/// Move cursor left a word.
pub left_word: KeyBinding,
/// Move cursor right a word.
pub right_word: KeyBinding,
/// Move cursor to start of line.
pub start: KeyBinding,
/// Move cursor to end of line.
pub end: KeyBinding,
/// Move cursor up.
pub up: KeyBinding,
/// Move cursor down.
pub down: KeyBinding,
}
#[derive(Debug, Group)]
pub struct EditorOp {
/// Insert newline.
pub newline: KeyBinding,
/// Delete before cursor.
pub backspace: KeyBinding,
/// Delete after cursor.
pub delete: KeyBinding,
/// Clear editor contents.
pub clear: KeyBinding,
}

View file

@ -173,11 +173,35 @@ impl<'de> Deserialize<'de> for KeyPress {
pub struct KeyBinding(Vec<KeyPress>);
impl KeyBinding {
pub fn new() -> Self {
Self(vec![])
}
pub fn with_key(self, key: &str) -> Result<Self, ParseKeysError> {
self.with_keys([key])
}
pub fn with_keys<'a, I>(mut self, keys: I) -> Result<Self, ParseKeysError>
where
I: IntoIterator<Item = &'a str>,
{
for key in keys {
self.0.push(key.parse()?);
}
Ok(self)
}
pub fn matches(&self, event: KeyEvent) -> bool {
self.0.iter().any(|kp| kp.matches(event))
}
}
impl Default for KeyBinding {
fn default() -> Self {
Self::new()
}
}
impl Serialize for KeyBinding {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.0.len() == 1 {

View file

@ -1,8 +1,8 @@
mod groups;
mod input;
mod keys;
pub use groups::*;
pub use cove_macro::Group;
pub use input::*;
pub use keys::*;