diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index bd5780db4..ae2d77020 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -294,14 +294,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format System.Span lastColWithContent = screenRows <= OutCommandInner.StackAllocThreshold ? stackalloc int[screenRows] : new int[screenRows]; for (int row = 0; row < screenRows; row++) { - for (int col = scArray.Length - 1; col > 0; col--) + for (int col = 0; col < scArray.Length; col++) { - int colWidth = _si.columnInfo[validColumnArray[col]].width; - int headerLength = values[col].Length; - if (headerLength / colWidth >= row && headerLength % colWidth > 0) + if (scArray[col].Count > row) { lastColWithContent[row] = col; - break; } } } 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 acd2f1ef7..1ddf8694a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Table.Tests.ps1 @@ -760,4 +760,44 @@ abc abc $output = $obj | Format-Table | Out-String $output.Replace("`r","").Replace(" ",".").Replace("`n","^") | Should -BeExactly $expectedTable.Replace("`r","").Replace(" ",".").Replace("`n","^") } + + It "Should render rows correctly when wrapped: " -TestCases @( + @{ variation = "right"; obj = [pscustomobject] @{A=1;B=2;Name="This`nIs some random`nmultiline content"}; expectedTable = @" + +A B Name +- - ---- +1 2 This + Is some random + multiline content + + + +"@ }, + @{ variation = "left"; obj = [pscustomobject] @{Name="This`nIs some random`nmultiline content";A=1;B=2}; expectedTable = @" + +Name A B +---- - - +This 1 2 +Is some random +multiline content + + + +"@ }, + @{ variation = "middle"; obj = [pscustomobject] @{A=1;Name="This`nIs some random`nmultiline content";B=2}; expectedTable = @" + +A Name B +- ---- - +1 This 2 + Is some random + multiline content + + + +"@ } + ) { + param($obj, $expectedTable) + $output = $obj | Format-Table -Wrap | Out-String + $output.Replace("`r","").Replace(" ",".").Replace("`n","^") | Should -BeExactly $expectedTable.Replace("`r","").Replace(" ",".").Replace("`n","^") + } }