Skip to content

Commit e1029b0

Browse files
authored
feat(cli): add the ability to specify a custom keybinding/symbols file via the cli (#2731)
1 parent 92e3b73 commit e1029b0

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* add `use_selection_fg` to theme file to allow customizing selection foreground color [[@Upsylonbare](https://github.com/Upsylonbare)] ([#2515](https://github.com/gitui-org/gitui/pull/2515))
2626
* add "go to line" command for the blame view [[@andrea-berling](https://github.com/andrea-berling)] ([#2262](https://github.com/extrawurst/gitui/pull/2262))
2727
* add `--file` cli flag to open the files tab with the given file already selected [[@laktak](https://github.com/laktak)] ([#2510](https://github.com/gitui-org/gitui/issues/2510))
28+
* add the ability to specify a custom keybinding/symbols file via the cli [[@0x61nas](https://github.com/0x61nas)] ([#2731](https://github.com/gitui-org/gitui/pull/2731))
2829

2930
### Changed
3031
* execute git-hooks directly if possible (on *nix) else use sh instead of bash (without reading SHELL variable) [[@Joshix](https://github.com/Joshix-1)] ([#2483](https://github.com/extrawurst/gitui/pull/2483))

src/args.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const WORKDIR_FLAG_ID: &str = "workdir";
2020
const FILE_FLAG_ID: &str = "file";
2121
const GIT_DIR_FLAG_ID: &str = "directory";
2222
const WATCHER_FLAG_ID: &str = "watcher";
23+
const KEY_BINDINGS_FLAG_ID: &str = "key_bindings";
24+
const KEY_SYMBOLS_FLAG_ID: &str = "key_symbols";
2325
const DEFAULT_THEME: &str = "theme.ron";
2426
const DEFAULT_GIT_DIR: &str = ".";
2527

@@ -29,6 +31,8 @@ pub struct CliArgs {
2931
pub select_file: Option<PathBuf>,
3032
pub repo_path: RepoPath,
3133
pub notify_watcher: bool,
34+
pub key_bindings_path: Option<PathBuf>,
35+
pub key_symbols_path: Option<PathBuf>,
3236
}
3337

3438
pub fn process_cmdline() -> Result<CliArgs> {
@@ -80,11 +84,21 @@ pub fn process_cmdline() -> Result<CliArgs> {
8084
let notify_watcher: bool =
8185
*arg_matches.get_one(WATCHER_FLAG_ID).unwrap_or(&false);
8286

87+
let key_bindings_path = arg_matches
88+
.get_one::<String>(KEY_BINDINGS_FLAG_ID)
89+
.map(PathBuf::from);
90+
91+
let key_symbols_path = arg_matches
92+
.get_one::<String>(KEY_SYMBOLS_FLAG_ID)
93+
.map(PathBuf::from);
94+
8395
Ok(CliArgs {
8496
theme,
8597
select_file,
8698
repo_path,
8799
notify_watcher,
100+
key_bindings_path,
101+
key_symbols_path,
88102
})
89103
}
90104

@@ -103,6 +117,22 @@ fn app() -> ClapApp {
103117
104118
{all-args}{after-help}
105119
",
120+
)
121+
.arg(
122+
Arg::new(KEY_BINDINGS_FLAG_ID)
123+
.help("Use a custom keybindings file")
124+
.short('k')
125+
.long("key-bindings")
126+
.value_name("KEY_LIST_FILENAME")
127+
.num_args(1),
128+
)
129+
.arg(
130+
Arg::new(KEY_SYMBOLS_FLAG_ID)
131+
.help("Use a custom symbols file")
132+
.short('s')
133+
.long("key-symbols")
134+
.value_name("KEY_SYMBOLS_FILENAME")
135+
.num_args(1),
106136
)
107137
.arg(
108138
Arg::new(THEME_FLAG_ID)

src/keys/key_config.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,21 @@ impl KeyConfig {
3434
.map_or_else(|_| Ok(symbols_file), Ok)
3535
}
3636

37-
pub fn init() -> Result<Self> {
38-
let keys = KeysList::init(Self::get_config_file()?);
39-
let symbols = KeySymbols::init(Self::get_symbols_file()?);
37+
pub fn init(
38+
key_bindings_path: Option<&PathBuf>,
39+
key_symbols_path: Option<&PathBuf>,
40+
) -> Result<Self> {
41+
let keys = KeysList::init(
42+
key_bindings_path
43+
.unwrap_or(&Self::get_config_file()?)
44+
.clone(),
45+
);
46+
let symbols = KeySymbols::init(
47+
key_symbols_path
48+
.unwrap_or(&Self::get_symbols_file()?)
49+
.clone(),
50+
);
51+
4052
Ok(Self { keys, symbols })
4153
}
4254

@@ -185,7 +197,7 @@ mod tests {
185197

186198
// testing
187199
let result = std::panic::catch_unwind(|| {
188-
let loaded_config = KeyConfig::init().unwrap();
200+
let loaded_config = KeyConfig::init(None, None).unwrap();
189201
assert_eq!(
190202
loaded_config.keys.move_down,
191203
KeysList::default().move_down
@@ -200,7 +212,7 @@ mod tests {
200212
&original_key_symbols_path,
201213
)
202214
.unwrap();
203-
let loaded_config = KeyConfig::init().unwrap();
215+
let loaded_config = KeyConfig::init(None, None).unwrap();
204216
assert_eq!(
205217
loaded_config.keys.move_down,
206218
KeysList::default().move_down
@@ -212,7 +224,7 @@ mod tests {
212224
&original_key_list_path,
213225
)
214226
.unwrap();
215-
let loaded_config = KeyConfig::init().unwrap();
227+
let loaded_config = KeyConfig::init(None, None).unwrap();
216228
assert_eq!(
217229
loaded_config.keys.move_down,
218230
GituiKeyEvent::new(
@@ -223,7 +235,7 @@ mod tests {
223235
assert_eq!(loaded_config.symbols.esc, "Esc");
224236

225237
fs::remove_file(&original_key_symbols_path).unwrap();
226-
let loaded_config = KeyConfig::init().unwrap();
238+
let loaded_config = KeyConfig::init(None, None).unwrap();
227239
assert_eq!(
228240
loaded_config.keys.move_down,
229241
GituiKeyEvent::new(

src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,12 @@ fn main() -> Result<()> {
170170
asyncgit::register_tracing_logging();
171171
ensure_valid_path(&cliargs.repo_path)?;
172172

173-
let key_config = KeyConfig::init()
174-
.map_err(|e| log_eprintln!("KeyConfig loading error: {e}"))
175-
.unwrap_or_default();
173+
let key_config = KeyConfig::init(
174+
cliargs.key_bindings_path.as_ref(),
175+
cliargs.key_symbols_path.as_ref(),
176+
)
177+
.map_err(|e| log_eprintln!("KeyConfig loading error: {e}"))
178+
.unwrap_or_default();
176179
let theme = Theme::init(&cliargs.theme);
177180

178181
setup_terminal()?;
@@ -212,6 +215,8 @@ fn main() -> Result<()> {
212215
select_file: None,
213216
theme: args.theme,
214217
notify_watcher: args.notify_watcher,
218+
key_bindings_path: args.key_bindings_path,
219+
key_symbols_path: args.key_symbols_path,
215220
}
216221
}
217222
_ => break,

0 commit comments

Comments
 (0)