Terminal: Clear-Host does not clear the previous content (Powershell)

Created on 9 Oct 2019  路  12Comments  路  Source: microsoft/terminal

Environment

Windows build number: Microsoft Windows NT 10.0.18362.0
Windows Terminal version (if applicable): 0.5.2762.0

Steps to reproduce

# open Terminal with powershell
# open git repo with more than 100 commits
git log -100 # to fill in the entire screen with text
Clear-Host
# scroll up

Expected behavior

Cannot scroll up, Clear-Host cleared the terminal, all previous output is gone.

Actual behavior

Scrolling up shows previous output. This repros with both Powershel 6.2.3 and 5.2.1.
This does not happen with the powershell terminal, which correctly removes all previous output.

Area-Output Area-VT Issue-Bug Priority-2 Product-Conpty Resolution-Fix-Committed v1-Scrubbed

Most helpful comment

I'm pulling the triage tag and putting this in v1.0: this is a complaint we receive so often that we'd be remiss to ship a final product without giving it an investigation.

All 12 comments

I think this is somewhat related to #2715. Not necessarily a duplicate, but I suspect that fixing this issue might also fix #2715.

Same thing with WSL, where clear does not cleaning scrollback.

Just checked: this _will_ almost certainly be fixed by /dup #2715. If PowerShell is clearing the screen in another way (which it might be doing), we'll have to evaluate then. PowerShell Core, however, can always be updated to take advantage of CSI 3 J

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

We're going to need to look at this again. We're going to pass through CSI 3 J but that's not going to solve folks who are rotating the entire backing buffer with the API (or something else) to clear it out.

We probably need to consider and engage on fixing those.

I'm pulling the triage tag and putting this in v1.0: this is a complaint we receive so often that we'd be remiss to ship a final product without giving it an investigation.

It looks like both CMD and PowerShell...

  1. set the cursor to 0,0
  2. fill the entire console buffer with spaces

PowerShell further..

  1. stomps the attributes on the entire buffer with the (narrowed) buffer attributes

This _might_(?) be detectable.

Discussion from triage room: we can probably quirk it for cmd and powershell (powershell and pwsh.exe).

This issue was very common in VsCode/PowerShell. But they managed to fix it. Here's a PR: https://github.com/PowerShell/vscode-powershell/pull/2316

Maybe it can be easily ported.

No, they did a workaround that is specific to that project and VS Code. It can't even be ported to other PowerShell Editor Service Clients. They run a custom console so they changed the clear command to run a VS Code command. The regular PowerShell and CMD consoles do not clear in VS Code.

Self notes from what Dustin commented months ago during triage:

PowerShell

Powershell clears the screen with:
https://github.com/iSazonov/PowerShell/blob/8dae4915d3072cbac54a9312cf524ff7a73c977a/src/System.Management.Automation/engine/InitialSessionState.cs#L4063-L4075

```c#
if (Platform.IsWindows)
{
// use $RawUI so this works over remoting where there isn't a physical console
return @"
$RawUI = $Host.UI.RawUI
$RawUI.CursorPosition = @{X=0;Y=0}
$RawUI.SetBufferContents(
@{Top = -1; Bottom = -1; Right = -1; Left = -1},
@{Character = ' '; ForegroundColor = $rawui.ForegroundColor; BackgroundColor = $rawui.BackgroundColor})

.Link

https://go.microsoft.com/fwlink/?LinkID=225747

.ExternalHelp System.Management.Automation.dll-help.xml

";
}


Which then calls to:
https://github.com/PowerShell/PowerShell/blob/b1e998046e12ebe5da9dee479f20d479aa2256d7/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs#L957-L971

```c#
            if (region.Left == -1 && region.Right == -1 && region.Top == -1 && region.Bottom == -1)
            {
                if (bufferWidth % 2 == 1 &&
                    ConsoleControl.IsCJKOutputCodePage(out codePage) &&
                    LengthInBufferCells(fill.Character) == 2)
                {
                    throw PSTraceSource.NewArgumentException("fill");
                }

                int cells = bufferWidth * bufferHeight;

                ConsoleControl.FillConsoleOutputCharacter(handle, fill.Character, cells, origin);
                ConsoleControl.FillConsoleOutputAttribute(handle, attribute, cells, origin);
                return;
            }

CMD

CMD on the other hand calls

    ScrollConsoleScreenBuffer( handle, { 0, 0, bufferWidth, bufferHeight }, nullptr,  { 0, -bufferHeight }, {space with the current attributes});
    SetConsoleCursorPosition( handle, {0, 0} );

(more or less)

:tada:This issue was addressed in #5627, which has now been successfully released as Windows Terminal Release Candidate v0.11.1251.0 (1.0rc1).:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

waf picture waf  路  3Comments

TayYuanGeng picture TayYuanGeng  路  3Comments

Wid-Mimosa picture Wid-Mimosa  路  3Comments

mrmlnc picture mrmlnc  路  3Comments

miniksa picture miniksa  路  3Comments