From 3513aae3ed1c58670de110eaf71e6f21c330416c Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Tue, 23 Mar 2021 14:20:29 -0700 Subject: [PATCH] fix(shell): Support conditional format strings for `$indicator` (#2489) Previously attempting to use conditional format strings with `$indicator` would never display an indicator, e.g.: ```toml [shell] fish_indicator = "" bash_indicator = "B " format = "($indicator )" disabled = false ``` This would always display an empty string. Fixes #2474. --- src/modules/shell.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/modules/shell.rs b/src/modules/shell.rs index fc51b52f..c8b6a2c9 100644 --- a/src/modules/shell.rs +++ b/src/modules/shell.rs @@ -28,6 +28,16 @@ pub fn module<'a>(context: &'a Context) -> Option> { }, _ => None, }) + .map(|var| match var { + "bash_indicator" => Some(Ok(config.bash_indicator)), + "fish_indicator" => Some(Ok(config.fish_indicator)), + "zsh_indicator" => Some(Ok(config.zsh_indicator)), + "powershell_indicator" => Some(Ok(config.powershell_indicator)), + "ion_indicator" => Some(Ok(config.ion_indicator)), + "elvish_indicator" => Some(Ok(config.elvish_indicator)), + "tcsh_indicator" => Some(Ok(config.tcsh_indicator)), + _ => None, + }) .parse(None) }); @@ -237,4 +247,36 @@ mod tests { assert_eq!(expected, actual); } + + #[test] + fn test_custom_format_conditional_indicator_match() { + let expected = Some(format!("{} ", "B")); + let actual = ModuleRenderer::new("shell") + .shell(Shell::Bash) + .config(toml::toml! { + [shell] + bash_indicator = "B" + format = "($bash_indicator )" + disabled = false + }) + .collect(); + + assert_eq!(expected, actual); + } + + #[test] + fn test_custom_format_conditional_indicator_no_match() { + let expected = None; + let actual = ModuleRenderer::new("shell") + .shell(Shell::Fish) + .config(toml::toml! { + [shell] + bash_indicator = "B" + format = "($indicator )" + disabled = false + }) + .collect(); + + assert_eq!(expected, actual); + } }