Skip to content

Commit c678978

Browse files
committed
feat(cli): add the ability to specify a custom keybinding/symbols file via the cli
this change is meant to address github issue: #2714
1 parent 1d22485 commit c678978

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
@@ -16,6 +16,8 @@ pub struct CliArgs {
1616
pub theme: PathBuf,
1717
pub repo_path: RepoPath,
1818
pub notify_watcher: bool,
19+
pub key_bindings_path: Option<PathBuf>,
20+
pub key_symbols_path: Option<PathBuf>,
1921
}
2022

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

65+
let key_bindings_path = arg_matches
66+
.get_one::<String>("key_bindings")
67+
.map(PathBuf::from);
68+
69+
let key_symbols_path = arg_matches
70+
.get_one::<String>("key_symbols")
71+
.map(PathBuf::from);
72+
6373
Ok(CliArgs {
6474
theme,
6575
repo_path,
6676
notify_watcher,
77+
key_bindings_path,
78+
key_symbols_path,
6779
})
6880
}
6981

@@ -82,6 +94,22 @@ fn app() -> ClapApp {
8294
8395
{all-args}{after-help}
8496
",
97+
)
98+
.arg(
99+
Arg::new("key_bindings")
100+
.help("Use a custom keybindings file")
101+
.short('k')
102+
.long("keybindings")
103+
.value_name("KEY_LIST_FILENAME")
104+
.num_args(1),
105+
)
106+
.arg(
107+
Arg::new("key_symbols")
108+
.help("Use a custom symbols file")
109+
.short('s')
110+
.long("key-symblos")
111+
.value_name("KEY_SYMBOLS_FILENAME")
112+
.num_args(1),
85113
)
86114
.arg(
87115
Arg::new("theme")

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
@@ -166,7 +166,7 @@ fn main() -> Result<()> {
166166
asyncgit::register_tracing_logging();
167167
ensure_valid_path(&cliargs.repo_path)?;
168168

169-
let key_config = KeyConfig::init()
169+
let key_config = KeyConfig::init(&cliargs)
170170
.map_err(|e| log_eprintln!("KeyConfig loading error: {e}"))
171171
.unwrap_or_default();
172172
let theme = Theme::init(&cliargs.theme);

0 commit comments

Comments
 (0)