Skip to content

Commit

Permalink
Remove VT color quirk for PowerShell (#17666)
Browse files Browse the repository at this point in the history
Roughly 4 years ago we gave Windows Terminal the ability to
differentiate between black/white and the default colors.
One of the victims was PowerShell and most importantly PSReadLine,
which emit SRG 37 & 40 when what they really want is 38 & 48.
We fixed this on our side by adding a shim.

Since the addition of VT passthrough in #17510 we now intentionally
lost the ability to translate VT sequences from one thing to another.
This meant we also lost the ability to do this shim and as such
this PR removes it. Luckily Windows 11 now ships PSReadLine 2.0.0,
which contains a proper fix for this.

Unfortunately, this is not the case for Windows 10, which ships
PSReadLine 2.0.0-beta2. Users affected by this will have to install
a newer version of PSReadLine or use the default black/white theme.

See 1bf4c08

Closes #13037
  • Loading branch information
lhecker committed Aug 6, 2024
1 parent 5174c96 commit dfb5233
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 275 deletions.
41 changes: 0 additions & 41 deletions src/buffer/out/TextAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,47 +65,6 @@ void TextAttribute::SetLegacyDefaultAttributes(const WORD defaultAttributes) noe
gsl::at(s_legacyBackgroundColorMap, s_legacyDefaultBackground) = TextColor{};
}

// Routine Description:
// Pursuant to GH#6807
// This routine replaces VT colors from the 16-color set with the "default"
// flag. It is intended to be used as part of the "VT Quirk" in
// WriteConsole[AW].
//
// There is going to be a very long tail of applications that will
// explicitly request VT SGR 40/37 when what they really want is to
// SetConsoleTextAttribute() with a black background/white foreground.
// Instead of making those applications look bad (and therefore making us
// look bad, because we're releasing this as an update to something that
// "looks good" already), we're introducing this compatibility hack. Before
// the color reckoning in GH#6698 + GH#6506, *every* color was subject to
// being spontaneously and erroneously turned into the default color. Now,
// only the 16-color palette value that matches the active console
// background color will be destroyed when the quirk is enabled.
//
// This is not intended to be a long-term solution. This comment will be
// discovered in forty years(*) time and people will laugh at our hubris.
//
// *it doesn't matter when you're reading this, it will always be 40 years
// from now.
TextAttribute TextAttribute::StripErroneousVT16VersionsOfLegacyDefaults(const TextAttribute& attribute) noexcept
{
const auto fg{ attribute.GetForeground() };
const auto bg{ attribute.GetBackground() };
auto copy{ attribute };
if (fg.IsIndex16() &&
attribute.IsIntense() == WI_IsFlagSet(s_ansiDefaultForeground, FOREGROUND_INTENSITY) &&
fg.GetIndex() == (s_ansiDefaultForeground & ~FOREGROUND_INTENSITY))
{
// We don't want to turn 1;37m into 39m (or even 1;39m), as this was meant to mimic a legacy color.
copy.SetDefaultForeground();
}
if (bg.IsIndex16() && bg.GetIndex() == s_ansiDefaultBackground)
{
copy.SetDefaultBackground();
}
return copy;
}

// Routine Description:
// - Returns a WORD with legacy-style attributes for this textattribute.
// Parameters:
Expand Down
1 change: 0 additions & 1 deletion src/buffer/out/TextAttribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class TextAttribute final
}

static void SetLegacyDefaultAttributes(const WORD defaultAttributes) noexcept;
static TextAttribute StripErroneousVT16VersionsOfLegacyDefaults(const TextAttribute& attribute) noexcept;
WORD GetLegacyAttributes() const noexcept;

bool IsTopHorizontalDisplayed() const noexcept;
Expand Down
2 changes: 0 additions & 2 deletions src/host/ApiRoutines.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ class ApiRoutines : public IApiRoutines
[[nodiscard]] HRESULT WriteConsoleAImpl(IConsoleOutputObject& context,
const std::string_view buffer,
size_t& read,
bool requiresVtQuirk,
std::unique_ptr<IWaitRoutine>& waiter) noexcept override;

[[nodiscard]] HRESULT WriteConsoleWImpl(IConsoleOutputObject& context,
const std::wstring_view buffer,
size_t& read,
bool requiresVtQuirk,
std::unique_ptr<IWaitRoutine>& waiter) noexcept override;

