77
88#include < TerminalCore/ControlKeyStates.hpp>
99#include < TerminalThemeHelpers.h>
10- #include < til/hash.h>
1110#include < til/io.h>
11+ #include < til/hash.h>
12+ #include < til/unicode.h>
1213#include < shlobj.h>
1314#include < Utils.h>
1415
@@ -3198,7 +3199,19 @@ namespace winrt::TerminalApp::implementation
31983199 {
31993200 // We have to initialize the dialog here to be able to change the text of the text block within it
32003201 std::ignore = FindName (L" MultiLinePasteDialog" );
3201- ClipboardText ().Text (text);
3202+
3203+ // WinUI absolutely cannot deal with large amounts of text (at least O(n), possibly O(n^2),
3204+ // so we limit the string length here and add an ellipsis if necessary.
3205+ auto clipboardText = text;
3206+ if (clipboardText.size () > 1024 )
3207+ {
3208+ const std::wstring_view view{ text };
3209+ // Make sure we don't cut in the middle of a surrogate pair
3210+ const auto len = til::utf16_iterate_prev (view, 512 );
3211+ clipboardText = til::hstring_format (FMT_COMPILE (L" {}\n …" ), view.substr (0 , len));
3212+ }
3213+
3214+ ClipboardText ().Text (std::move (clipboardText));
32023215
32033216 // The vertical offset on the scrollbar does not reset automatically, so reset it manually
32043217 ClipboardContentScrollViewer ().ScrollToVerticalOffset (0 );
@@ -3214,7 +3227,7 @@ namespace winrt::TerminalApp::implementation
32143227 }
32153228
32163229 // Clear the clipboard text so it doesn't lie around in memory
3217- ClipboardText ().Text (L" " );
3230+ ClipboardText ().Text ({} );
32183231
32193232 if (warningResult != ContentDialogResult::Primary)
32203233 {
0 commit comments