Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Pasted Line Endings and then send to compty #2390

Closed
wants to merge 15 commits into from
2 changes: 1 addition & 1 deletion dep/gsl
Submodule gsl updated 2 files
+7 −13 appveyor.yml
+5 −5 include/gsl/multi_span
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static constexpr std::string_view UseAcrylicKey{ "useAcrylic" };
static constexpr std::string_view ScrollbarStateKey{ "scrollbarState" };
static constexpr std::string_view CloseOnExitKey{ "closeOnExit" };
static constexpr std::string_view PaddingKey{ "padding" };
static constexpr std::string_view ConvertPasteLineEndingsKey{ "convertPasteLineEndings" };
static constexpr std::string_view StartingDirectoryKey{ "startingDirectory" };
static constexpr std::string_view IconKey{ "icon" };
static constexpr std::string_view BackgroundImageKey{ "backgroundImage" };
Expand Down Expand Up @@ -99,6 +100,7 @@ Profile::Profile(const winrt::guid& guid) :
_useAcrylic{ false },
_scrollbarState{},
_closeOnExit{ true },
_convertPasteLineEndings{},
_padding{ DEFAULT_PADDING },
_icon{},
_backgroundImage{},
Expand Down Expand Up @@ -164,6 +166,10 @@ TerminalSettings Profile::CreateTerminalSettings(const std::vector<ColorScheme>&
// Fill in the remaining properties from the profile
terminalSettings.UseAcrylic(_useAcrylic);
terminalSettings.CloseOnExit(_closeOnExit);
if (_convertPasteLineEndings)
{
terminalSettings.ConvertPasteLineEndings(_convertPasteLineEndings.value());
}
terminalSettings.TintOpacity(_acrylicTransparency);

terminalSettings.FontFace(_fontFace);
Expand Down Expand Up @@ -287,10 +293,16 @@ Json::Value Profile::ToJson() const
root[JsonKey(CloseOnExitKey)] = _closeOnExit;
root[JsonKey(PaddingKey)] = winrt::to_string(_padding);

if (_convertPasteLineEndings)
{
root[JsonKey(ConvertPasteLineEndingsKey)] = _convertPasteLineEndings.value();
}

if (_connectionType)
{
root[JsonKey(ConnectionTypeKey)] = winrt::to_string(Utils::GuidToString(_connectionType.value()));
}

if (_scrollbarState)
{
const auto scrollbarState = winrt::to_string(_scrollbarState.value());
Expand Down Expand Up @@ -456,6 +468,10 @@ Profile Profile::FromJson(const Json::Value& json)
{
result._closeOnExit = closeOnExit.asBool();
}
if (auto convertPasteLineEndings{ json[JsonKey(ConvertPasteLineEndingsKey)] })
{
result._convertPasteLineEndings = convertPasteLineEndings.asBool();
}
if (auto padding{ json[JsonKey(PaddingKey)] })
{
result._padding = GetWstringFromJson(padding);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class TerminalApp::Profile final

std::optional<std::wstring> _scrollbarState;
bool _closeOnExit;
std::optional<bool> _convertPasteLineEndings;
std::wstring _padding;

std::optional<std::wstring> _icon;
Expand Down
45 changes: 33 additions & 12 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include "TermControl.h"
#include <regex>
#include <argb.h>
#include <DefaultSettings.h>
#include <unicode.hpp>
Expand Down Expand Up @@ -956,11 +957,10 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - mouseDelta: the mouse wheel delta that triggered this event.
void TermControl::_MouseZoomHandler(const double mouseDelta)
{
const auto fontDelta = mouseDelta < 0 ? -1 : 1;
try
{
// Make sure we have a non-zero font size
const auto newSize = std::max(gsl::narrow<short>(_desiredFont.GetEngineSize().Y + fontDelta), static_cast<short>(1));
const auto newSize = std::max(gsl::narrow<short>(_desiredFont.GetEngineSize().Y + (mouseDelta < 0 ? -1 : 1)), static_cast<short>(1));
const auto* fontFace = _settings.FontFace().c_str();
_actualFont = { fontFace, 0, 10, { 0, newSize }, CP_UTF8, false };
_desiredFont = { _actualFont };
Expand Down Expand Up @@ -1192,6 +1192,25 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}
}

// Method Description:
// - Pre-process (if necessary) text pasted (presumably from the clipboard)
// before sending it over the terminal's connection, respecting
// appropriate terminal settings
void TermControl::_SendPastedTextToConnection(const std::wstring& wstr)
{
// Check settings to see if we should be converting line
// endings
if (_settings.ConvertPasteLineEndings())
{
const static std::wregex rgx{ L"\n\r" };
std::wstring stripped = std::regex_replace(wstr, rgx, L"\n");
_SendInputToConnection(stripped);
return;
}

_SendInputToConnection(wstr);
}

void TermControl::_SendInputToConnection(const std::wstring& wstr)
{
_connection.WriteInput(wstr);
Expand Down Expand Up @@ -1255,11 +1274,12 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
if ((_closing) || (!_terminal->IsCursorBlinkingAllowed() && _terminal->IsCursorVisible()))
{
return;
return;
}
_terminal->SetCursorVisible(!_terminal->IsCursorVisible());
}


// Method Description:
// - Sets selection's end position to match supplied cursor position, e.g. while mouse dragging.
// Arguments:
Expand Down Expand Up @@ -1379,11 +1399,12 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

hstring TermControl::Title()
{
if (!_initializedTerminal)
return L"";

hstring hstr(_terminal->GetConsoleTitle());
return hstr;
if (_initializedTerminal)
{
hstring hstr(_terminal->GetConsoleTitle());
return hstr;
}
return L"";
}

// Method Description:
Expand All @@ -1408,7 +1429,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<PasteFromClipboardEventArgs>(clipboardDataHandler);

// send paste event up to TermApp
Expand Down Expand Up @@ -1771,10 +1792,10 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// exit early. This is a single click.
_multiClickCounter = 1;
}
else
else
{
_multiClickCounter++;
}
_multiClickCounter++;
}
return _multiClickCounter;
}

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettings/IControlSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Microsoft.Terminal.Settings
{
Boolean UseAcrylic;
Boolean CloseOnExit;
Boolean ConvertPasteLineEndings;

Double TintOpacity;
ScrollbarState ScrollState;

Expand Down
11 changes: 11 additions & 0 deletions src/cascadia/TerminalSettings/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
_wordDelimiters{ DEFAULT_WORD_DELIMITERS },
_useAcrylic{ false },
_closeOnExit{ true },
_convertPasteLineEndings{ false },
_tintOpacity{ 0.5 },
_padding{ DEFAULT_PADDING },
_fontFace{ DEFAULT_FONT_FACE },
Expand Down Expand Up @@ -169,6 +170,16 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
_closeOnExit = value;
}

bool TerminalSettings::ConvertPasteLineEndings()
{
return _convertPasteLineEndings;
}

void TerminalSettings::ConvertPasteLineEndings(bool value)
{
_convertPasteLineEndings = value;
}

double TerminalSettings::TintOpacity()
{
return _tintOpacity;
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettings/terminalsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
void UseAcrylic(bool value);
bool CloseOnExit();
void CloseOnExit(bool value);
bool ConvertPasteLineEndings();
void ConvertPasteLineEndings(bool value);
double TintOpacity();
void TintOpacity(double value);
hstring Padding();
Expand Down Expand Up @@ -105,6 +107,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
uint32_t _cursorHeight;
hstring _wordDelimiters;

bool _convertPasteLineEndings;
bool _useAcrylic;
bool _closeOnExit;
double _tintOpacity;
Expand Down
1 change: 0 additions & 1 deletion src/propsheet/ColorsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ static int iColor;
break;
default:
return SimpleColorControlProc(hColor, wMsg, wParam, lParam);
break;
}
return TRUE;
}
Expand Down