简体   繁体   English

当 window 在 powershell 中调整大小时,带有背景颜色的 Write-Host 用背景颜色填充整行

[英]Write-Host with background colour fills the entire line with background colour when window is resized in powershell

eg, if you execute this command in a powershell session例如,如果您在 powershell session 中执行此命令

write-host 'hello' -ForegroundColor 'red' -BackgroundColor 'cyan'

resizing the window causes the background colour to fill the entire line:调整 window 的大小会导致背景颜色填充整行:

在此处输入图像描述

I have tested this in Windows Terminal, Windows Console and Fluent Terminal and they all exhibit this behaviour.我已经在 Windows 终端、Windows 控制台和 Fluent 终端中对此进行了测试,它们都表现出这种行为。 (I am guessing there could be a virtual terminal sequence/ANSI code to fix this issue, but I have no idea what this could be). (我猜可能有一个虚拟终端序列/ANSI 代码来解决这个问题,但我不知道这可能是什么)。

So, how can we prevent this from happening?那么,我们怎样才能防止这种情况发生呢?

λ $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.1
PSEdition                      Core
GitCommitId                    7.1.1
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

EDIT: As a test I tried the following:编辑:作为测试,我尝试了以下方法:

write-host 'hello' -ForegroundColor 'red' -BackgroundColor 'cyan' -NoNewline; write-host 'world' -BackgroundColor black

which of course displays 'HelloWorld', with 'World appearing without a background colour or more accurately the same as the default (black).这当然显示'HelloWorld','World 出现没有背景颜色或更准确地说与默认颜色相同(黑色)。

If we take this another step forward and print an extra space after with the background colour set to black, then this does not achieve the desired response, IE we're back to seeing the background colour cyan filling the entire line after a resize occurs.如果我们再向前迈出一步并在背景颜色设置为黑色之后打印一个额外的空间,那么这不会达到预期的响应,即在调整大小发生后,我们又会看到背景颜色为青色填充整行。

So we need some kind of non visible character that blocks the overspill of the background colour or an entirely different approach of which I'm not currently aware.所以我们需要某种不可见的字符来阻止背景颜色的溢出或我目前不知道的完全不同的方法。

This is confirmed bug in conhost from 2017. So, everything that relies on conhost will have this bug.这是从 2017 年开始在conhost确认的错误。因此,所有依赖于conhost的东西都会有这个错误。

This is a bug in windows console, not in powershell.这是 windows 控制台中的错误,而不是 powershell 中的错误。 It is buggy outside powershell (try this , ex).它在 powershell 外面有问题(试试这个,例如)。

Console fills line with color of last character that is not space (0x20), nor tab (0x09), nor newline.控制台用不是空格 (0x20)、制表符 (0x09) 和换行符的最后一个字符的颜色填充行。

You can try one of solutions:您可以尝试以下解决方案之一:

1 - Use Non-Breaking space character space at the end of line. 1 - 在行尾使用不间断空格字符空间。 This is the only space one-byte character that is space, and that is not ignored by console, and that is not line break.这是唯一的空格单字节字符,它是空格,并且不会被控制台忽略,也不是换行符。

write-host 'hello' -f 'red' -b 'cyan' -n;   write-host ([char]0xA0)

2 - Use any character with color matching console colors. 2 - 使用颜色匹配控制台 colors 的任何字符。 This is worse as it is copied to clipboard, to output, etc.这更糟糕,因为它被复制到剪贴板,output 等。

$hostBgColor = $Host.UI.RawUI.BackgroundColor
write-host 'hello' -f 'red' -b 'cyan' -n;   write-host 'X' -f $hostBgColor -b $hostBgColor

3 - Use other unicode multi-byte whitespace characters , but they can look different depending on many factors, including fonts and unicode support int console\output file. 3 - 使用其他 unicode 多字节空白字符,但根据许多因素,它们看起来会有所不同,包括 fonts 和 unicode 支持 int 控制台\输出文件。

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

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