Pla*_*fan 5 console powershell write-host
例如,如果您在 powershell 会话中执行此命令
\nwrite-host \'hello\' -ForegroundColor \'red\' -BackgroundColor \'cyan\'\n\nRun Code Online (Sandbox Code Playgroud)\n调整窗口大小会导致背景颜色填充整行:
\n\n我已经在 Windows 终端、Windows 控制台和 Fluent 终端中对此进行了测试,它们都表现出这种行为。(我猜测可能有一个虚拟终端序列/ANSI 代码来解决这个问题,但我不知道这可能是什么)。
\n那么,我们怎样才能防止这种情况发生呢?
\n\xce\xbb $PSVersionTable\n\nName Value\n---- -----\nPSVersion 7.1.1\nPSEdition Core\nGitCommitId 7.1.1\nOS Microsoft Windows 10.0.19041\nPlatform Win32NT\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0\xe2\x80\xa6}\nPSRemotingProtocolVersion 2.3\nSerializationVersion 1.1.0.1\nWSManStackVersion 3.0\nRun Code Online (Sandbox Code Playgroud)\n编辑:作为测试,我尝试了以下操作:
\nwrite-host \'hello\' -ForegroundColor \'red\' -BackgroundColor \'cyan\' -NoNewline; write-host \'world\' -BackgroundColor black\nRun Code Online (Sandbox Code Playgroud)\n当然,它会显示“HelloWorld”,而“World”则没有背景颜色,或更准确地说与默认颜色(黑色)相同。
\n如果我们再向前迈出一步,并在背景颜色设置为黑色后打印一个额外的空格,那么这不会达到预期的响应,IE 我们会在调整大小后看到背景颜色青色填充整行。
\n因此,我们需要某种不可见的字符来阻止背景颜色的溢出,或者需要一种我目前不知道的完全不同的方法。
\n这是从 2017 年开始确认的 bugconhost。所以,所有依赖的东西conhost都会有这个 bug。
这是 Windows 控制台中的错误,而不是 powershell 中的错误。它在 powershell 之外有问题(试试这个,例如)。
控制台使用最后一个字符的颜色填充行,该颜色不是空格 (0x20)、制表符 (0x09) 或换行符。
您可以尝试以下解决方案之一:
1 -在行尾使用不间断空格字符空间。这是唯一一个空格单字节字符,它是空格,不会被控制台忽略,也不是换行符。
write-host 'hello' -f 'red' -b 'cyan' -n; write-host ([char]0xA0)
Run Code Online (Sandbox Code Playgroud)
2 - 使用颜色与控制台颜色匹配的任何字符。这更糟糕,因为它被复制到剪贴板、输出等。
$hostBgColor = $Host.UI.RawUI.BackgroundColor
write-host 'hello' -f 'red' -b 'cyan' -n; write-host 'X' -f $hostBgColor -b $hostBgColor
Run Code Online (Sandbox Code Playgroud)
3 - 使用其他 unicode 多字节空白字符,但它们可能看起来不同,具体取决于许多因素,包括字体和 unicode 支持 int console\output 文件。
| 归档时间: |
|
| 查看次数: |
2076 次 |
| 最近记录: |