Skip to content

Commit

Permalink
Update TextAttribute operations to support XTPUSHSGR/XTPOPSGR.
Browse files Browse the repository at this point in the history
The XTPUSHSGR / XTPOPSGR control sequences support restoring just a
portion of saved text attributes (such as "just the foreground color").
This commit introduces operations to support that.
  • Loading branch information
jazzdelightsme committed Dec 18, 2019
1 parent 0d51e9b commit 66c2a69
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
56 changes: 51 additions & 5 deletions src/buffer/out/TextAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COLORREF TextAttribute::CalculateRgbForeground(std::basic_string_view<COLORREF>
COLORREF defaultFgColor,
COLORREF defaultBgColor) const noexcept
{
return _IsReverseVideo() ? _GetRgbBackground(colorTable, defaultBgColor) : _GetRgbForeground(colorTable, defaultFgColor);
return IsReverseVideo() ? _GetRgbBackground(colorTable, defaultBgColor) : _GetRgbForeground(colorTable, defaultFgColor);
}

// Routine Description:
Expand All @@ -31,7 +31,33 @@ COLORREF TextAttribute::CalculateRgbBackground(std::basic_string_view<COLORREF>
COLORREF defaultFgColor,
COLORREF defaultBgColor) const noexcept
{
return _IsReverseVideo() ? _GetRgbForeground(colorTable, defaultFgColor) : _GetRgbBackground(colorTable, defaultBgColor);
return IsReverseVideo() ? _GetRgbForeground(colorTable, defaultFgColor) : _GetRgbBackground(colorTable, defaultBgColor);
}

// Routine Description:
// - Makes this TextAttribute's foreground color the same as the other one.
// Arguments:
// - The TextAttribute to copy the foreground color from
// Return Value:
// - <none>
void TextAttribute::SetForegroundFrom(const TextAttribute& other) noexcept
{
_foreground = other._foreground;
WI_ClearAllFlags(_wAttrLegacy, FG_ATTRS);
_wAttrLegacy |= (other._wAttrLegacy & FG_ATTRS);
}

// Routine Description:
// - Makes this TextAttribute's background color the same as the other one.
// Arguments:
// - The TextAttribute to copy the background color from
// Return Value:
// - <none>
void TextAttribute::SetBackgroundFrom(const TextAttribute& other) noexcept
{
_background = other._background;
WI_ClearAllFlags(_wAttrLegacy, BG_ATTRS);
_wAttrLegacy |= (other._wAttrLegacy & BG_ATTRS);
}

// Routine Description:
Expand Down Expand Up @@ -155,7 +181,12 @@ void TextAttribute::SetColor(const COLORREF rgbColor, const bool fIsForeground)
}
}

bool TextAttribute::_IsReverseVideo() const noexcept
bool TextAttribute::IsUnderline() const noexcept
{
return IsBottomHorizontalDisplayed();
}

bool TextAttribute::IsReverseVideo() const noexcept
{
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_REVERSE_VIDEO);
}
Expand Down Expand Up @@ -190,16 +221,21 @@ bool TextAttribute::IsRightVerticalDisplayed() const noexcept
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_GRID_RVERTICAL);
}

void TextAttribute::SetLeftVerticalDisplayed(const bool isDisplayed) noexcept
void TextAttribute::SetLeftVerticalDisplayed(bool isDisplayed) noexcept
{
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_GRID_LVERTICAL, isDisplayed);
}

void TextAttribute::SetRightVerticalDisplayed(const bool isDisplayed) noexcept
void TextAttribute::SetRightVerticalDisplayed(bool isDisplayed) noexcept
{
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_GRID_RVERTICAL, isDisplayed);
}

void TextAttribute::SetBottomHorizontalDisplayed(bool isDisplayed) noexcept
{
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_UNDERSCORE, isDisplayed);
}

void TextAttribute::Embolden() noexcept
{
_SetBoldness(true);
Expand All @@ -210,6 +246,16 @@ void TextAttribute::Debolden() noexcept
_SetBoldness(false);
}

void TextAttribute::EnableUnderline() noexcept
{
SetBottomHorizontalDisplayed(true);
}

void TextAttribute::DisableUnderline() noexcept
{
SetBottomHorizontalDisplayed(false);
}

void TextAttribute::SetExtendedAttributes(const ExtendedAttributes attrs) noexcept
{
_extendedAttrs = attrs;
Expand Down
14 changes: 11 additions & 3 deletions src/buffer/out/TextAttribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ class TextAttribute final
COLORREF defaultFgColor,
COLORREF defaultBgColor) const noexcept;

void SetForegroundFrom(const TextAttribute& other) noexcept;
void SetBackgroundFrom(const TextAttribute& other) noexcept;

bool IsLeadingByte() const noexcept;
bool IsTrailingByte() const noexcept;
bool IsTopHorizontalDisplayed() const noexcept;
bool IsBottomHorizontalDisplayed() const noexcept;
bool IsLeftVerticalDisplayed() const noexcept;
bool IsRightVerticalDisplayed() const noexcept;

