From 75035e98d5399ae9324c1bc23e59eb765ee0f3b3 Mon Sep 17 00:00:00 2001 From: stknohg Date: Fri, 23 Mar 2018 17:35:58 +0900 Subject: [PATCH 01/12] Add checking if an output width is spcified. --- .../common/BaseOutputtingCommand.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index 5ef046b61a0..c8063e4f495 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -1132,10 +1132,23 @@ internal override void Initialize() // get the header info and the view hint WideFormattingHint hint = this.InnerCommand.RetrieveFormattingHint() as WideFormattingHint; + int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber; + if (columnsOnTheScreen == int.MaxValue) + { + try + { + columnsOnTheScreen = Console.WindowWidth; + } + catch + { + columnsOnTheScreen = 120; + } + } + // give a preference to the hint, if there if (hint != null && hint.maxWidth > 0) { - itemsPerRow = TableWriter.ComputeWideViewBestItemsPerRowFit(hint.maxWidth, this.InnerCommand._lo.ColumnNumber); + itemsPerRow = TableWriter.ComputeWideViewBestItemsPerRowFit(hint.maxWidth, columnsOnTheScreen); } else if (this.CurrentWideHeaderInfo.columns > 0) { @@ -1155,7 +1168,7 @@ internal override void Initialize() alignment[k] = TextAlignment.Left; } - this.Writer.Initialize(0, this.InnerCommand._lo.ColumnNumber, columnWidths, alignment, false); + this.Writer.Initialize(0, columnsOnTheScreen, columnWidths, alignment, false); } /// From edafe88506de7cc0a417e9fffb05ebc32093aaf1 Mon Sep 17 00:00:00 2001 From: stknohg Date: Fri, 23 Mar 2018 19:06:41 +0900 Subject: [PATCH 02/12] add test. --- .../Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 index 8b805c08870..7b43cc3b903 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 @@ -15,6 +15,8 @@ Describe "Format-Wide" -Tags "CI" { It "Should be able to use the autosize switch" { { Get-ChildItem | Format-Wide -Autosize } | Should -Not -Throw + # Test for #6471 + { Get-ChildItem | Format-Wide -Autosize | Out-String } | Should -Not -Throw } It "Should be able to take inputobject instead of pipe" { From fdb5ed5c17bd06c8c646e716f22dac488cd0929f Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 10:50:44 +0900 Subject: [PATCH 03/12] add TryGetColumnsOnTheScreen method. --- .../common/BaseOutputtingCommand.cs | 52 ++++++++----------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index c8063e4f495..c48d1e37548 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -743,6 +743,27 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List /// base class for all the formatting hints /// @@ -910,23 +931,7 @@ internal override void Initialize() columnWidthsHint = tableHint.columnWidths; } - int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber; - // Tables need to use spaces for padding to maintain table look even if console window is resized. - // For all other output, we use int.MaxValue if the user didn't explicitly specify a width. - // If we detect that int.MaxValue is used, first we try to get the current console window width. - // However, if we can't read that (for example, implicit remoting has no console window), we default - // to something reasonable: 120 columns. - if (columnsOnTheScreen == int.MaxValue) - { - try - { - columnsOnTheScreen = Console.WindowWidth; - } - catch - { - columnsOnTheScreen = 120; - } - } + int columnsOnTheScreen = TryGetColumnsOnTheScreen(this.InnerCommand._lo.ColumnNumber); int columns = this.CurrentTableHeaderInfo.tableColumnInfoList.Count; if (columns == 0) @@ -1132,18 +1137,7 @@ internal override void Initialize() // get the header info and the view hint WideFormattingHint hint = this.InnerCommand.RetrieveFormattingHint() as WideFormattingHint; - int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber; - if (columnsOnTheScreen == int.MaxValue) - { - try - { - columnsOnTheScreen = Console.WindowWidth; - } - catch - { - columnsOnTheScreen = 120; - } - } + int columnsOnTheScreen = TryGetColumnsOnTheScreen(this.InnerCommand._lo.ColumnNumber); // give a preference to the hint, if there if (hint != null && hint.maxWidth > 0) From e16ee242b3723fe42af92e0fda1ce6e2a5a2fdd5 Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 10:54:57 +0900 Subject: [PATCH 04/12] fix test comment. --- .../Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 index 7b43cc3b903..83cf41ca55a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 @@ -15,7 +15,8 @@ Describe "Format-Wide" -Tags "CI" { It "Should be able to use the autosize switch" { { Get-ChildItem | Format-Wide -Autosize } | Should -Not -Throw - # Test for #6471 + # 'Format-Wide -AutoSize | Out-String' fails on PowerShell Core 6.0.1. (issue #6471) + # so we add a new test for that. { Get-ChildItem | Format-Wide -Autosize | Out-String } | Should -Not -Throw } From 19caaf980cfad1af65a44105ac9bdf3a3e34301b Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 11:40:52 +0900 Subject: [PATCH 05/12] change to use $TestDrive. --- .../Format-Wide.Tests.ps1 | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 index 83cf41ca55a..a75f499d1c2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 @@ -1,35 +1,39 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. Describe "Format-Wide" -Tags "CI" { + BeforeAll { + 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\Testdir{0:00}" -f $_) -ItemType Directory } + 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\TestFile{0:00}.txt" -f $_) -ItemType File } + } It "Should have the same output between the alias and the unaliased function" { - $nonaliased = Get-ChildItem | Format-Wide - $aliased = Get-ChildItem | fw + $nonaliased = Get-ChildItem $TestDrive | Format-Wide + $aliased = Get-ChildItem $TestDrive | fw $($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should -Be 0 } It "Should be able to specify the columns in output using the column switch" { - { Get-ChildItem | Format-Wide -Column 3 } | Should -Not -Throw + { Get-ChildItem $TestDrive | Format-Wide -Column 3 } | Should -Not -Throw } It "Should be able to use the autosize switch" { - { Get-ChildItem | Format-Wide -Autosize } | Should -Not -Throw + { Get-ChildItem $TestDrive | Format-Wide -Autosize } | Should -Not -Throw # 'Format-Wide -AutoSize | Out-String' fails on PowerShell Core 6.0.1. (issue #6471) # so we add a new test for that. - { Get-ChildItem | Format-Wide -Autosize | Out-String } | Should -Not -Throw + { Get-ChildItem $TestDrive | Format-Wide -Autosize | Out-String } | Should -Not -Throw } It "Should be able to take inputobject instead of pipe" { - { Format-Wide -InputObject $(Get-ChildItem) } | Should -Not -Throw + { Format-Wide -InputObject $(Get-ChildItem $TestDrive) } | Should -Not -Throw } It "Should be able to use the property switch" { - { Format-Wide -InputObject $(Get-ChildItem) -Property Mode } | Should -Not -Throw + { Format-Wide -InputObject $(Get-ChildItem $TestDrive) -Property Mode } | Should -Not -Throw } It "Should throw an error when property switch and view switch are used together" { - { Format-Wide -InputObject $(Get-ChildItem) -Property CreationTime -View aoeu } | + { Format-Wide -InputObject $(Get-ChildItem $TestDrive) -Property CreationTime -View aoeu } | Should -Throw -ErrorId "FormatCannotSpecifyViewAndProperty,Microsoft.PowerShell.Commands.FormatWideCommand" } @@ -39,7 +43,7 @@ Describe "Format-Wide" -Tags "CI" { } Describe "Format-Wide DRT basic functionality" -Tags "CI" { - It "Format-Wide with array should work" { + It "Format-Wide with array should work" { $al = (0..255) $info = @{} $info.array = $al @@ -79,7 +83,7 @@ Describe "Format-Wide DRT basic functionality" -Tags "CI" { $result | Should -Match "Line2" } - It "Format-Wide with complex object for End-To-End should work" { + It "Format-Wide with complex object for End-To-End should work" { Add-Type -TypeDefinition "public enum MyDayOfWeek{Sun,Mon,Tue,Wed,Thu,Fri,Sat}" $eto = New-Object MyDayOfWeek $info = @{} From 9321971c8bc055452127774c12e752f683b363bd Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 11:47:11 +0900 Subject: [PATCH 06/12] update code formatting(tab to space etc.). --- .../Format-Wide.Tests.ps1 | 154 +++++++++--------- 1 file changed, 77 insertions(+), 77 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 index a75f499d1c2..03038f7383a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 @@ -1,14 +1,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. Describe "Format-Wide" -Tags "CI" { - BeforeAll { - 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\Testdir{0:00}" -f $_) -ItemType Directory } - 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\TestFile{0:00}.txt" -f $_) -ItemType File } - } + BeforeAll { + 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\Testdir{0:00}" -f $_) -ItemType Directory } + 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\TestFile{0:00}.txt" -f $_) -ItemType File } + } It "Should have the same output between the alias and the unaliased function" { $nonaliased = Get-ChildItem $TestDrive | Format-Wide - $aliased = Get-ChildItem $TestDrive | fw + $aliased = Get-ChildItem $TestDrive | fw $($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should -Be 0 } @@ -19,8 +19,8 @@ Describe "Format-Wide" -Tags "CI" { It "Should be able to use the autosize switch" { { Get-ChildItem $TestDrive | Format-Wide -Autosize } | Should -Not -Throw - # 'Format-Wide -AutoSize | Out-String' fails on PowerShell Core 6.0.1. (issue #6471) - # so we add a new test for that. + # 'Format-Wide -AutoSize | Out-String' fails on PowerShell Core 6.0.1. (issue #6471) + # so we add a new test for that. { Get-ChildItem $TestDrive | Format-Wide -Autosize | Out-String } | Should -Not -Throw } @@ -33,8 +33,8 @@ Describe "Format-Wide" -Tags "CI" { } It "Should throw an error when property switch and view switch are used together" { - { Format-Wide -InputObject $(Get-ChildItem $TestDrive) -Property CreationTime -View aoeu } | - Should -Throw -ErrorId "FormatCannotSpecifyViewAndProperty,Microsoft.PowerShell.Commands.FormatWideCommand" + { Format-Wide -InputObject $(Get-ChildItem $TestDrive) -Property CreationTime -View aoeu } | + Should -Throw -ErrorId "FormatCannotSpecifyViewAndProperty,Microsoft.PowerShell.Commands.FormatWideCommand" } It "Should throw and suggest proper input when view is used with invalid input without the property switch" { @@ -43,74 +43,74 @@ Describe "Format-Wide" -Tags "CI" { } Describe "Format-Wide DRT basic functionality" -Tags "CI" { - It "Format-Wide with array should work" { - $al = (0..255) - $info = @{} - $info.array = $al - $result = $info | Format-Wide | Out-String - $result | Should -Match "array" - } - - It "Format-Wide with No Objects for End-To-End should work"{ - $p = @{} - $result = $p | Format-Wide | Out-String - $result | Should -BeNullOrEmpty - } - - It "Format-Wide with Null Objects for End-To-End should work"{ - $p = $null - $result = $p | Format-Wide | Out-String - $result | Should -BeNullOrEmpty - } - - It "Format-Wide with single line string for End-To-End should work"{ - $p = "single line string" - $result = $p | Format-Wide | Out-String - $result | Should -Match $p - } - - It "Format-Wide with multiple line string for End-To-End should work"{ - $p = "Line1\nLine2" - $result = $p | Format-Wide | Out-String - $result | Should -Match "Line1" - $result | Should -Match "Line2" - } - - It "Format-Wide with string sequence for End-To-End should work"{ - $p = "Line1","Line2" - $result = $p |Format-Wide | Out-String - $result | Should -Match "Line1" - $result | Should -Match "Line2" - } + It "Format-Wide with array should work" { + $al = (0..255) + $info = @{} + $info.array = $al + $result = $info | Format-Wide | Out-String + $result | Should -Match "array" + } + + It "Format-Wide with No Objects for End-To-End should work" { + $p = @{} + $result = $p | Format-Wide | Out-String + $result | Should -BeNullOrEmpty + } + + It "Format-Wide with Null Objects for End-To-End should work" { + $p = $null + $result = $p | Format-Wide | Out-String + $result | Should -BeNullOrEmpty + } + + It "Format-Wide with single line string for End-To-End should work" { + $p = "single line string" + $result = $p | Format-Wide | Out-String + $result | Should -Match $p + } + + It "Format-Wide with multiple line string for End-To-End should work" { + $p = "Line1\nLine2" + $result = $p | Format-Wide | Out-String + $result | Should -Match "Line1" + $result | Should -Match "Line2" + } + + It "Format-Wide with string sequence for End-To-End should work" { + $p = "Line1", "Line2" + $result = $p |Format-Wide | Out-String + $result | Should -Match "Line1" + $result | Should -Match "Line2" + } It "Format-Wide with complex object for End-To-End should work" { - Add-Type -TypeDefinition "public enum MyDayOfWeek{Sun,Mon,Tue,Wed,Thu,Fri,Sat}" - $eto = New-Object MyDayOfWeek - $info = @{} - $info.intArray = 1,2,3,4 - $info.arrayList = "string1","string2" - $info.enumerable = [MyDayOfWeek]$eto - $info.enumerableTestObject = $eto - $result = $info|Format-Wide|Out-String - $result | Should -Match "intArray" - $result | Should -Match "arrayList" - $result | Should -Match "enumerable" - $result | Should -Match "enumerableTestObject" - } - - It "Format-Wide with multiple same class object with grouping should work"{ - Add-Type -TypeDefinition "public class TestGroupingClass{public TestGroupingClass(string name,int length){Name = name;Length = length;}public string Name;public int Length;public string GroupingKey;}" - $testobject1 = [TestGroupingClass]::New('name1',1) - $testobject1.GroupingKey = "foo" - $testobject2 = [TestGroupingClass]::New('name2',2) - $testobject1.GroupingKey = "bar" - $testobject3 = [TestGroupingClass]::New('name3',3) - $testobject1.GroupingKey = "bar" - $testobjects = @($testobject1,$testobject2,$testobject3) - $result = $testobjects|Format-Wide -GroupBy GroupingKey|Out-String - $result | Should -Match "GroupingKey: bar" - $result | Should -Match "name1" - $result | Should -Match " GroupingKey:" - $result | Should -Match "name2\s+name3" - } + Add-Type -TypeDefinition "public enum MyDayOfWeek{Sun,Mon,Tue,Wed,Thu,Fri,Sat}" + $eto = New-Object MyDayOfWeek + $info = @{} + $info.intArray = 1, 2, 3, 4 + $info.arrayList = "string1", "string2" + $info.enumerable = [MyDayOfWeek]$eto + $info.enumerableTestObject = $eto + $result = $info|Format-Wide|Out-String + $result | Should -Match "intArray" + $result | Should -Match "arrayList" + $result | Should -Match "enumerable" + $result | Should -Match "enumerableTestObject" + } + + It "Format-Wide with multiple same class object with grouping should work" { + Add-Type -TypeDefinition "public class TestGroupingClass{public TestGroupingClass(string name,int length){Name = name;Length = length;}public string Name;public int Length;public string GroupingKey;}" + $testobject1 = [TestGroupingClass]::New('name1', 1) + $testobject1.GroupingKey = "foo" + $testobject2 = [TestGroupingClass]::New('name2', 2) + $testobject1.GroupingKey = "bar" + $testobject3 = [TestGroupingClass]::New('name3', 3) + $testobject1.GroupingKey = "bar" + $testobjects = @($testobject1, $testobject2, $testobject3) + $result = $testobjects|Format-Wide -GroupBy GroupingKey|Out-String + $result | Should -Match "GroupingKey: bar" + $result | Should -Match "name1" + $result | Should -Match " GroupingKey:" + $result | Should -Match "name2\s+name3" + } } From 5005e5e303ab5857222bf23d8c7bcdef1df7b3df Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 14:35:27 +0900 Subject: [PATCH 07/12] rename TryGetColumnsOnTheScreen to GetConsoleWindowWidth. --- .../common/BaseOutputtingCommand.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index c48d1e37548..4b3b08bb26b 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -743,15 +743,15 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List 0) From 7905a31bd46e47123fd20970fd3517a93f6d5a17 Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 14:47:25 +0900 Subject: [PATCH 08/12] Fix review point. * Change the count of files and directories used for testing from 10 to 2. * Change to call 'Get-ChildItem' only once * Remove unnecessary comment. --- .../Format-Wide.Tests.ps1 | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 index 03038f7383a..4d2d19c060d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 @@ -2,38 +2,37 @@ # Licensed under the MIT License. Describe "Format-Wide" -Tags "CI" { BeforeAll { - 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\Testdir{0:00}" -f $_) -ItemType Directory } - 1..10 | ForEach-Object { New-Item -Path ("TestDrive:\TestFile{0:00}.txt" -f $_) -ItemType File } + 1..2 | ForEach-Object { New-Item -Path ("TestDrive:\Testdir{0:00}" -f $_) -ItemType Directory } + 1..2 | ForEach-Object { New-Item -Path ("TestDrive:\TestFile{0:00}.txt" -f $_) -ItemType File } + $pathList = Get-ChildItem $TestDrive } It "Should have the same output between the alias and the unaliased function" { - $nonaliased = Get-ChildItem $TestDrive | Format-Wide - $aliased = Get-ChildItem $TestDrive | fw + $nonaliased = $pathList | Format-Wide + $aliased = $pathList | fw $($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should -Be 0 } It "Should be able to specify the columns in output using the column switch" { - { Get-ChildItem $TestDrive | Format-Wide -Column 3 } | Should -Not -Throw + { $pathList | Format-Wide -Column 3 } | Should -Not -Throw } It "Should be able to use the autosize switch" { - { Get-ChildItem $TestDrive | Format-Wide -Autosize } | Should -Not -Throw - # 'Format-Wide -AutoSize | Out-String' fails on PowerShell Core 6.0.1. (issue #6471) - # so we add a new test for that. - { Get-ChildItem $TestDrive | Format-Wide -Autosize | Out-String } | Should -Not -Throw + { $pathList | Format-Wide -Autosize } | Should -Not -Throw + { $pathList | Format-Wide -Autosize | Out-String } | Should -Not -Throw } It "Should be able to take inputobject instead of pipe" { - { Format-Wide -InputObject $(Get-ChildItem $TestDrive) } | Should -Not -Throw + { Format-Wide -InputObject $pathList } | Should -Not -Throw } It "Should be able to use the property switch" { - { Format-Wide -InputObject $(Get-ChildItem $TestDrive) -Property Mode } | Should -Not -Throw + { Format-Wide -InputObject $pathList -Property Mode } | Should -Not -Throw } It "Should throw an error when property switch and view switch are used together" { - { Format-Wide -InputObject $(Get-ChildItem $TestDrive) -Property CreationTime -View aoeu } | + { Format-Wide -InputObject $pathList -Property CreationTime -View aoeu } | Should -Throw -ErrorId "FormatCannotSpecifyViewAndProperty,Microsoft.PowerShell.Commands.FormatWideCommand" } From 773cf267542bc18be600b633292ebca8a8879bd6 Mon Sep 17 00:00:00 2001 From: stknohg Date: Wed, 28 Mar 2018 23:34:30 +0900 Subject: [PATCH 09/12] Remove unnecessary test. --- .../Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 index 4d2d19c060d..f10f9f38649 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Wide.Tests.ps1 @@ -7,13 +7,6 @@ Describe "Format-Wide" -Tags "CI" { $pathList = Get-ChildItem $TestDrive } - It "Should have the same output between the alias and the unaliased function" { - $nonaliased = $pathList | Format-Wide - $aliased = $pathList | fw - - $($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should -Be 0 - } - It "Should be able to specify the columns in output using the column switch" { { $pathList | Format-Wide -Column 3 } | Should -Not -Throw } From 672ac5680fe3e67dc66f669fc2de9a7eecbce71e Mon Sep 17 00:00:00 2001 From: stknohg Date: Thu, 29 Mar 2018 15:42:53 +0900 Subject: [PATCH 10/12] Add '_failedToReadConsoleWidth' variable. This variable is to cache the default console width when failed to get 'Console.WindowWidth' value. --- .../FormatAndOutput/common/BaseOutputtingCommand.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index 4b3b08bb26b..cbff6b67057 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -743,6 +743,8 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List 0) + { + return _failedToReadConsoleWidth; + } try { return Console.WindowWidth; } catch { - return 120; + _failedToReadConsoleWidth = 120; + return _failedToReadConsoleWidth; } } return columnNumber; From 81c28eca95fedc67e164b51a263075d5ebd8f7dc Mon Sep 17 00:00:00 2001 From: stknohg Date: Mon, 2 Apr 2018 11:03:27 +0900 Subject: [PATCH 11/12] Reflect review results. * change _failedToReadConsoleWidth variable to _noConsole bool value. * added comments. --- .../common/BaseOutputtingCommand.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index cbff6b67057..2f38d096029 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -743,10 +743,14 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List 0) + if (_noConsole) { - return _failedToReadConsoleWidth; + return defaultConsoleWidth; } try { @@ -764,8 +768,8 @@ static private int GetConsoleWindowWidth(int columnNumber) } catch { - _failedToReadConsoleWidth = 120; - return _failedToReadConsoleWidth; + _noConsole = true; + return defaultConsoleWidth; } } return columnNumber; From 52b6d4e1d22926c5e1395c6080b221b62e4a033d Mon Sep 17 00:00:00 2001 From: stknohg Date: Mon, 2 Apr 2018 13:06:23 +0900 Subject: [PATCH 12/12] Update comments to XML Documentation style. --- .../common/BaseOutputtingCommand.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs index 2f38d096029..9399ee5fc2a 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs @@ -743,21 +743,25 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List + /// In cases like implicit remoting, there is no console so reading the console width results in an exception. + /// Instead of handling exception every time we cache this value to increase performance. + /// static private bool _noConsole = false; + /// + /// Tables and Wides need to use spaces for padding to maintain table look even if console window is resized. + /// For all other output, we use int.MaxValue if the user didn't explicitly specify a width. + /// If we detect that int.MaxValue is used, first we try to get the current console window width. + /// However, if we can't read that (for example, implicit remoting has no console window), we default + /// to something reasonable: 120 columns. + /// static private int GetConsoleWindowWidth(int columnNumber) { const int defaultConsoleWidth = 120; if (columnNumber == int.MaxValue) { - // Tables and Wides need to use spaces for padding to maintain table look even if console window is resized. - // For all other output, we use int.MaxValue if the user didn't explicitly specify a width. - // If we detect that int.MaxValue is used, first we try to get the current console window width. - // However, if we can't read that (for example, implicit remoting has no console window), we default - // to something reasonable: 120 columns. if (_noConsole) { return defaultConsoleWidth;