PowerShell ANSI 转义颜色不适用于格式表

标签 powershell visual-studio-code powershell-5.0

问题

出于某种原因,与 Format-Table 一起使用的 ANSI 转义颜色会丢失列对齐方式。有什么建议可以纠正这种行为吗?导致此问题的行是

"$e[${color}m$($dbValue)${e}[0m"

然而,如果我们不使用 ANSI Escape,该函数可以正常工作。

预期结果(移除颜色)

id    author         title                genre    price  publish_date
--    ------         -----                -----    -----  ------------
Bk001 LName, FName   XML Powershell Guide Computer 104.95 2020-10-01
Bk002 LName2, FName2 Python Guide         Computer 104.95 2020-12-16

当前结果(移除颜色)

id    author         title                         genre             price  publish_date
--    ------         -----                         -----             -----  ------------
Bk001 LName, FName   XML Powershell Guide Computer 104.95 2020-10-01  
Bk002 LName2, FName2 Python Guide         Computer 104.95 2020-12-16

示例代码

$myBooks = [xml] @"
<catalog>
   <book id="Bk001">
      <author>LName, FName</author>
      <title>XML Powershell Guide</title>
      <genre>Computer</genre>
      <price>104.95</price>
      <publish_date>2020-10-01</publish_date>
   </book>
   <book id="Bk002">
      <author>LName2, FName2</author>
      <title>Python Guide</title>
      <genre>Computer</genre>
      <price>104.95</price>
      <publish_date>2020-12-16</publish_date>      
   </book>
</catalog>
"@

# Colors
# Red   31
# Green 32
$ColorRed = '31'
$ColorGreen = '32'

function ColorValue {
    param (     
        [Parameter(Mandatory = $False)][string]$dbValue,
        [Parameter(Mandatory = $False)][string]$color
    )
    $e = [char]27

    if ($color -eq [string]::Empty) {
        $color = $ColorGreen
    }

    if ([string]::IsNullOrWhiteSpace($dbValue)) {
        $dbValue = 'NULL'
        $color = $ColorRed
    }
    "$e[${color}m$($dbValue)${e}[0m"
}

# title, genre, price, publish_date
$myBooks.catalog.book | Format-Table -AutoSize -Wrap -Property id, author, 
@{name = 'title'; expression = { (ColorValue $_.title)}}, 
@{name='genre';expression={ColorValue $_.genre $ColorRed}} , price, publish_date

PowerShell 信息

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

更新

即使升级到 PowerShell 5.1.19041.1,问题仍然存在。它实际上发生在 VS Code PowerShell 终端而不是 PowerShell 中......去看看吧。 VS Code 的任何解决方法或将其作为错误提交?

最佳答案

事实证明,问题不在代码中,而在 VS Code:PowerShell 扩展中。将其归档为错误:https://github.com/PowerShell/vscode-powershell/issues/2815

关于PowerShell ANSI 转义颜色不适用于格式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62962513/

相关文章:

powershell - 无法使用 powershell 从文件名中删除 '+' 符号

powershell - 在 Powershell 4.0 中将 PSRemotingJob 对象作为参数传递

windows - 如何在 Visual Studio Code (VSC) 中打开多个文件?

visual-studio-code - vscode 中自定义片段的键绑定(bind)?

PowerShell 帮助未找到异常 : could not find forfiles in a help file in this session

powershell - 在Powershell 5.0类中使用自定义类型

powershell - 使用 PowerShell 执行可能位于两个不同位置的文件

arrays - 为什么在创建数组时需要逗号前导?

powershell - 如何使用 Powershell 将 Set-Content 写入文件?

visual-studio-code - 无法使用 Microsoft 的 VS Code 扩展[远程 SSH] 访问特定服务器