From fee3fdf3221f512fea081a15c494e1a83ac9d4f3 Mon Sep 17 00:00:00 2001 From: d-bingham Date: Wed, 6 Nov 2019 08:02:55 -0600 Subject: [PATCH] Replacing \r\n line endings with \r line endings (#3449) ## Summary of the Pull Request ## References #1091 #1094 #2390 #3314 ## PR Checklist * [x] Closes #1091 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Requires documentation to be updated * [x] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx ## Detailed Description of the Pull Request / Additional comments Combination of the PRs #1094, #2390, and #3314, especially as discussed in #3314. In short, this changes line endings from Windows-space \r\n to the more universal \r. ## Validation Steps Performed Copied and pasted text into the terminal without the patch, line endings were doubled. With the patch, line endings weren't doubled. -------------------- * Replacing \r\n line endings with \r line endings * Fixing Formatting --- src/cascadia/TerminalControl/TermControl.cpp | 31 +++++++++++++++++++- src/cascadia/TerminalControl/TermControl.h | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index aa764e02ca..a75cbee7c0 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -1205,6 +1205,35 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation _connection.WriteInput(wstr); } + // Method Description: + // - Pre-process text pasted (presumably from the clipboard) + // before sending it over the terminal's connection, converting + // Windows-space \r\n line-endings to \r line-endings + void TermControl::_SendPastedTextToConnection(const std::wstring& wstr) + { + // Some notes on this implementation: + // + // - std::regex can do this in a single line, but is somewhat + // overkill for a simple search/replace operation (and its + // performance guarantees aren't exactly stellar) + // - The STL doesn't have a simple string search/replace method. + // This fact is lamentable. + // - This line-ending converstion is intentionally fairly + // conservative, to avoid stripping out lone \n characters + // where they could conceivably be intentional. + + std::wstring stripped{ wstr }; + + std::wstring::size_type pos = 0; + + while ((pos = stripped.find(L"\r\n", pos)) != std::wstring::npos) + { + stripped.replace(pos, 2, L"\r"); + } + + _connection.WriteInput(stripped); + } + // Method Description: // - Update the font with the renderer. This will be called either when the // font changes or the DPI changes, as DPI changes will necessitate a @@ -1456,7 +1485,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation { // attach TermControl::_SendInputToConnection() as the clipboardDataHandler. // This is called when the clipboard data is loaded. - auto clipboardDataHandler = std::bind(&TermControl::_SendInputToConnection, this, std::placeholders::_1); + auto clipboardDataHandler = std::bind(&TermControl::_SendPastedTextToConnection, this, std::placeholders::_1); auto pasteArgs = winrt::make_self(clipboardDataHandler); // send paste event up to TermApp diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index ee3597ba46..146ae5f222 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -165,6 +165,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation void _BlinkCursor(Windows::Foundation::IInspectable const& sender, Windows::Foundation::IInspectable const& e); void _SetEndSelectionPointAtCursor(Windows::Foundation::Point const& cursorPosition); void _SendInputToConnection(const std::wstring& wstr); + void _SendPastedTextToConnection(const std::wstring& wstr); void _SwapChainSizeChanged(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::SizeChangedEventArgs const& e); void _SwapChainScaleChanged(Windows::UI::Xaml::Controls::SwapChainPanel const& sender, Windows::Foundation::IInspectable const& args); void _DoResize(const double newWidth, const double newHeight);