[PR #5758] [MERGED] Refactor the SGR implementation in AdaptDispatch #26459

Open
opened 2026-01-31 09:16:14 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/5758
Author: @j4james
Created: 5/5/2020
Status: Merged
Merged: 5/9/2020
Merged by: @DHowett-MSFT

Base: masterHead: refactor-sgr-dispatch


📝 Commits (10+)

  • dbf25c4 Get rid of unused legacy methods in ATTR_ROW and OutputCell.
  • 562bdec Replace unnecessary usage of TextAttributeRun::SetAttributesFromLegacy with SetAttributes.
  • aeb1c7f Replace unnecessary usage of TextAttribute::SetFromLegacy with TextAttribute constructor.
  • 0f08581 Add methods to the TextAttribute class to update attributes with less dependence on internal details.
  • 249003f Update SetColorTableEntry API to take XTerm indices and add corresponding GetColorTableEntry API.
  • 9534337 Rewrite the AdaptDispatch::SetGraphicsRendition method with a simpler implementation.
  • 3c96482 Update adapter tests to account for the changes to the ConGetSet API.
  • ee517f6 Remove all the APIs that aren't needed anymore.
  • 1513de7 Add suggested comment changes and TODO note.
  • 9252ea2 Add bitfield(s) and href to the apis dictionary.

📊 Changes

28 files changed (+519 additions, -1452 deletions)

View changed files

📝 .github/actions/spell-check/dictionary/apis.txt (+3 -0)
📝 src/buffer/out/AttrRow.cpp (+0 -20)
📝 src/buffer/out/AttrRow.hpp (+0 -1)
📝 src/buffer/out/OutputCell.cpp (+0 -26)
📝 src/buffer/out/OutputCell.hpp (+0 -2)
📝 src/buffer/out/OutputCellIterator.cpp (+1 -2)
📝 src/buffer/out/TextAttribute.cpp (+75 -81)
📝 src/buffer/out/TextAttribute.hpp (+21 -37)
📝 src/buffer/out/TextAttributeRun.cpp (+0 -11)
📝 src/buffer/out/TextAttributeRun.hpp (+0 -1)
📝 src/buffer/out/ut_textbuffer/TextAttributeTests.cpp (+7 -7)
📝 src/cascadia/TerminalCore/TerminalApi.cpp (+5 -20)
📝 src/cascadia/TerminalCore/terminalrenderdata.cpp (+1 -1)
📝 src/cascadia/UnitTests_TerminalCore/ConptyRoundtripTests.cpp (+4 -4)
📝 src/host/getset.cpp (+24 -136)
📝 src/host/getset.h (+2 -24)
📝 src/host/outputStream.cpp (+14 -119)
📝 src/host/outputStream.hpp (+2 -22)
📝 src/host/ut_host/AttrRowTests.cpp (+6 -6)
📝 src/host/ut_host/OutputCellIteratorTests.cpp (+6 -12)

...and 8 more files

📄 Description

Summary of the Pull Request

This is an attempt to simplify the SGR (Select Graphic Rendition) implementation in conhost, to cut down on the number of methods required in the ConGetSet interface, and pave the way for future improvements and bug fixes. It already fixes one bug that prevented SGR 0 from being correctly applied when combined with meta attributes.

References

  • This a first step towards fixing the conpty narrowing bugs in issue #2661
  • I'm hoping the simplification of ConGetSet will also help with #3849.
  • Some of the TextAttribute refactoring in this PR overlaps with similar work in PR #1978.

PR Checklist

Detailed Description of the Pull Request / Additional comments

The main point of this PR was to simplify the AdaptDispatch::SetGraphicsRendition implementation. So instead of having it call a half a dozen methods in the ConGetSet API, depending on what kinds of attributes needed to be set, there is now just one call to get current attributes, and another call to set the new value. All adjustments to the attributes are made in the AdaptDispatch class, in a simple switch statement.

To help with this refactoring, I also made some change to the TextAttribute class to make it easier to work with. This included adding a set of methods for setting (and getting) the individual attribute flags, instead of having the calling code being exposed to the internal attribute structures and messing with bit manipulation. I've tried to get rid of any methods that were directly setting legacy, meta, and extended attributes.

Other than the fix to the SGR 0 bug, the AdaptDispatch refactoring mostly follows the behaviour of the original code. In particular, it still maps the SGR 38/48 indexed colors to RGB instead of retaining the index, which is what we ultimately need it to do. Fixing that will first require the color tables to be unified (issue #1223), which I'm hoping to address in a followup PR.

But for now, mapping the indexed colors to RGB values required adding an an additional ConGetSet API to lookup the color table entries. In the future that won't be necessary, but the API will still be useful for other color reporting operations that we may want to support. I've made this API, and the existing setter, standardise on index values being in the "Xterm" order, since that'll be essential for unifying the code with the terminal adapter one day.

I should also point out one minor change to the SGR 38/48 behavior, which is that out-of-range RGB colors are now ignored rather than being clamped, since that matches the way Xterm works.

Validation Steps Performed

This refactoring has obviously required corresponding changes to the unit tests, but most were just minor updates to use the new TextAttribute methods without any real change in behavior. However, the adapter tests did require significant changes to accommodate the new ConGetSet API. The basic structure of the tests remain the same, but the simpler API has meant fewer values needed to be checked in each test case. I think they are all still covering the areas there were intended to, though, and they are all still passing.

Other than getting the unit tests to work, I've also done a bunch of manual testing of my own. I've made sure the color tests in Vttest all still work as well as they used to. And I've confirmed that the test case from issue #5341 is now working correctly.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/5758 **Author:** [@j4james](https://github.com/j4james) **Created:** 5/5/2020 **Status:** ✅ Merged **Merged:** 5/9/2020 **Merged by:** [@DHowett-MSFT](https://github.com/DHowett-MSFT) **Base:** `master` ← **Head:** `refactor-sgr-dispatch` --- ### 📝 Commits (10+) - [`dbf25c4`](https://github.com/microsoft/terminal/commit/dbf25c4849ff7aeebb5dfc03b26a72ec59877ebf) Get rid of unused legacy methods in ATTR_ROW and OutputCell. - [`562bdec`](https://github.com/microsoft/terminal/commit/562bdece15bdf7561d702acd05614292a21a32b5) Replace unnecessary usage of TextAttributeRun::SetAttributesFromLegacy with SetAttributes. - [`aeb1c7f`](https://github.com/microsoft/terminal/commit/aeb1c7f64a4bc062f3a366a1f89288933fc74520) Replace unnecessary usage of TextAttribute::SetFromLegacy with TextAttribute constructor. - [`0f08581`](https://github.com/microsoft/terminal/commit/0f0858135363ca597b0c4f288d5fab5245afda8d) Add methods to the TextAttribute class to update attributes with less dependence on internal details. - [`249003f`](https://github.com/microsoft/terminal/commit/249003fa8d4eb37bda12f31514c490858d7985ff) Update SetColorTableEntry API to take XTerm indices and add corresponding GetColorTableEntry API. - [`9534337`](https://github.com/microsoft/terminal/commit/95343370aa5c2f7e36934a4b903ab3c435053e29) Rewrite the AdaptDispatch::SetGraphicsRendition method with a simpler implementation. - [`3c96482`](https://github.com/microsoft/terminal/commit/3c96482d1e06556b96e3088bc88e841eb74904b2) Update adapter tests to account for the changes to the ConGetSet API. - [`ee517f6`](https://github.com/microsoft/terminal/commit/ee517f676740456f3c1d94963b209ddd17028630) Remove all the APIs that aren't needed anymore. - [`1513de7`](https://github.com/microsoft/terminal/commit/1513de7607d75f067093cee2831770c7940c2a6e) Add suggested comment changes and TODO note. - [`9252ea2`](https://github.com/microsoft/terminal/commit/9252ea2ad48ff35c134a47ef8e0c8aef1df5edf0) Add bitfield(s) and href to the apis dictionary. ### 📊 Changes **28 files changed** (+519 additions, -1452 deletions) <details> <summary>View changed files</summary> 📝 `.github/actions/spell-check/dictionary/apis.txt` (+3 -0) 📝 `src/buffer/out/AttrRow.cpp` (+0 -20) 📝 `src/buffer/out/AttrRow.hpp` (+0 -1) 📝 `src/buffer/out/OutputCell.cpp` (+0 -26) 📝 `src/buffer/out/OutputCell.hpp` (+0 -2) 📝 `src/buffer/out/OutputCellIterator.cpp` (+1 -2) 📝 `src/buffer/out/TextAttribute.cpp` (+75 -81) 📝 `src/buffer/out/TextAttribute.hpp` (+21 -37) 📝 `src/buffer/out/TextAttributeRun.cpp` (+0 -11) 📝 `src/buffer/out/TextAttributeRun.hpp` (+0 -1) 📝 `src/buffer/out/ut_textbuffer/TextAttributeTests.cpp` (+7 -7) 📝 `src/cascadia/TerminalCore/TerminalApi.cpp` (+5 -20) 📝 `src/cascadia/TerminalCore/terminalrenderdata.cpp` (+1 -1) 📝 `src/cascadia/UnitTests_TerminalCore/ConptyRoundtripTests.cpp` (+4 -4) 📝 `src/host/getset.cpp` (+24 -136) 📝 `src/host/getset.h` (+2 -24) 📝 `src/host/outputStream.cpp` (+14 -119) 📝 `src/host/outputStream.hpp` (+2 -22) 📝 `src/host/ut_host/AttrRowTests.cpp` (+6 -6) 📝 `src/host/ut_host/OutputCellIteratorTests.cpp` (+6 -12) _...and 8 more files_ </details> ### 📄 Description ## Summary of the Pull Request This is an attempt to simplify the SGR (Select Graphic Rendition) implementation in conhost, to cut down on the number of methods required in the `ConGetSet` interface, and pave the way for future improvements and bug fixes. It already fixes one bug that prevented SGR 0 from being correctly applied when combined with meta attributes. ## References * This a first step towards fixing the conpty narrowing bugs in issue #2661 * I'm hoping the simplification of `ConGetSet` will also help with #3849. * Some of the `TextAttribute` refactoring in this PR overlaps with similar work in PR #1978. ## PR Checklist * [x] Closes #5341 * [x] CLA signed. * [x] 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: https://github.com/microsoft/terminal/pull/3160#discussion_r341249386 ## Detailed Description of the Pull Request / Additional comments The main point of this PR was to simplify the `AdaptDispatch::SetGraphicsRendition` implementation. So instead of having it call a half a dozen methods in the `ConGetSet` API, depending on what kinds of attributes needed to be set, there is now just one call to get current attributes, and another call to set the new value. All adjustments to the attributes are made in the `AdaptDispatch` class, in a simple switch statement. To help with this refactoring, I also made some change to the `TextAttribute` class to make it easier to work with. This included adding a set of methods for setting (and getting) the individual attribute flags, instead of having the calling code being exposed to the internal attribute structures and messing with bit manipulation. I've tried to get rid of any methods that were directly setting legacy, meta, and extended attributes. Other than the fix to the `SGR 0` bug, the `AdaptDispatch` refactoring mostly follows the behaviour of the original code. In particular, it still maps the `SGR 38/48` indexed colors to RGB instead of retaining the index, which is what we ultimately need it to do. Fixing that will first require the color tables to be unified (issue #1223), which I'm hoping to address in a followup PR. But for now, mapping the indexed colors to RGB values required adding an an additional `ConGetSet` API to lookup the color table entries. In the future that won't be necessary, but the API will still be useful for other color reporting operations that we may want to support. I've made this API, and the existing setter, standardise on index values being in the "Xterm" order, since that'll be essential for unifying the code with the terminal adapter one day. I should also point out one minor change to the `SGR 38/48` behavior, which is that out-of-range RGB colors are now ignored rather than being clamped, since that matches the way Xterm works. ## Validation Steps Performed This refactoring has obviously required corresponding changes to the unit tests, but most were just minor updates to use the new `TextAttribute` methods without any real change in behavior. However, the adapter tests did require significant changes to accommodate the new `ConGetSet` API. The basic structure of the tests remain the same, but the simpler API has meant fewer values needed to be checked in each test case. I think they are all still covering the areas there were intended to, though, and they are all still passing. Other than getting the unit tests to work, I've also done a bunch of manual testing of my own. I've made sure the color tests in Vttest all still work as well as they used to. And I've confirmed that the test case from issue #5341 is now working correctly. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:16:14 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#26459