void SetLeftVerticalDisplayed(const bool isDisplayed) noexcept;
void SetRightVerticalDisplayed(const bool isDisplayed) noexcept;
void SetLeftVerticalDisplayed(bool isDisplayed) noexcept;
void SetRightVerticalDisplayed(bool isDisplayed) noexcept;
void SetBottomHorizontalDisplayed(bool isDisplayed) noexcept;

void SetFromLegacy(const WORD wLegacy) noexcept;

Expand All @@ -122,6 +126,8 @@ class TextAttribute final

void Embolden() noexcept;
void Debolden() noexcept;
void EnableUnderline() noexcept;
void DisableUnderline() noexcept;

void Invert() noexcept;

Expand All @@ -139,6 +145,9 @@ class TextAttribute final
return WI_IsFlagSet(_extendedAttrs, ExtendedAttributes::Bold);
}

bool IsUnderline() const noexcept;
bool IsReverseVideo() const noexcept;

constexpr ExtendedAttributes GetExtendedAttributes() const noexcept
{
return _extendedAttrs;
Expand Down Expand Up @@ -168,7 +177,6 @@ class TextAttribute final
COLORREF defaultColor) const noexcept;
COLORREF _GetRgbBackground(std::basic_string_view<COLORREF> colorTable,
COLORREF defaultColor) const noexcept;
bool _IsReverseVideo() const noexcept;
void _SetBoldness(const bool isBold) noexcept;

WORD _wAttrLegacy;
Expand Down
10 changes: 5 additions & 5 deletions src/buffer/out/ut_textbuffer/TextAttributeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void TextAttributeTests::TestTextAttributeColorGetters()

// verify that calculated foreground/background are the same as the direct
// values when reverse video is not set
VERIFY_IS_FALSE(attr._IsReverseVideo());
VERIFY_IS_FALSE(attr.IsReverseVideo());

VERIFY_ARE_EQUAL(red, attr._GetRgbForeground(view, _defaultFg));
VERIFY_ARE_EQUAL(red, attr.CalculateRgbForeground(view, _defaultFg, _defaultBg));
Expand Down Expand Up @@ -166,7 +166,7 @@ void TextAttributeTests::TestReverseDefaultColors()

// verify that calculated foreground/background are the same as the direct
// values when reverse video is not set
VERIFY_IS_FALSE(attr._IsReverseVideo());
VERIFY_IS_FALSE(attr.IsReverseVideo());

VERIFY_ARE_EQUAL(_defaultFg, attr._GetRgbForeground(view, _defaultFg));
VERIFY_ARE_EQUAL(_defaultFg, attr.CalculateRgbForeground(view, _defaultFg, _defaultBg));
Expand All @@ -177,7 +177,7 @@ void TextAttributeTests::TestReverseDefaultColors()
// with reverse video set, calucated foreground/background values should be
// switched while getters stay the same
attr.SetMetaAttributes(COMMON_LVB_REVERSE_VIDEO);
VERIFY_IS_TRUE(attr._IsReverseVideo());
VERIFY_IS_TRUE(attr.IsReverseVideo());

VERIFY_ARE_EQUAL(_defaultFg, attr._GetRgbForeground(view, _defaultFg));
VERIFY_ARE_EQUAL(_defaultBg, attr.CalculateRgbForeground(view, _defaultFg, _defaultBg));
Expand All @@ -186,7 +186,7 @@ void TextAttributeTests::TestReverseDefaultColors()
VERIFY_ARE_EQUAL(_defaultFg, attr.CalculateRgbBackground(view, _defaultFg, _defaultBg));

attr.SetForeground(red);
VERIFY_IS_TRUE(attr._IsReverseVideo());
VERIFY_IS_TRUE(attr.IsReverseVideo());

VERIFY_ARE_EQUAL(red, attr._GetRgbForeground(view, _defaultFg));
VERIFY_ARE_EQUAL(_defaultBg, attr.CalculateRgbForeground(view, _defaultFg, _defaultBg));
Expand All @@ -195,7 +195,7 @@ void TextAttributeTests::TestReverseDefaultColors()
VERIFY_ARE_EQUAL(red, attr.CalculateRgbBackground(view, _defaultFg, _defaultBg));

attr.Invert();
VERIFY_IS_FALSE(attr._IsReverseVideo());
VERIFY_IS_FALSE(attr.IsReverseVideo());
attr.SetDefaultForeground();
attr.SetBackground(green);

Expand Down
3 changes: 3 additions & 0 deletions src/terminal/adapter/ut_adapter/Adapter.UnitTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<ProjectReference Include="..\lib\adapter.vcxproj">
<Project>{dcf55140-ef6a-4736-a403-957e4f7430bb}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\buffer\out\lib\bufferout.vcxproj">
<Project>{0cf235bd-2da0-407e-90ee-c467e8bbc714}</Project>
</ProjectReference>
</ItemGroup>
<ItemDefinitionGroup>
<ClCompile>
Expand Down

0 comments on commit 66c2a69

Please sign in to comment.