Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding format to convert custom keybinds to core-keybinds #29

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions lefthk-core/src/config/keybind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ pub struct Keybind {
pub modifier: Vec<String>,
pub key: String,
}

/// A trait which can convert self into the `Keybind` struct of lefthk_core
/// by simulating the `self`-keybinding by lefthk_core-keybinds.
pub trait CoreKeybind {
fn to_core_keybind(&self) -> Vec<Keybind>;
}
8 changes: 2 additions & 6 deletions lefthk-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ pub mod command;
mod keybind;

pub use command::Command;
pub use keybind::Keybind;
pub use keybind::{Keybind, CoreKeybind};

pub trait Config {
fn mapped_bindings(&self) -> Vec<Keybind>;
}

pub trait CommandAdapter {
fn convert(&self) -> Vec<Box<dyn Command>>;
fn mapped_keybinds(&self) -> Vec<Keybind>;
}
141 changes: 74 additions & 67 deletions lefthk/src/config/keybind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::errors::{LeftError, Result};
use lefthk_core::config::command as command_mod;
use lefthk_core::config::Command as core_command;
use lefthk_core::config::Keybind as core_keybind;
use lefthk_core::config::CoreKeybind;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -36,72 +37,78 @@ pub struct Keybind {
pub key: Key,
}

impl TryFrom<Keybind> for Vec<core_keybind> {
type Error = LeftError;

fn try_from(kb: Keybind) -> Result<Self> {
let command_key_pairs: Vec<(Box<dyn core_command>, String)> = match kb.command {
Command::Chord(children) if !children.is_empty() => {
let key = get_key!(kb.key);
let children = children
.iter()
.filter_map(|kb| match TryFrom::try_from(kb.clone()) {
Ok(keybinds) => Some::<Vec<lefthk_core::config::Keybind>>(keybinds),
Err(err) => {
tracing::error!("Invalid key binding: {}\n{:?}", err, kb);
None
}
})
.flatten()
.collect();

vec![(Box::new(command_mod::Chord::new(children)), key)]
}
Command::Chord(_) => return Err(LeftError::ChildrenNotFound),
Command::Execute(value) if !value.is_empty() => {
let keys = get_key!(kb.key);
vec![(Box::new(command_mod::Execute::new(value)), keys)]
}
Command::Execute(_) => return Err(LeftError::ValueNotFound),
Command::Executes(values) if !values.is_empty() => {
let keys = get_keys!(kb.key);
if keys.len() != values.len() {
return Err(LeftError::NumberOfKeysDiffersFromValues);
}
values
.iter()
.enumerate()
.map(|(i, v)| {
(
Box::new(command_mod::Execute::new(v.to_owned()))
as Box<dyn core_command>,
keys[i].clone(),
)
})
.collect()
}
Command::Executes(_) => return Err(LeftError::ValuesNotFound),
Command::ExitChord => {
let keys = get_key!(kb.key);
vec![(Box::new(command_mod::ExitChord::new()), keys)]
}
Command::Reload => {
let keys = get_key!(kb.key);
vec![(Box::new(command_mod::Reload::new()), keys)]
}
Command::Kill => {
let keys = get_key!(kb.key);
vec![(Box::new(command_mod::Kill::new()), keys)]
}
};
let keybinds = command_key_pairs
.iter()
.map(|(c, k)| core_keybind {
command: c.normalize(),
modifier: kb.modifier.clone(),
key: k.to_owned(),
})
.collect();
Ok(keybinds)
impl CoreKeybind for Keybind {
fn to_core_keybind(&self) -> Vec<core_keybind> {
todo!()
}
}

// impl TryFrom<Keybind> for Vec<core_keybind> {
// type Error = LeftError;
//
// fn try_from(kb: Keybind) -> Result<Self> {
// let command_key_pairs: Vec<(Box<dyn core_command>, String)> = match kb.command {
// Command::Chord(children) if !children.is_empty() => {
// let key = get_key!(kb.key);
// let children = children
// .iter()
// .filter_map(|kb| match TryFrom::try_from(kb.clone()) {
// Ok(keybinds) => Some::<Vec<lefthk_core::config::Keybind>>(keybinds),
// Err(err) => {
// tracing::error!("Invalid key binding: {}\n{:?}", err, kb);
// None
// }
// })
// .flatten()
// .collect();
//
// vec![(Box::new(command_mod::Chord::new(children)), key)]
// }
// Command::Chord(_) => return Err(LeftError::ChildrenNotFound),
// Command::Execute(value) if !value.is_empty() => {
// let keys = get_key!(kb.key);
// vec![(Box::new(command_mod::Execute::new(value)), keys)]
// }
// Command::Execute(_) => return Err(LeftError::ValueNotFound),
// Command::Executes(values) if !values.is_empty() => {
// let keys = get_keys!(kb.key);
// if keys.len() != values.len() {
// return Err(LeftError::NumberOfKeysDiffersFromValues);
// }
// values
// .iter()
// .enumerate()
// .map(|(i, v)| {
// (
// Box::new(command_mod::Execute::new(v.to_owned()))
// as Box<dyn core_command>,
// keys[i].clone(),
// )
// })
// .collect()
// }
// Command::Executes(_) => return Err(LeftError::ValuesNotFound),
// Command::ExitChord => {
// let keys = get_key!(kb.key);
// vec![(Box::new(command_mod::ExitChord::new()), keys)]
// }
// Command::Reload => {
// let keys = get_key!(kb.key);
// vec![(Box::new(command_mod::Reload::new()), keys)]
// }
// Command::Kill => {
// let keys = get_key!(kb.key);
// vec![(Box::new(command_mod::Kill::new()), keys)]
// }
// };
// let keybinds = command_key_pairs
// .iter()
// .map(|(c, k)| core_keybind {
// command: c.normalize(),
// modifier: kb.modifier.clone(),
// key: k.to_owned(),
// })
// .collect();
// Ok(keybinds)
// }
// }
2 changes: 1 addition & 1 deletion lefthk/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Config {
}

impl lefthk_core::config::Config for Config {
fn mapped_bindings(&self) -> Vec<lefthk_core::config::Keybind> {
fn convert_to_core_keybind(&self) -> Vec<lefthk_core::config::Keybind> {
self.keybinds
.iter()
.filter_map(|kb| match TryFrom::try_from(kb.clone()) {
Expand Down
2 changes: 1 addition & 1 deletion lefthk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() {
let _rt_guard = rt.enter();

let status =
rt.block_on(Worker::new(config.mapped_bindings(), path.clone()).event_loop());
rt.block_on(Worker::new(config.convert_to_core_keybind(), path.clone()).event_loop());
kill_requested.store(status == Status::Kill, Ordering::SeqCst);
});

Expand Down