diff --git a/src/utils.rs b/src/utils.rs index d1a3025c..352efb84 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -207,7 +207,15 @@ CMake suite maintained and supported by Kitware (kitware.com/cmake).\n", } /// Wraps ANSI color escape sequences in the shell-appropriate wrappers. -pub fn wrap_colorseq_for_shell(ansi: String, shell: Shell) -> String { +pub fn wrap_colorseq_for_shell(mut ansi: String, shell: Shell) -> String { + // Bash might interepret baskslashes, backticks and $ + // see #658 for more details + if shell == Shell::Bash { + ansi = ansi.replace('\\', r"\\"); + ansi = ansi.replace('$', r"\$"); + ansi = ansi.replace('`', r"\`"); + } + const ESCAPE_BEGIN: char = '\u{1b}'; const ESCAPE_END: char = 'm'; wrap_seq_for_shell(ansi, shell, ESCAPE_BEGIN, ESCAPE_END) @@ -465,4 +473,37 @@ mod tests { assert_eq!(&bresult4, "herpaderp"); assert_eq!(&bresult5, ""); } + + #[test] + fn test_bash_escape() { + let test = "$(echo a)"; + assert_eq!( + wrap_colorseq_for_shell(test.to_owned(), Shell::Bash), + r"\$(echo a)" + ); + assert_eq!( + wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell), + test + ); + + let test = r"\$(echo a)"; + assert_eq!( + wrap_colorseq_for_shell(test.to_owned(), Shell::Bash), + r"\\\$(echo a)" + ); + assert_eq!( + wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell), + test + ); + + let test = r"`echo a`"; + assert_eq!( + wrap_colorseq_for_shell(test.to_owned(), Shell::Bash), + r"\`echo a\`" + ); + assert_eq!( + wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell), + test + ); + } }