diff --git a/src/args.rs b/src/args.rs index b03d88a806..ffded183ab 100644 --- a/src/args.rs +++ b/src/args.rs @@ -16,6 +16,8 @@ pub struct CliArgs { pub theme: PathBuf, pub repo_path: RepoPath, pub notify_watcher: bool, + pub key_bindings_path: Option, + pub key_symbols_path: Option, } pub fn process_cmdline() -> Result { @@ -60,10 +62,20 @@ pub fn process_cmdline() -> Result { let notify_watcher: bool = *arg_matches.get_one("watcher").unwrap_or(&false); + let key_bindings_path = arg_matches + .get_one::("key_bindings") + .map(PathBuf::from); + + let key_symbols_path = arg_matches + .get_one::("key_symbols") + .map(PathBuf::from); + Ok(CliArgs { theme, repo_path, notify_watcher, + key_bindings_path, + key_symbols_path, }) } @@ -82,6 +94,22 @@ fn app() -> ClapApp { {all-args}{after-help} ", + ) + .arg( + Arg::new("key_bindings") + .help("Use a custom keybindings file") + .short('k') + .long("key-bindings") + .value_name("KEY_LIST_FILENAME") + .num_args(1), + ) + .arg( + Arg::new("key_symbols") + .help("Use a custom symbols file") + .short('s') + .long("key-symblos") + .value_name("KEY_SYMBOLS_FILENAME") + .num_args(1), ) .arg( Arg::new("theme") diff --git a/src/keys/key_config.rs b/src/keys/key_config.rs index fe6a987765..c63e23e54c 100644 --- a/src/keys/key_config.rs +++ b/src/keys/key_config.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; use crossterm::event::{KeyCode, KeyModifiers}; use std::{fs::canonicalize, path::PathBuf, rc::Rc}; @@ -34,9 +34,32 @@ impl KeyConfig { .map_or_else(|_| Ok(symbols_file), Ok) } - pub fn init() -> Result { - let keys = KeysList::init(Self::get_config_file()?); - let symbols = KeySymbols::init(Self::get_symbols_file()?); + pub fn init( + key_bindings_path: Option<&PathBuf>, + key_symbols_path: Option<&PathBuf>, + ) -> Result { + let keys = if let Some(path) = key_bindings_path { + if !path.exists() { + return Err(anyhow!( + "The custom key bindings file dosen't exists" + )); + } + KeysList::init(path.clone()) + } else { + KeysList::init(Self::get_config_file()?) + }; + + let symbols = if let Some(path) = key_symbols_path { + if !path.exists() { + return Err(anyhow!( + "The custom key symbols file dosen't exists" + )); + } + KeySymbols::init(path.clone()) + } else { + KeySymbols::init(Self::get_symbols_file()?) + }; + Ok(Self { keys, symbols }) } @@ -193,7 +216,7 @@ mod tests { // testing let result = std::panic::catch_unwind(|| { - let loaded_config = KeyConfig::init().unwrap(); + let loaded_config = KeyConfig::init(None, None).unwrap(); assert_eq!( loaded_config.keys.move_down, KeysList::default().move_down @@ -208,7 +231,7 @@ mod tests { &original_key_symbols_path, ) .unwrap(); - let loaded_config = KeyConfig::init().unwrap(); + let loaded_config = KeyConfig::init(None, None).unwrap(); assert_eq!( loaded_config.keys.move_down, KeysList::default().move_down @@ -220,7 +243,7 @@ mod tests { &original_key_list_path, ) .unwrap(); - let loaded_config = KeyConfig::init().unwrap(); + let loaded_config = KeyConfig::init(None, None).unwrap(); assert_eq!( loaded_config.keys.move_down, GituiKeyEvent::new( @@ -231,7 +254,7 @@ mod tests { assert_eq!(loaded_config.symbols.esc, "Esc"); fs::remove_file(&original_key_symbols_path).unwrap(); - let loaded_config = KeyConfig::init().unwrap(); + let loaded_config = KeyConfig::init(None, None).unwrap(); assert_eq!( loaded_config.keys.move_down, GituiKeyEvent::new( diff --git a/src/main.rs b/src/main.rs index de1e6afce1..e509c3f29f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,9 +166,12 @@ fn main() -> Result<()> { asyncgit::register_tracing_logging(); ensure_valid_path(&cliargs.repo_path)?; - let key_config = KeyConfig::init() - .map_err(|e| log_eprintln!("KeyConfig loading error: {e}")) - .unwrap_or_default(); + let key_config = KeyConfig::init( + cliargs.key_bindings_path.as_ref(), + cliargs.key_symbols_path.as_ref(), + ) + .map_err(|e| log_eprintln!("KeyConfig loading error: {e}")) + .unwrap_or_default(); let theme = Theme::init(&cliargs.theme); setup_terminal()?;