diff --git a/src/System.Management.Automation/FormatAndOutput/common/ListWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/ListWriter.cs index 786a25c2d7..407c0d5dca 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/ListWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/ListWriter.cs @@ -227,6 +227,10 @@ namespace Microsoft.PowerShell.Commands.Internal.Format // The padding to use in the lines after the first. string headPadding = null; + // The VT style used for the list label. + string style = PSStyle.Instance.Formatting.FormatAccent; + string reset = PSStyle.Instance.Reset; + // display the string collection for (int k = 0; k < sc.Count; k++) { @@ -235,18 +239,21 @@ namespace Microsoft.PowerShell.Commands.Internal.Format if (k == 0) { - if (string.IsNullOrWhiteSpace(prependString)) + if (string.IsNullOrWhiteSpace(prependString) || style == string.Empty) { - // Sometimes 'prependString' is just padding white spaces. - // We don't need to add formatting escape sequences in such a case. + // - Sometimes 'prependString' is just padding white spaces, and we don't + // need to add formatting escape sequences in such a case. + // - Otherwise, if the style is an empty string, then the user has chosen + // to not apply a style to the list label. _cachedBuilder.Append(prependString).Append(str); } else { + // Apply the style to the list label. _cachedBuilder - .Append(PSStyle.Instance.Formatting.FormatAccent) + .Append(style) .Append(prependString) - .Append(PSStyle.Instance.Reset) + .Append(reset) .Append(str); } } @@ -257,9 +264,9 @@ namespace Microsoft.PowerShell.Commands.Internal.Format _cachedBuilder.Append(headPadding).Append(str); } - if (str.Contains(ValueStringDecorated.ESC) && !str.EndsWith(PSStyle.Instance.Reset)) + if (str.Contains(ValueStringDecorated.ESC) && !str.EndsWith(reset)) { - _cachedBuilder.Append(PSStyle.Instance.Reset); + _cachedBuilder.Append(reset); } lo.WriteLine(_cachedBuilder.ToString()); diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index 3a641edc42..3a5dcb36f0 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -698,6 +698,11 @@ namespace System.Management.Automation private static string ValidateNoContent(string text) { + if (text is null) + { + throw new ArgumentNullException(nameof(text)); + } + var decorartedString = new ValueStringDecorated(text); if (decorartedString.ContentLength > 0) { diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index eae65766bb..eb540f4904 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -151,9 +151,12 @@ namespace Microsoft.PowerShell.Commands.Internal.Format } else if (_header != null) { + string style = PSStyle.Instance.Formatting.TableHeader; + string reset = PSStyle.Instance.Reset; + foreach (string line in _header) { - lo.WriteLine(PSStyle.Instance.Formatting.TableHeader + line + PSStyle.Instance.Reset); + lo.WriteLine(style == string.Empty ? line : style + line + reset); } return _header.Count; @@ -226,6 +229,9 @@ namespace Microsoft.PowerShell.Commands.Internal.Format } } + string style = PSStyle.Instance.Formatting.TableHeader; + string reset = PSStyle.Instance.Reset; + if (multiLine) { foreach (string line in GenerateTableRow(values, currentAlignment, lo.DisplayCells)) @@ -233,7 +239,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format generatedRows?.Add(line); if (isHeader) { - lo.WriteLine(PSStyle.Instance.Formatting.TableHeader + line + PSStyle.Instance.Reset); + lo.WriteLine(style == string.Empty ? line : style + line + reset); } else { @@ -247,7 +253,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format generatedRows?.Add(line); if (isHeader) { - lo.WriteLine(PSStyle.Instance.Formatting.TableHeader + line + PSStyle.Instance.Reset); + lo.WriteLine(style == string.Empty ? line : style + line + reset); } else { diff --git a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 index e20534223f..6855ea713c 100644 --- a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 +++ b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 @@ -151,6 +151,22 @@ Describe 'Tests for $PSStyle automatic variable' { } } + It '$PSStyle.Formatting.FormatAccent is ignored when it''s set to be an empty string' { + $old = $PSStyle.Formatting.FormatAccent + $oldRender = $PSStyle.OutputRendering + + try { + $PSStyle.OutputRendering = 'Ansi' + $PSStyle.Formatting.FormatAccent = '' + $out = $PSVersionTable | Format-List | Out-String + $out.Contains("`e[") | Should -BeFalse + } + finally { + $PSStyle.OutputRendering = $oldRender + $PSStyle.Formatting.FormatAccent = $old + } + } + It '$PSStyle.Formatting.TableHeader is applied to Format-Table' { $old = $PSStyle.Formatting.TableHeader $oldRender = $PSStyle.OutputRendering @@ -167,6 +183,22 @@ Describe 'Tests for $PSStyle automatic variable' { } } + It '$PSStyle.Formatting.TableHeader is ignored when it''s set to be an empty string' { + $old = $PSStyle.Formatting.TableHeader + $oldRender = $PSStyle.OutputRendering + + try { + $PSStyle.OutputRendering = 'Ansi' + $PSStyle.Formatting.TableHeader = '' + $out = $PSVersionTable | Format-Table | Out-String + $out.Contains("`e[") | Should -BeFalse + } + finally { + $PSStyle.OutputRendering = $oldRender + $PSStyle.Formatting.TableHeader = $old + } + } + It 'Should fail if setting formatting contains printable characters: .' -TestCases @( @{ Submember = 'Reset' } @{ Submember = 'BlinkOff' }