简体   繁体   中英

PowerShell write-host background color continuing past the code

在此处输入图像描述

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

在此处输入图像描述

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM