From c573503cbfe434ad5927a40e4dcf9be6873bdc72 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Fri, 21 Mar 2025 21:33:53 +0100 Subject: [PATCH] fix(config): fix shell integration keybindings not overwriting defaults (#431) Fixes #430 --- television/config/mod.rs | 33 +++++++++++++++++++++++++++++++++ television/main.rs | 8 +++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/television/config/mod.rs b/television/config/mod.rs index 2007d67..a61e5de 100644 --- a/television/config/mod.rs +++ b/television/config/mod.rs @@ -440,4 +440,37 @@ mod tests { vec![(&String::from("some command"), &String::from("files"))] ); } + + #[test] + fn test_shell_integration_keybindings_are_overwritten_by_user() { + let user_config = r#" + [shell_integration.keybindings] + "smart_autocomplete" = "ctrl-t" + "command_history" = "ctrl-[" + "#; + + let dir = tempdir().unwrap(); + let config_dir = dir.path(); + let config_file = config_dir.join(CONFIG_FILE_NAME); + let mut file = File::create(&config_file).unwrap(); + file.write_all(user_config.as_bytes()).unwrap(); + + let config_env = ConfigEnv { + _data_dir: get_data_dir(), + config_dir: config_dir.to_path_buf(), + }; + + let config = Config::new(&config_env).unwrap(); + + assert_eq!( + config.shell_integration.keybindings, + [ + (&String::from("command_history"), &String::from("ctrl-[")), + (&String::from("smart_autocomplete"), &String::from("ctrl-t")) + ] + .iter() + .map(|(k, v)| ((*k).to_string(), (*v).to_string())) + .collect() + ); + } } diff --git a/television/main.rs b/television/main.rs index a0e11b1..94f9378 100644 --- a/television/main.rs +++ b/television/main.rs @@ -46,7 +46,9 @@ async fn main() -> Result<()> { // optionally handle subcommands debug!("Handling subcommands..."); - args.command.as_ref().map(handle_subcommands); + args.command + .as_ref() + .map(|x| handle_subcommands(x, &config)); // optionally change the working directory args.working_directory.as_ref().map(set_current_dir); @@ -112,7 +114,7 @@ pub fn set_current_dir(path: &String) -> Result<()> { Ok(()) } -pub fn handle_subcommands(command: &Command) -> Result<()> { +pub fn handle_subcommands(command: &Command, config: &Config) -> Result<()> { match command { Command::ListChannels => { list_channels(); @@ -126,7 +128,7 @@ pub fn handle_subcommands(command: &Command) -> Result<()> { let script = render_autocomplete_script_template( target_shell, completion_script(target_shell)?, - &Config::default().shell_integration, + &config.shell_integration, )?; println!("{script}"); exit(0);