Skip to content
Open
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
28 changes: 28 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct CliArgs {
pub theme: PathBuf,
pub repo_path: RepoPath,
pub notify_watcher: bool,
pub key_bindings_path: Option<PathBuf>,
pub key_symbols_path: Option<PathBuf>,
}

pub fn process_cmdline() -> Result<CliArgs> {
Expand Down Expand Up @@ -60,10 +62,20 @@ pub fn process_cmdline() -> Result<CliArgs> {
let notify_watcher: bool =
*arg_matches.get_one("watcher").unwrap_or(&false);

let key_bindings_path = arg_matches
.get_one::<String>("key_bindings")
.map(PathBuf::from);

let key_symbols_path = arg_matches
.get_one::<String>("key_symbols")
.map(PathBuf::from);

Ok(CliArgs {
theme,
repo_path,
notify_watcher,
key_bindings_path,
key_symbols_path,
})
}

Expand All @@ -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")
Expand Down
39 changes: 31 additions & 8 deletions src/keys/key_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use crossterm::event::{KeyCode, KeyModifiers};
use std::{fs::canonicalize, path::PathBuf, rc::Rc};

Expand Down Expand Up @@ -34,9 +34,32 @@ impl KeyConfig {
.map_or_else(|_| Ok(symbols_file), Ok)
}

pub fn init() -> Result<Self> {
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<Self> {
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 })
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
Loading