[PR #2367] Make the RIS command clear the display and scrollback correctly #24906

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

Original Pull Request: https://github.com/microsoft/terminal/pull/2367

State: closed
Merged: Yes


Summary of the Pull Request

When the scrollback buffer is empty, the RIS escape sequence (Reset to Initial State) will fail to clear the screen, or reset any of the state. And when there is something in the scrollback, it doesn't get cleared completely, and the screen may get filled with the wrong background color (it should use the default color, but it actually uses the previously active background color). This PR attempts to fix those issues.

PR Checklist

  • Closes mc is slower and shows artifacts (#2307)
  • CLA signed. If not, go over here and sign the CLA
  • Tests added/passed
  • Requires documentation to be updated
  • 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: #2307

Detailed Description of the Pull Request / Additional comments

The initial failure is caused by the SCREEN_INFORMATION::WriteRect method throwing an exception when passed an empty viewport. And the reason it's passed an empty viewport is because that's what the Viewport::Subtract method returns when the result of the subtraction is nothing. The PR fixes the problem by making the Viewport::Subtract method actually return nothing in that situation.

This is a change in the defined behavior that also required the associated viewport tests to be updated. However, it does seem a sensible change, since the Subtract method never returns empty viewports under any other circumstances. And the only place the method seems to be used is in the ScrollRegion implementation, where the previous behavior is guaranteed to throw an exception.

The other issues are fixed simply by changing the order in which things are reset in the AdaptDispatch::HardReset method. The call to SoftReset needed to be made first, so that the SGR attributes would be reset before the screen was cleared, thus making sure that the default background color would be used. And the screen needed to be cleared before the scrollback was erased, otherwise the last view of the screen would be retained in the scrollback buffer.

These changes also required existing adapter tests to be updated, but not because of a change in the expected behaviour. It's just that certain tests relied on the SoftReset happening later in the order, so weren't expecting it to be called if say the scrollback erase had failed. It doesn't seem like the tests were deliberately trying to verify that the SoftReset hadn't been called.

Validation Steps Performed

In addition to the updates to existing tests, this PR also add a new screen buffer test which verifies the display and scrollback are correctly cleared under the conditions that were previously failing.

**Original Pull Request:** https://github.com/microsoft/terminal/pull/2367 **State:** closed **Merged:** Yes --- ## Summary of the Pull Request When the scrollback buffer is empty, the RIS escape sequence (Reset to Initial State) will fail to clear the screen, or reset any of the state. And when there is something in the scrollback, it doesn't get cleared completely, and the screen may get filled with the wrong background color (it should use the default color, but it actually uses the previously active background color). This PR attempts to fix those issues. ## PR Checklist * [x] Closes #2307 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [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: #2307 ## Detailed Description of the Pull Request / Additional comments The initial failure is caused by the `SCREEN_INFORMATION::WriteRect` method [throwing an exception](https://github.com/microsoft/terminal/blob/7abcc35fdf7d98bc98016cda8f4b90398c65692e/src/host/screenInfo.cpp#L2608-L2609) when passed an empty viewport. And the reason it's passed an empty viewport is because that's what the `Viewport::Subtract` method returns [when the result of the subtraction is nothing](https://github.com/microsoft/terminal/blob/9b92986b49bed8cc41fde4d6ef080921c41e6d9e/src/types/viewport.cpp#L995-L998). The PR fixes the problem by making the `Viewport::Subtract` method actually return nothing in that situation. This is a change in the defined behavior that also required the associated viewport tests to be updated. However, it does seem a sensible change, since the `Subtract` method never returns empty viewports under any other circumstances. And [the only place the method seems to be used](https://github.com/microsoft/terminal/blob/9b92986b49bed8cc41fde4d6ef080921c41e6d9e/src/host/output.cpp#L469-L476) is in the `ScrollRegion` implementation, where the previous behavior is guaranteed to throw an exception. The other issues are fixed simply by changing the order in which things are reset in the `AdaptDispatch::HardReset` method. The call to `SoftReset` needed to be made first, so that the SGR attributes would be reset before the screen was cleared, thus making sure that the default background color would be used. And the screen needed to be cleared before the scrollback was erased, otherwise the last view of the screen would be retained in the scrollback buffer. These changes also required existing adapter tests to be updated, but not because of a change in the expected behaviour. It's just that certain tests relied on the `SoftReset` happening later in the order, so weren't expecting it to be called if say the scrollback erase had failed. It doesn't seem like the tests were deliberately trying to verify that the SoftReset _hadn't_ been called. ## Validation Steps Performed In addition to the updates to existing tests, this PR also add a new screen buffer test which verifies the display and scrollback are correctly cleared under the conditions that were previously failing.
claunia added the pull-request label 2026-01-31 09:06:02 +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#24906