Skip to content

Format-Table column width calculated wrong on Mac when ANSI colors in content #2502

@latkin

Description

@latkin

Steps to reproduce

# create an object with string fields that are ANSI-colored
$esc = [char]27
function red($s)  { "$esc[31m$s$esc[0m" }
function white($s){ "$esc[37m$s$esc[0m" }
function blue($s) { "$esc[36m$s$esc[0m" }

$o = [pscustomobject]@{
  Field1 = (red "This is red")
  Field2 = (white "This is white")
  Field3 = (blue "This is blue")
}

$o | Format-Table

Expected behavior

Output is both colored and correctly aligned. This is the case on Windows 10 with both Windows Powershell 5.1 and Powershell Core 6.0 Alpha 11.

image

Actual behavior

On Mac, output is colored, but table headings are misaligned.

image

I believe the issue is that column width is being calculated based on raw string length, not considering that some characters don't contribute to the "display length".

Environment data

OSX El Capitan 10.11.6

Name                           Value                                                                                   
----                           -----                                                                                   
PSVersion                      6.0.0-alpha                                                                             
PSEdition                      Core                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
BuildVersion                   3.0.0.0                                                                                 
GitCommitId                    v6.0.0-alpha.11                                                                         
CLRVersion                                                                                                             
WSManStackVersion              3.0                                                                                     
PSRemotingProtocolVersion      2.3                                                                                     
SerializationVersion           1.1.0.1 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions