繁体   English   中英

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

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

在此处输入图像描述

这是来自 VS 代码,也发生在 Windows 终端中的 PowerShell 7.3

在此处输入图像描述

代码是这样的:

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

这只是为了测试,但是当write-host在一个大脚本中并且背景 colors 只是继续拖到文本之外时,它会更频繁地发生。 我怎样才能防止这种情况发生? 几个月来一直遇到这个问题。

ps 正确的词似乎是流血,所以 PowerShell write-host颜色流血正在发生。

这是一个与滚动相关的已知错误 Write-Host的output涉及到滚动时,背景色漏进命令行:

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

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

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

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

暂无
暂无

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

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