From bcf2c745e01d0029f748e6ddb0eabd581ece3b27 Mon Sep 17 00:00:00 2001 From: Tsukasa OI Date: Tue, 8 Oct 2024 15:23:03 +0900 Subject: [PATCH] Escape single quotes for WSL This commit escapes single quotes (allowed in the Win32 subsystem) with `'"'"'` (finish quote, print a single quote then begin quote again), which is a valid escape in the context of the POSIX shell, when a file/ folder is dropped to a WSL tab. --- src/cascadia/TerminalControl/TermControl.cpp | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index f76c8c594035..66db053f92e5 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -3287,7 +3287,26 @@ namespace winrt::Microsoft::Terminal::Control::implementation { allPathsString.push_back(quotesChar); } - allPathsString.append(fullPath); + if (isWSL) + { + // Fix quoted path for WSL + // Single quote is allowed on the Win32 subsystem and must be processed + // when we are quoting the path with single quotes (on WSL). + // Note that we assume that all paths are quoted for WSL. + size_t pos; + auto fullPathView = std::wstring_view(fullPath); + while ((pos = fullPathView.find(L"'")) != std::wstring_view::npos) + { + allPathsString.append(fullPathView.begin(), fullPathView.begin() + pos); + allPathsString.append(L"'\"'\"'"); + fullPathView.remove_prefix(pos + 1); + } + allPathsString.append(fullPathView); + } + else + { + allPathsString.append(fullPath); + } if (quotesNeeded) { allPathsString.push_back(quotesChar);