#pragma region ThreadCreationInfo
Expand Down
24 changes: 4 additions & 20 deletions src/host/_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ void WriteClearScreen(SCREEN_INFORMATION& screenInfo)
[[nodiscard]] NTSTATUS DoWriteConsole(_In_reads_bytes_(*pcbBuffer) PCWCHAR pwchBuffer,
_Inout_ size_t* const pcbBuffer,
SCREEN_INFORMATION& screenInfo,
bool requiresVtQuirk,
std::unique_ptr<WriteData>& waiter)
try
{
Expand All @@ -416,22 +415,10 @@ try
waiter = std::make_unique<WriteData>(screenInfo,
pwchBuffer,
*pcbBuffer,
gci.OutputCP,
requiresVtQuirk);
gci.OutputCP);
return CONSOLE_STATUS_WAIT;
}

const auto restoreVtQuirk = wil::scope_exit([&]() {
if (requiresVtQuirk)
{
screenInfo.ResetIgnoreLegacyEquivalentVTAttributes();
}
});
if (requiresVtQuirk)
{
screenInfo.SetIgnoreLegacyEquivalentVTAttributes();
}

const std::wstring_view str{ pwchBuffer, *pcbBuffer / sizeof(WCHAR) };

if (WI_IsAnyFlagClear(screenInfo.OutputMode, ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT))
Expand Down Expand Up @@ -464,7 +451,6 @@ NT_CATCH_RETURN()
[[nodiscard]] HRESULT WriteConsoleWImplHelper(IConsoleOutputObject& context,
const std::wstring_view buffer,
size_t& read,
bool requiresVtQuirk,
std::unique_ptr<WriteData>& waiter) noexcept
{
try
Expand All @@ -477,7 +463,7 @@ NT_CATCH_RETURN()
size_t cbTextBufferLength;
RETURN_IF_FAILED(SizeTMult(buffer.size(), sizeof(wchar_t), &cbTextBufferLength));

auto Status = DoWriteConsole(const_cast<wchar_t*>(buffer.data()), &cbTextBufferLength, context, requiresVtQuirk, waiter);
auto Status = DoWriteConsole(const_cast<wchar_t*>(buffer.data()), &cbTextBufferLength, context, waiter);

// Convert back from bytes to characters for the resulting string length written.
read = cbTextBufferLength / sizeof(wchar_t);
Expand Down Expand Up @@ -510,7 +496,6 @@ NT_CATCH_RETURN()
[[nodiscard]] HRESULT ApiRoutines::WriteConsoleAImpl(IConsoleOutputObject& context,
const std::string_view buffer,
size_t& read,
bool requiresVtQuirk,
std::unique_ptr<IWaitRoutine>& waiter) noexcept
{
try
Expand Down Expand Up @@ -622,7 +607,7 @@ NT_CATCH_RETURN()

// Make the W version of the call
size_t wcBufferWritten{};
const auto hr{ WriteConsoleWImplHelper(screenInfo, wstr, wcBufferWritten, requiresVtQuirk, writeDataWaiter) };
const auto hr{ WriteConsoleWImplHelper(screenInfo, wstr, wcBufferWritten, writeDataWaiter) };

// If there is no waiter, process the byte count now.
if (nullptr == writeDataWaiter.get())
Expand Down Expand Up @@ -700,7 +685,6 @@ NT_CATCH_RETURN()
[[nodiscard]] HRESULT ApiRoutines::WriteConsoleWImpl(IConsoleOutputObject& context,
const std::wstring_view buffer,
size_t& read,
bool requiresVtQuirk,
std::unique_ptr<IWaitRoutine>& waiter) noexcept
{
try
Expand All @@ -709,7 +693,7 @@ NT_CATCH_RETURN()
auto unlock = wil::scope_exit([&] { UnlockConsole(); });

std::unique_ptr<WriteData> writeDataWaiter;
RETURN_IF_FAILED(WriteConsoleWImplHelper(context.GetActiveBuffer(), buffer, read, requiresVtQuirk, writeDataWaiter));
RETURN_IF_FAILED(WriteConsoleWImplHelper(context.GetActiveBuffer(), buffer, read, writeDataWaiter));

// Transfer specific waiter pointer into the generic interface wrapper.
waiter.reset(writeDataWaiter.release());
Expand Down
1 change: 0 additions & 1 deletion src/host/_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ void WriteClearScreen(SCREEN_INFORMATION& screenInfo);
[[nodiscard]] NTSTATUS DoWriteConsole(_In_reads_bytes_(pcbBuffer) const wchar_t* pwchBuffer,
_Inout_ size_t* const pcbBuffer,
SCREEN_INFORMATION& screenInfo,
bool requiresVtQuirk,
std::unique_ptr<WriteData>& waiter);
24 changes: 1 addition & 23 deletions src/host/screenInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ SCREEN_INFORMATION::SCREEN_INFORMATION(
_PopupAttributes{ popupAttributes },
_virtualBottom{ 0 },
_currentFont{ fontInfo },
_desiredFont{ fontInfo },
_ignoreLegacyEquivalentVTAttributes{ false }
_desiredFont{ fontInfo }
{
// Check if VT mode should be enabled by default. This can be true if
// VirtualTerminalLevel is set to !=0 in the registry, or when conhost
Expand Down Expand Up @@ -2014,13 +2013,6 @@ const TextAttribute& SCREEN_INFORMATION::GetPopupAttributes() const noexcept
// <none>
void SCREEN_INFORMATION::SetAttributes(const TextAttribute& attributes)
{
if (_ignoreLegacyEquivalentVTAttributes)
{
// See the comment on StripErroneousVT16VersionsOfLegacyDefaults for more info.
_textBuffer->SetCurrentAttributes(TextAttribute::StripErroneousVT16VersionsOfLegacyDefaults(attributes));
return;
}

_textBuffer->SetCurrentAttributes(attributes);

// If we're an alt buffer, DON'T propagate this setting up to the main buffer.
Expand Down Expand Up @@ -2466,17 +2458,3 @@ const FontInfoDesired& SCREEN_INFORMATION::GetDesiredFont() const noexcept
{
return _desiredFont;
}

// Routine Description:
// - Engages the legacy VT handling quirk; see TextAttribute::StripErroneousVT16VersionsOfLegacyDefaults
void SCREEN_INFORMATION::SetIgnoreLegacyEquivalentVTAttributes() noexcept
{
_ignoreLegacyEquivalentVTAttributes = true;
}

// Routine Description:
// - Disengages the legacy VT handling quirk; see TextAttribute::StripErroneousVT16VersionsOfLegacyDefaults
void SCREEN_INFORMATION::ResetIgnoreLegacyEquivalentVTAttributes() noexcept
{
_ignoreLegacyEquivalentVTAttributes = false;
}
5 changes: 0 additions & 5 deletions src/host/screenInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ class SCREEN_INFORMATION : public ConsoleObjectHeader, public Microsoft::Console
FontInfoDesired& GetDesiredFont() noexcept;
const FontInfoDesired& GetDesiredFont() const noexcept;

void SetIgnoreLegacyEquivalentVTAttributes() noexcept;
void ResetIgnoreLegacyEquivalentVTAttributes() noexcept;

[[nodiscard]] NTSTATUS ResizeWithReflow(const til::size coordnewScreenSize);
[[nodiscard]] NTSTATUS ResizeTraditional(const til::size coordNewScreenSize);

Expand Down Expand Up @@ -277,8 +274,6 @@ class SCREEN_INFORMATION : public ConsoleObjectHeader, public Microsoft::Console
// the viewport to move (SetBufferInfo, WriteConsole, etc)
til::CoordType _virtualBottom;

bool _ignoreLegacyEquivalentVTAttributes;

std::optional<til::size> _deferredPtyResize{ std::nullopt };

static void _handleDeferredResize(SCREEN_INFORMATION& siMain);
Expand Down
4 changes: 2 additions & 2 deletions src/host/ut_host/ApiRoutinesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class ApiRoutinesTests
const auto cchWriteLength = std::min(cchIncrement, cchTestText - i);

// Run the test method
const auto hr = _pApiRoutines->WriteConsoleAImpl(si, { pszTestText + i, cchWriteLength }, cchRead, false, waiter);
const auto hr = _pApiRoutines->WriteConsoleAImpl(si, { pszTestText + i, cchWriteLength }, cchRead, waiter);

VERIFY_ARE_EQUAL(S_OK, hr, L"Successful result code from writing.");
if (!fInduceWait)
Expand Down Expand Up @@ -441,7 +441,7 @@ class ApiRoutinesTests

size_t cchRead = 0;
std::unique_ptr<IWaitRoutine> waiter;
const auto hr = _pApiRoutines->WriteConsoleWImpl(si, testText, cchRead, false, waiter);
const auto hr = _pApiRoutines->WriteConsoleWImpl(si, testText, cchRead, waiter);

VERIFY_ARE_EQUAL(S_OK, hr, L"Successful result code from writing.");
if (!fInduceWait)
Expand Down
Loading

0 comments on commit dfb5233

Please sign in to comment.