简体   繁体   English

PowerShell 写主机背景颜色继续过去的代码

[英]PowerShell write-host background color continuing past the code

在此处输入图像描述

this is from VS code, also happens in PowerShell 7.3 in Windows Terminal这是来自 VS 代码,也发生在 Windows 终端中的 PowerShell 7.3

在此处输入图像描述

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.这只是为了测试,但是当write-host在一个大脚本中并且背景 colors 只是继续拖到文本之外时,它会更频繁地发生。 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. ps 正确的词似乎是流血,所以 PowerShell write-host颜色流血正在发生。

This is a known bug related to scrolling.这是一个与滚动相关的已知错误 When the output of Write-Host involves scrolling, the background color leaks into the command-line: Write-Host的output涉及到滚动时,背景色漏进命令行:

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:作为一种解决方法,您可以使用ANSI 转义序列进行着色,从 PS 7.2+ 开始,使用自动$PSStyle变量非常容易:

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. $($PSStyle.Reset)显式重置行尾的样式,这应该避免任何背景颜色泄漏到命令行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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