-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
WIP: Fix decoration leaking into newline at end of screen buffer #19019
base: master
Are you sure you want to change the base?
Conversation
This seems to be a by-design behavior in the .NET and terminal perspective. The problem is that we are writing out the newline along with the text, so the restoring of the original background color doesn't happen until after the newline gets written out. When it's at the last line in terminal, that newline triggers a buffer scroll when the background color is still It works fine in Windows PowerShell because it writes out newline in a different So, I think maybe the right fix is to check if we are at the end of terminal before writing in Host, and if so, write out the newline in a separate call without changing the foreground and background. |
@daxian-dbw Windows PowerShell doesn't use VT, so the behavior is different for the Console API. If the VT background sequence exists and you write a newline by itself, the entire line after the newline will have the background color. You can see this in zsh/bash: echo -e "\e[44m1\n2" So writing a newline in a separate call w/o reseting will result in a whole line with that decoration. |
This behavior is not related to VT decorations, and I can reproduce it with the following C# code as a console application:
If I choose to write the newline in a separate call, after restoring the background color, then it will work as expected.
But this doesn't mean this problem doesn't exist in Windows PowerShell, you can reproduce it in Windows PowerShell by running |
I see. I misunderstood what you meant previously. I'll make the change. Note that if you use Write-Output with content including VT, I need the other change to reset anyways. |
@SteveL-MSFT, I'm just thinking aloud here, still not sure what is the right fix yet. As for VT decoration, when writing out So, the question is -- is that the expected behavior? The user may intentionally omit the Reset sequence, but should be rare. |
@daxian-dbw the current fix has the same behavior as in zsh. The first here is the fix, the second zsh, the last current pwsh. |
src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs
Outdated
Show resolved
Hide resolved
|
||
if (newLine) | ||
{ | ||
// write the newline after resetting the colors so there isn't a buffer wide colored line when buffer scrolls | ||
this.WriteImpl(string.Empty, newLine: true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we reset only for newline.
I believe we should think about every output as atomic operation - set all attributes at start and reset all attributes at end because in the general case we do not know who and what will be output to the console in the next moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if you Write-Host -nonewline
, the formatandoutput will eventually emit a newline before the prompt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean some sequential write operations in script (before prompt) .
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
I'll hold off on fixing the test issues until we agree on how this should be fixed |
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Has there been any continued discussion on how this should be fixed? This is painful for enhanced prompt tools like oh-my-posh, too, when working in Powershell. |
I really dislike manipulating |
This is waiting on WG-Interactive to discuss and make a decision |
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
PR Summary
When writing content and the screen buffer scrolls, the decorations also get written to the next line. This is most evident when using a background color and the entire newline contains the background color. Fix is to insert a VT Reset sequence if the terminal supports VT.
This was validated on macOS and Windows. This shows the before and after:
write-color-fix.mp4
PR Context
This fixes part of the issue reported below, but PSCal needs to account for when the screen buffer scrolls and the cursor position has changed. Ideally, it should not be using cursor position to render, but instead pre-calculate all the rows.
Fix #18984
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).