Fix formatting to act appropriately when the style of table header or list label is empty string (#17463)

This commit is contained in:
Dongbo Wang
2022-06-02 09:20:32 -07:00
committed by GitHub
parent 5b6407741c
commit 12de67ca5d
4 changed files with 60 additions and 10 deletions
@@ -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());
@@ -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)
{
@@ -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
{
@@ -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: <member>.<submember>' -TestCases @(
@{ Submember = 'Reset' }
@{ Submember = 'BlinkOff' }