Skip to content

Commit

Permalink
Merge keybind config into Config.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 5, 2024
1 parent feb499c commit 6260731
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use serde::Deserialize;

use crate::keybind::KeyBind;

const APP_DIR_NAME: &str = "serie";
const CONFIG_FILE_NAME: &str = "config.toml";

Expand All @@ -17,7 +17,7 @@ const DEFAULT_DETAIL_DATE_LOCAL: bool = true;
pub struct Config {
#[serde(default)]
pub ui: UiConfig,
pub custom_keybind_path: Option<PathBuf>,
pub custom_keybind_patch: Option<KeyBind>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
Expand Down Expand Up @@ -134,6 +134,7 @@ mod tests {
date_local: true,
},
},
custom_keybind_patch: None,
};
assert_eq!(actual, expected);
}
Expand Down Expand Up @@ -166,6 +167,7 @@ mod tests {
date_local: false,
},
},
custom_keybind_patch: None,
};
assert_eq!(actual, expected);
}
Expand All @@ -191,6 +193,7 @@ mod tests {
date_local: true,
},
},
custom_keybind_patch: None,
};
assert_eq!(actual, expected);
}
Expand Down
14 changes: 4 additions & 10 deletions src/keybind.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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};

const DEFAULT_KEY_BIND: &str = include_str!("../assets/default-keybind.toml");

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct KeyBind(pub HashMap<KeyEvent, UserEvent>);

impl Deref for KeyBind {
Expand All @@ -27,16 +25,12 @@ impl DerefMut for KeyBind {
}

impl KeyBind {
pub fn new(custom_keybind_path: Option<PathBuf>) -> Result<Self, ()> {
pub fn new(custom_keybind_patch: Option<KeyBind>) -> Result<Self, ()> {
let mut keybind: KeyBind =
toml::from_str(DEFAULT_KEY_BIND).expect("default key bind should be correct");

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(mut custom_keybind_patch) = custom_keybind_patch {
for (key_event, user_event) in custom_keybind_patch.drain() {
if let Some(_old_user_event) = keybind.insert(key_event, user_event) {
// log!("{key_event}: {_old_user_event} -> {user_event}")
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn run() -> std::io::Result<()> {
color_eyre::install().unwrap();
let args = Args::parse();
let mut config = config::Config::load();
let key_bind = keybind::KeyBind::new(config.custom_keybind_path.take())
let key_bind = keybind::KeyBind::new(config.custom_keybind_patch.take())
.expect("default key bind should work");

let color_set = color::ColorSet::default();
Expand Down

0 comments on commit 6260731

Please sign in to comment.