Skip to content

ANSI Escape colors for Format-Table issue #2815

@AhsenBaig

Description

@AhsenBaig

Issue Description

PowerShell output in VS Code is not rendering ANSI Escape colors properly for Format-Table

Issue

From StackOverflow: https://stackoverflow.com/q/62962513/601197

For some reason the ANSI Escape colors in use with Format-Table loses the column alignment. Any suggestions to correct this behavior? The line that is causing this issue is

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

Yet, if we don't use ANSI Escape the function works fine.

Expected results (colors removed)

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

Current results (colors removed)

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

Sample code

$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

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 10.0.19041
VSCode 1.47.2
PowerShell Extension Version 2020.6.0

PowerShell Information

Name Value
PSVersion 5.1.19041.1
PSEdition Desktop
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.19041.1
BuildVersion 10.0.19041.1
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand)
Extension Author Version
better-comments aaron-bond 2.1.0
code-spell-checker streetsidesoftware 1.9.0
cpptools ms-vscode 0.29.0
csharp ms-dotnettools 1.22.1
debugger-for-chrome msjsdiag 4.12.9
debugger-for-edge msjsdiag 1.0.15
docs-yaml docsmsft 0.2.5
ecdc mitchdenny 1.3.0
gc-excelviewer GrapeCity 3.0.38
gitlens eamodio 10.2.2
mssql ms-mssql 1.9.0
powershell ms-vscode 2020.6.0
python ms-python 2020.7.94776
remote-containers ms-vscode-remote 0.128.0
remote-ssh ms-vscode-remote 0.51.0
remote-ssh-edit ms-vscode-remote 0.51.0
remote-wsl ms-vscode-remote 0.44.4
team ms-vsts 1.161.0
todo-tree Gruntfuggly 0.0.178
vscode-docker ms-azuretools 1.3.1
vscode-firefox-debug firefox-devtools 2.9.0
vscode-icons vscode-icons-team 10.2.0
vscode-java-debug vscjava 0.27.0
vscode-markdownlint DavidAnson 0.36.1
vscode-remote-extensionpack ms-vscode-remote 0.20.0
vscode-yaml redhat 0.9.1
vscodeintellicode VisualStudioExptTeam 1.2.9
xml DotJoshJohnson 2.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions