Skip to content

Commit

Permalink
Add custom keybind config
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 4, 2024
1 parent 53622e4 commit 87eee58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use serde::Deserialize;

const APP_DIR_NAME: &str = "serie";
Expand All @@ -13,6 +15,7 @@ const DEFAULT_DETAIL_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S %z";
pub struct Config {
#[serde(default)]
pub ui: UiConfig,
pub custom_keybind_path: Option<PathBuf>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
Expand Down
20 changes: 17 additions & 3 deletions src/keybind.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -25,10 +27,22 @@ impl DerefMut for KeyBind {
}

impl KeyBind {
pub fn new() -> Result<Self, ()> {
let keybind: KeyBind =
pub fn new(custom_keybind_path: Option<PathBuf>) -> Result<Self, ()> {
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)
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ fn initialize_panic_handler() {
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();
Expand Down

0 comments on commit 87eee58

Please sign in to comment.