Skip to content

Commit b9c0211

Browse files
committed
feat(cli): add the ability to specify a custom keybinding/symbols file via the cli
Introduce `-k`,`--key-bindings`, `-s`, and `--key-symbols` arguments to allow the user from specifying a custom config files without having to change the global ones this change is meant to address github issue: #2714
1 parent 92e3b73 commit b9c0211

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/args.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct CliArgs {
2929
pub select_file: Option<PathBuf>,
3030
pub repo_path: RepoPath,
3131
pub notify_watcher: bool,
32+
pub key_bindings_path: Option<PathBuf>,
33+
pub key_symbols_path: Option<PathBuf>,
3234
}
3335

3436
pub fn process_cmdline() -> Result<CliArgs> {
@@ -80,11 +82,21 @@ pub fn process_cmdline() -> Result<CliArgs> {
8082
let notify_watcher: bool =
8183
*arg_matches.get_one(WATCHER_FLAG_ID).unwrap_or(&false);
8284

85+
let key_bindings_path = arg_matches
86+
.get_one::<String>("key_bindings")
87+
.map(PathBuf::from);
88+
89+
let key_symbols_path = arg_matches
90+
.get_one::<String>("key_symbols")
91+
.map(PathBuf::from);
92+
8393
Ok(CliArgs {
8494
theme,
8595
select_file,
8696
repo_path,
8797
notify_watcher,
98+
key_bindings_path,
99+
key_symbols_path,
88100
})
89101
}
90102

@@ -103,6 +115,22 @@ fn app() -> ClapApp {
103115
104116
{all-args}{after-help}
105117
",
118+
)
119+
.arg(
120+
Arg::new("key_bindings")
121+
.help("Use a custom keybindings file")
122+
.short('k')
123+
.long("key-bindings")
124+
.value_name("KEY_LIST_FILENAME")
125+
.num_args(1),
126+
)
127+
.arg(
128+
Arg::new("key_symbols")
129+
.help("Use a custom symbols file")
130+
.short('s')
131+
.long("key-symblos")
132+
.value_name("KEY_SYMBOLS_FILENAME")
133+
.num_args(1),
106134
)
107135
.arg(
108136
Arg::new(THEME_FLAG_ID)

src/keys/key_config.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use anyhow::Result;
1+
use anyhow::{anyhow, Result};
22
use crossterm::event::{KeyCode, KeyModifiers};
33
use std::{fs::canonicalize, path::PathBuf, rc::Rc};
44

5-
use crate::{args::get_app_config_path, strings::symbol};
5+
use crate::{
6+
args::{get_app_config_path, CliArgs},
7+
strings::symbol,
8+
};
69

710
use super::{
811
key_list::{GituiKeyEvent, KeysList},
@@ -34,9 +37,29 @@ impl KeyConfig {
3437
.map_or_else(|_| Ok(symbols_file), Ok)
3538
}
3639

37-
pub fn init() -> Result<Self> {
38-
let keys = KeysList::init(Self::get_config_file()?);
39-
let symbols = KeySymbols::init(Self::get_symbols_file()?);
40+
pub fn init(cli_args: &CliArgs) -> Result<Self> {
41+
let keys = if let Some(path) = &cli_args.key_bindings_path {
42+
if !path.exists() {
43+
return Err(anyhow!(
44+
"The custom key bindings file dosen't exists"
45+
));
46+
}
47+
KeysList::init(path.to_path_buf())
48+
} else {
49+
KeysList::init(Self::get_config_file()?)
50+
};
51+
52+
let symbols = if let Some(path) = &cli_args.key_symbols_path {
53+
if !path.exists() {
54+
return Err(anyhow!(
55+
"The custom key symbols file dosen't exists"
56+
));
57+
}
58+
KeySymbols::init(path.to_path_buf())
59+
} else {
60+
KeySymbols::init(Self::get_symbols_file()?)
61+
};
62+
4063
Ok(Self { keys, symbols })
4164
}
4265

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn main() -> Result<()> {
170170
asyncgit::register_tracing_logging();
171171
ensure_valid_path(&cliargs.repo_path)?;
172172

173-
let key_config = KeyConfig::init()
173+
let key_config = KeyConfig::init(&cliargs)
174174
.map_err(|e| log_eprintln!("KeyConfig loading error: {e}"))
175175
.unwrap_or_default();
176176
let theme = Theme::init(&cliargs.theme);

0 commit comments

Comments
 (0)