From 2eb7ca8309bb002d1cfaecb6da32b702fb69346c Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 21 Dec 2018 23:57:44 -0800 Subject: [PATCH] Fix formatting of header of table when center aligned (#8497) --- .../FormatAndOutput/common/TableWriter.cs | 2 +- .../Format-Table.Tests.ps1 | 33 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index 4f864eaf71..a47b18e5f4 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -386,7 +386,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format { StringCollection sc = StringManipulationHelper.GenerateLines(dc, val, _si.columnInfo[k].width, _si.columnInfo[k].width); - if (addPadding || alignment == TextAlignment.Right) + if (addPadding || alignment == TextAlignment.Right || alignment == TextAlignment.Center) { // if length is shorter, do some padding for (int col = 0; col < sc.Count; col++) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 index 264121b7c2..8a86d146f7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -476,7 +476,7 @@ er @{ view = "Default"; widths = 4,7,5; variation = "narrow values"; values = [PSCustomObject]@{First=1;Second=2;Third=3}; wrap = $false; expectedTable = @" Long*Header2*Heade -Long*********r3 +Long**********r3 Head er ----*-------*----- @@ -487,18 +487,18 @@ er @{ view = "Default"; widths = 4,7,5; variation = "narrow values with wrap"; values = [PSCustomObject]@{First=1;Second=2;Third=3}; wrap = $true; expectedTable = @" Long*Header2*Heade -Long*********r3 +Long**********r3 Head er ----*-------*----- -1**********2*3 +1**********2***3 "@ }, @{ view = "Default"; widths = 4,7,5; variation = "wide values"; values = [PSCustomObject]@{First="12345";Second="12345678";Third="123456"}; wrap = $false; expectedTable = @" Long*Header2*Heade -Long*********r3 +Long**********r3 Head er ----*-------*----- @@ -521,7 +521,7 @@ er @{ view = "Default"; widths = 4,8,6; variation = "wide values with wrap, 1st column"; values = [PSCustomObject]@{First="12345";Second="12345678";Third="123456"}; wrap = $true; expectedTable = @" Long**Header2*Header -Long**********3 +Long************3 Head er ----**-------*------ @@ -533,7 +533,7 @@ er @{ view = "Default"; widths = 5,7,6; variation = "wide values with wrap, 2nd column"; values = [PSCustomObject]@{First="12345";Second="12345678";Third="123456"}; wrap = $true; expectedTable = @" LongL*Header2*Header -ongHe*********3 +ongHe***********3 ader -----*-------*------ 12345*1234567*123456 @@ -544,23 +544,23 @@ ader @{ view = "Default"; widths = 5,8,5; variation = "wide values with wrap, 3rd column"; values = [PSCustomObject]@{First="12345";Second="12345678";Third="123456"}; wrap = $true; expectedTable = @" LongL**Header2*Heade -ongHe**********r3 +ongHe***********r3 ader -----**-------*----- 12345*12345678*12345 -***************6 +*****************6 "@ }, @{ view = "Default"; widths = 4,7,5; variation = "wide values with wrap, all 3 columns"; values = [PSCustomObject]@{First="12345";Second="12345678";Third="123456"}; wrap = $true; expectedTable = @" Long*Header2*Heade -Long*********r3 +Long**********r3 Head er ----*-------*----- 1234*1234567*12345 -5**********8*6 +5**********8***6 "@ }, @@ -799,4 +799,17 @@ A Name B $lines = $out.Split([System.Environment]::NewLine) ($lines | Select-String "Name\s*Value").Count | Should -Be ($numHeaders + 1) } + + It "Should be formatted correctly if width is declared and using center alignment" { + $expectedTable = @" + + one + --- + 1 + + +"@ + $output = [pscustomobject] @{ one = 1 } | Format-Table @{ l='one'; e='one'; width=10; alignment='center' } | Out-String + $output.Replace("`r","").Replace(" ",".").Replace("`n","^") | Should -BeExactly $expectedTable.Replace("`r","").Replace(" ",".").Replace("`n","^") + } }