summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Knaack <davidkna@users.noreply.github.com>2021-03-05 08:47:07 +0100
committerGitHub <noreply@github.com>2021-03-05 01:47:07 -0600
commit7385dc27a98096ccc51fc19a7f8b4f88542a66ed (patch)
treebb45c1ae5da14e7a51511b93f68753145b54fc5e
parentdc8fe1bb6cf5bd866f0c7359ec4e6af5dfc4682b (diff)
fix(bash): escape interpretable characters (#2404)
* fix(bash): escape interpretable characters * also escape backticks
-rw-r--r--src/utils.rs43
1 files changed, 42 insertions, 1 deletions
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
+ );
+ }
}