2

enter image description here

this is from VS code, also happens in PowerShell 7.3 in Windows Terminal

enter image description here

the code is this:

write-host "text text text" -ForegroundColor Magenta -BackgroundColor white

that's just for a test, but it happens a lot more often when write-host is inside a big script and the background colors just keep dragging outside the text. how can I prevent this from happening? been having this problem for a couple of months.

p.s the correct word for this seems to be bleeding, so PowerShell write-host color bleeding is happening.

  • 1
    I don't know the answer but as a workaround you could use ANSI escape coloring, which is pretty easy to use through the global `$PSStyle` variable: `Write-Host "$($PSStyle.Foreground.Magenta)$($PSStyle.Background.White)text text text$($PSStyle.Reset)"` -- this explicitly resets the style which should avoid any "bleeding" into the command-line. – zett42 Jan 16 '23 at 14:55
  • This doesnt happen in other editors, so I suspect it's more a vscode issue than a Powershell one. – Scepticalist Jan 16 '23 at 16:29
  • I mean I don't know, did you even see the Windows Terminal screenshot in the post? @zett42 thank you very much, yes that indeed stopped the bleeding, now no matter how fast I spam it, no bleeding occurs anywhere. want to add it as answer? –  Jan 16 '23 at 17:32

1 Answers1

3

This is a known bug related to scrolling. When the output of Write-Host involves scrolling, the background color leaks into the command-line:

1..100 | % { write-host "text text text" -ForegroundColor Magenta -BackgroundColor white }

As a workaround you could use ANSI escape sequences for coloring, which is pretty easy as of PS 7.2+ by using the automatic $PSStyle variable:

Write-Host "$($PSStyle.Foreground.Magenta)$($PSStyle.Background.White)text text text$($PSStyle.Reset)" 

The $($PSStyle.Reset) explicitly resets the style at the end of the line which should avoid any background color leaking into the command-line.

zett42
  • 25,437
  • 3
  • 35
  • 72