From 7385dc27a98096ccc51fc19a7f8b4f88542a66ed Mon Sep 17 00:00:00 2001 From: David Knaack Date: Fri, 5 Mar 2021 08:47:07 +0100 Subject: fix(bash): escape interpretable characters (#2404) * fix(bash): escape interpretable characters * also escape backticks --- src/utils.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index d1a3025c5..352efb84f 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 + ); + } } -- cgit v1.2.3