[PR #5627] [MERGED] Implement a pair of shims for cls, Clear-Host in conpty mode #26398

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

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/5627
Author: @zadjii-msft
Created: 4/28/2020
Status: Merged
Merged: 4/30/2020
Merged by: @undefined

Base: masterHead: dev/migrie/b/3126-cls-shim


📝 Commits (10+)

  • ff14bad This seems like it works as a test
  • 54a5bd2 Just from the test passing, I believe this will ship cmd.exe. Now time to actually play with it
  • 9583e04 Add the powershell case too
  • 841eff6 create the shim for powershell and Clear-Host as well
  • a4151c3 some doc comments I missed
  • f74d272 Make this only work for cmd.exe and powershell.exe
  • ed062ff pr comments
  • 2f61d55 Merge remote-tracking branch 'origin/master' into dev/migrie/b/3126-cls-shim
  • 2b953b4 whoops
  • 8707c18 yikes this is some real ded code

📊 Changes

24 files changed (+475 additions, -72 deletions)

View changed files

📝 src/cascadia/UnitTests_TerminalCore/ConptyRoundtripTests.cpp (+147 -37)
📝 src/host/ApiRoutines.h (+4 -2)
📝 src/host/VtIo.cpp (+22 -2)
📝 src/host/VtIo.hpp (+3 -1)
📝 src/host/_output.cpp (+29 -2)
📝 src/host/getset.cpp (+42 -3)
📝 src/host/globals.cpp (+3 -3)
📝 src/host/globals.h (+1 -1)
📝 src/host/ut_host/ConptyOutputTests.cpp (+9 -15)
📝 src/inc/til/size.h (+8 -0)
📝 src/renderer/vt/VtSequences.cpp (+5 -0)
📝 src/renderer/vt/Xterm256Engine.cpp (+14 -0)
📝 src/renderer/vt/Xterm256Engine.hpp (+2 -0)
📝 src/renderer/vt/state.cpp (+17 -0)
📝 src/renderer/vt/vtrenderer.hpp (+3 -0)
📝 src/server/ApiDispatchers.cpp (+8 -2)
src/server/ConsoleShimPolicy.cpp (+82 -0)
src/server/ConsoleShimPolicy.h (+35 -0)
📝 src/server/IApiRoutines.h (+4 -2)
📝 src/server/ProcessHandle.cpp (+10 -1)

...and 4 more files

📄 Description

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

🔄 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/5627 **Author:** [@zadjii-msft](https://github.com/zadjii-msft) **Created:** 4/28/2020 **Status:** ✅ Merged **Merged:** 4/30/2020 **Merged by:** [@undefined](undefined) **Base:** `master` ← **Head:** `dev/migrie/b/3126-cls-shim` --- ### 📝 Commits (10+) - [`ff14bad`](https://github.com/microsoft/terminal/commit/ff14bada3b58d91a680c6d22f7015147a7411230) This seems like it works as a test - [`54a5bd2`](https://github.com/microsoft/terminal/commit/54a5bd24100c57ec3f9e37c67d8e9a1e08b19756) Just from the test passing, I believe this will ship cmd.exe. Now time to actually play with it - [`9583e04`](https://github.com/microsoft/terminal/commit/9583e04605d6d4cb57d7f5df90674533ca499407) Add the powershell case too - [`841eff6`](https://github.com/microsoft/terminal/commit/841eff6281ce1353de671177407320b1d4707616) create the shim for powershell and Clear-Host as well - [`a4151c3`](https://github.com/microsoft/terminal/commit/a4151c320e879d24874d9b8a6baf23283b47dce2) some doc comments I missed - [`f74d272`](https://github.com/microsoft/terminal/commit/f74d272a8bc096e0dbf9804e8e22220855395073) Make this only work for cmd.exe and powershell.exe - [`ed062ff`](https://github.com/microsoft/terminal/commit/ed062ff46027003267e8a58904c88accd6d07c90) pr comments - [`2f61d55`](https://github.com/microsoft/terminal/commit/2f61d5565b523262722b10cfb83437e63f26c8be) Merge remote-tracking branch 'origin/master' into dev/migrie/b/3126-cls-shim - [`2b953b4`](https://github.com/microsoft/terminal/commit/2b953b40118f09f2e378020eece4ac4a563063e1) whoops - [`8707c18`](https://github.com/microsoft/terminal/commit/8707c1824cac68be5cf2ed83b69e13a3ea31a344) yikes this is some real ded code ### 📊 Changes **24 files changed** (+475 additions, -72 deletions) <details> <summary>View changed files</summary> 📝 `src/cascadia/UnitTests_TerminalCore/ConptyRoundtripTests.cpp` (+147 -37) 📝 `src/host/ApiRoutines.h` (+4 -2) 📝 `src/host/VtIo.cpp` (+22 -2) 📝 `src/host/VtIo.hpp` (+3 -1) 📝 `src/host/_output.cpp` (+29 -2) 📝 `src/host/getset.cpp` (+42 -3) 📝 `src/host/globals.cpp` (+3 -3) 📝 `src/host/globals.h` (+1 -1) 📝 `src/host/ut_host/ConptyOutputTests.cpp` (+9 -15) 📝 `src/inc/til/size.h` (+8 -0) 📝 `src/renderer/vt/VtSequences.cpp` (+5 -0) 📝 `src/renderer/vt/Xterm256Engine.cpp` (+14 -0) 📝 `src/renderer/vt/Xterm256Engine.hpp` (+2 -0) 📝 `src/renderer/vt/state.cpp` (+17 -0) 📝 `src/renderer/vt/vtrenderer.hpp` (+3 -0) 📝 `src/server/ApiDispatchers.cpp` (+8 -2) ➕ `src/server/ConsoleShimPolicy.cpp` (+82 -0) ➕ `src/server/ConsoleShimPolicy.h` (+35 -0) 📝 `src/server/IApiRoutines.h` (+4 -2) 📝 `src/server/ProcessHandle.cpp` (+10 -1) _...and 4 more files_ </details> ### 📄 Description ## 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 ⚠️_~~ * [x] 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 * [x] Closes #3126 * [x] Actually closes #1305 too, which is really the same thing, but probably deserves a callout * [x] I work here * [x] Tests added/passed * [n/a] Requires documentation to be updated ## Validation Steps Performed * ran tests * checked `cls` in the Terminal * checked `Clear-Host` in the Terminal * Checked running `powershell clear-host` from `cmd.exe` --- <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:15:49 +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#26398