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

Implement a pair of shims for cls, Clear-Host in conpty mode #5627

Merged
14 commits merged into from Apr 30, 2020

Conversation

zadjii-msft
Copy link
Member

@zadjii-msft zadjii-msft commented Apr 28, 2020

Summary of the Pull Request

This PR implements a pair of shims for cmd and powershell, so that their cls and Clear-Host functions will clear the entire terminal buffer (like they do in conhost), instead of just the viewport. With the conpty viewport and buffer being the same size, there's effectively no way to know if an application is calling these API's in this way with the intention of clearing the buffer or the viewport. We absolutely have to guess.

Each of these shims checks to see if the way that the API is being called exactly matches the way cmd or powershell would call these APIs. If it does, we manually write a ^[[3J to the connected terminal, to get he Terminal to clear it's own scrollback.

⚠️ If another application were trying to clear the viewport with an exactly similar API call, this would also cause the terminal scrollback to get cleared ⚠️

  • Should these shims be restricted to when the process that's calling them is actually cmd.exe or powershell.exe? Can I even do this? I think we've done such a good job of isolating the client process information from the rest of the host code that I can't figure out how to do this.
    • YES, this can be done, and I did it.
  • TODO: While I'm here, should I have DoSrvPrivateEraseAll (the implementation for ^[[2J, in getset.cpp) also manually trigger a EraseAll in the terminal in conpty mode?

PR Checklist

Validation Steps Performed

  • ran tests
  • checked cls in the Terminal
  • checked Clear-Host in the Terminal
  • Checked running powershell clear-host from cmd.exe

@zadjii-msft zadjii-msft added Product-Conpty For console issues specifically related to conpty Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. labels Apr 28, 2020
@zadjii-msft
Copy link
Member Author

@msftbot make sure @miniksa signs off on this

@ghost ghost added the AutoMerge Marked for automatic merge by the bot when requirements are met label Apr 28, 2020
@ghost
Copy link

ghost commented Apr 28, 2020

Hello @zadjii-msft!

Because you've given me some instructions on how to help merge this pull request, I'll be modifying my merge approach. Here's how I understand your requirements for merging this pull request:

  • I'll only merge this pull request if it's approved by @miniksa

If this doesn't seem right to you, you can tell me to cancel these instructions and use the auto-merge policy that has been configured for this repository. Try telling me "forget everything I just told you".

@DHowett-MSFT
Copy link
Contributor

wowww

@ghost ghost added Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Area-VT Virtual Terminal sequence support Issue-Bug It either shouldn't be doing this or needs an investigation. Issue-Question For questions or discussion Priority-2 A description (P2) Product-Cmd.exe The issue is related to the legacy command interpreter, CMD.exe. labels Apr 29, 2020
@zadjii-msft zadjii-msft marked this pull request as ready for review April 29, 2020 21:13
@pingzing
Copy link
Contributor

This should probably check for pwsh.exe in addition to powershell.exe, yeah?

@zadjii-msft
Copy link
Member Author

This should probably check for pwsh.exe in addition to powershell.exe, yeah?

I was under the impression that the updated pwsh.exe would be using ^[[3J, but I just tried it and it doesn't look like that's the case... Hmm. I'm thinking the forward-thinking call would be to just get pwsh updated (since that's actually receiving updates, compared to powershell.exe), but adding pwsh.exe to this list would be trivial.

@ team thoughts?

@DHowett-MSFT
Copy link
Contributor

If pwsh begins using 3J it’ll just skip the shim, which I’m fine with. We should keep the shim because of the long tail of folks on 7.0

Copy link
Contributor

@DHowett-MSFT DHowett-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me; gotta rvw the tests

src/server/ConsoleShimPolicy.cpp Outdated Show resolved Hide resolved
src/server/ConsoleShimPolicy.cpp Outdated Show resolved Hide resolved
src/inc/til/size.h Show resolved Hide resolved
@miniksa
Copy link
Member

miniksa commented Apr 29, 2020

This should probably check for pwsh.exe in addition to powershell.exe, yeah?

I was under the impression that the updated pwsh.exe would be using ^[[3J, but I just tried it and it doesn't look like that's the case... Hmm. I'm thinking the forward-thinking call would be to just get pwsh updated (since that's actually receiving updates, compared to powershell.exe), but adding pwsh.exe to this list would be trivial.

@ team thoughts?

Do it for pwsh.exe too, is my thought.

@DHowett-MSFT
Copy link
Contributor

This feels right. Selfhost 0.11.1204.

src/host/_output.cpp Outdated Show resolved Hide resolved
@ghost ghost merged commit 7612044 into master Apr 30, 2020
@ghost ghost deleted the dev/migrie/b/3126-cls-shim branch April 30, 2020 21:53
@ghost
Copy link

ghost commented May 5, 2020

🎉Windows Terminal Release Candidate v0.11.1251.0 (1.0rc1) has been released which incorporates this pull request.:tada:

Handy links:

DHowett pushed a commit that referenced this pull request Jun 20, 2022
## Summary of the Pull Request

When you execute a `cls` in the cmd shell, or `Clear-Host` in
PowerShell, we have a pair of shims that attempt to detect those
operations and forward an `ED3` sequence to conpty to clear the
scrollback.

If there was a linefeed at the bottom of the viewport immediately 
prior to those functions being called, that event might still be
pending, and only forwarded to conpty after the `ED3`. The result
then is a line pushed into the scrollback that shouldn't be there.

This PR tries to avoid that situation by forcing the renderer to
flush before the `ED3` sequence is sent.

## References

The `cls` and `Clear-Host` shims were originally added in PR #5627.

## PR Checklist
* [x] Closes #5770
* [x] Closes #13320
* [x] CLA signed.
* [ ] Tests added/passed
* [ ] Documentation updated.
* [ ] Schema 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: #xxx

## Validation Steps Performed

I've manually tested in PowerShell with `echo Hello; Clear-Host` (this
is the only way I could reliably reproduce the original problem), and in
the cmd shell with `cls`. Both cases are now working as expected.
DHowett pushed a commit that referenced this pull request Jun 30, 2022
## Summary of the Pull Request

When you execute a `cls` in the cmd shell, or `Clear-Host` in
PowerShell, we have a pair of shims that attempt to detect those
operations and forward an `ED3` sequence to conpty to clear the
scrollback.

If there was a linefeed at the bottom of the viewport immediately
prior to those functions being called, that event might still be
pending, and only forwarded to conpty after the `ED3`. The result
then is a line pushed into the scrollback that shouldn't be there.

This PR tries to avoid that situation by forcing the renderer to
flush before the `ED3` sequence is sent.

## References

The `cls` and `Clear-Host` shims were originally added in PR #5627.

## PR Checklist
* [x] Closes #5770
* [x] Closes #13320
* [x] CLA signed.
* [ ] Tests added/passed
* [ ] Documentation updated.
* [ ] Schema 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: #xxx

## Validation Steps Performed

I've manually tested in PowerShell with `echo Hello; Clear-Host` (this
is the only way I could reliably reproduce the original problem), and in
the cmd shell with `cls`. Both cases are now working as expected.

(cherry picked from commit 08c2f35)
Service-Card-Id: 83388911
Service-Version: 1.14
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. Area-VT Virtual Terminal sequence support AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Issue-Question For questions or discussion Priority-2 A description (P2) Product-Cmd.exe The issue is related to the legacy command interpreter, CMD.exe. Product-Conpty For console issues specifically related to conpty
Projects
None yet
4 participants