diff --git a/src/config.rs b/src/config.rs index ffd28c3..c0ec1b1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use serde::Deserialize; const APP_DIR_NAME: &str = "serie"; @@ -15,6 +17,7 @@ const DEFAULT_DETAIL_DATE_LOCAL: bool = true; pub struct Config { #[serde(default)] pub ui: UiConfig, + pub custom_keybind_path: Option, } #[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)] diff --git a/src/keybind.rs b/src/keybind.rs index 1dc92aa..fdaf01f 100644 --- a/src/keybind.rs +++ b/src/keybind.rs @@ -1,7 +1,9 @@ use crate::event::UserEvent; use serde::{de::Deserializer, Deserialize}; use std::collections::HashMap; +use std::fs; use std::ops::{Deref, DerefMut}; +use std::path::PathBuf; use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; @@ -25,10 +27,22 @@ impl DerefMut for KeyBind { } impl KeyBind { - pub fn new() -> Result { - let keybind: KeyBind = + pub fn new(custom_keybind_path: Option) -> Result { + let mut keybind: KeyBind = toml::from_str(DEFAULT_KEY_BIND).expect("default key bind should be correct"); - // TODO: let user patch key bind here + + if let Some(custom_keybind_path) = custom_keybind_path { + let custom_keybind_content: String = + fs::read_to_string(custom_keybind_path).expect("custom keybind not found"); + let mut custom_keybind: KeyBind = + toml::from_str(&custom_keybind_content).expect("custom key bind should be correct"); + for (key_event, user_event) in custom_keybind.drain() { + if let Some(_old_user_event) = keybind.insert(key_event, user_event) { + // log!("{key_event}: {_old_user_event} -> {user_event}") + } + } + } + Ok(keybind) } diff --git a/src/lib.rs b/src/lib.rs index bb0e697..3d5c4bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,8 +113,9 @@ fn auto_detect_best_protocol() -> protocol::ImageProtocol { pub fn run() -> std::io::Result<()> { color_eyre::install().unwrap(); let args = Args::parse(); - let config = config::Config::load(); - let key_bind = keybind::KeyBind::new().expect("default key bind should work"); + let mut config = config::Config::load(); + let key_bind = keybind::KeyBind::new(config.custom_keybind_path.take()) + .expect("default key bind should work"); let color_set = color::ColorSet::default(); let image_protocol = args.protocol.into();