From a7a49290a82c75c2b7eb5ce5b809d6c675a87d0a Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 30 Sep 2022 15:47:51 -0700 Subject: [PATCH] Remove the extra new line form List formatting (#18185) --- .../FormatAndOutput/common/BaseOutputtingCommand.cs | 13 ++++++++++--- .../Format-List.Tests.ps1 | 4 +--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index 3ab591a7af..c0c932142d 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -456,9 +456,16 @@ namespace Microsoft.PowerShell.Commands.Internal.Format /// Current context, with Fs in it. private void ProcessFormatEnd(FormatEndData fe, FormatMessagesContextManager.OutputContext c) { - // Console.WriteLine("ProcessFormatEnd"); - // we just add an empty line to the display - this.LineOutput.WriteLine(string.Empty); + if (c is FormatOutputContext foContext + && foContext.Data.shapeInfo is ListViewHeaderInfo) + { + // Skip writing out a new line for List view, because we already wrote out + // an extra new line after displaying the last list entry. + return; + } + + // We just add an empty line to the display. + LineOutput.WriteLine(string.Empty); } /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-List.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-List.Tests.ps1 index 675d1fd510..bab0110a9a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-List.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-List.Tests.ps1 @@ -37,7 +37,7 @@ Describe "Format-List" -Tags "CI" { } It "Should produce the expected output" { - $expected = "${nl}testName : testValue${nl}${nl}${nl}" + $expected = "${nl}testName : testValue${nl}${nl}" $in = New-Object PSObject Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue @@ -201,7 +201,6 @@ dbda : KM 消息 : 千 - "@ $expected = $expected -replace "`r`n", "`n" @@ -224,7 +223,6 @@ float : 9876.543 decimal : 4567.123456789 - "@ $actual = $o | Format-List | Out-String