Skip to content

Commit

Permalink
Fix the list view to not leak VT decorations (#17262)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw committed May 10, 2022
1 parent 1884693 commit 44098e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Text;

namespace Microsoft.PowerShell.Commands.Internal.Format
{
Expand All @@ -30,6 +31,11 @@ internal class ListWriter
/// </summary>
private int _columnWidth = 0;

/// <summary>
/// A cached string builder used within this type to reduce creation of temporary strings.
/// </summary>
private readonly StringBuilder _cachedBuilder = new();

/// <summary>
/// </summary>
/// <param name="propertyNames">Names of the properties to display.</param>
Expand Down Expand Up @@ -207,7 +213,9 @@ private void WriteProperty(int k, string propertyValue, LineOutput lo)
private void WriteSingleLineHelper(string prependString, string line, LineOutput lo)
{
if (line == null)
{
line = string.Empty;
}

// compute the width of the field for the value string (in screen cells)
int fieldCellCount = _columnWidth - _propertyLabelsDisplayLength;
Expand All @@ -221,14 +229,30 @@ private void WriteSingleLineHelper(string prependString, string line, LineOutput
// display the string collection
for (int k = 0; k < sc.Count; k++)
{
string str = sc[k];
_cachedBuilder.Clear();

if (k == 0)
{
lo.WriteLine(PSStyle.Instance.Formatting.FormatAccent + prependString + PSStyle.Instance.Reset + sc[k]);
_cachedBuilder
.Append(PSStyle.Instance.Formatting.FormatAccent)
.Append(prependString)
.Append(PSStyle.Instance.Reset)
.Append(str);
}
else
{
lo.WriteLine(padding + PSStyle.Instance.Formatting.FormatAccent + PSStyle.Instance.Reset + sc[k]);
_cachedBuilder
.Append(padding)
.Append(str);
}

if (str.Contains(ValueStringDecorated.ESC) && !str.EndsWith(PSStyle.Instance.Reset))
{
_cachedBuilder.Append(PSStyle.Instance.Reset);
}

lo.WriteLine(_cachedBuilder.ToString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,17 @@ Describe 'Format-List color tests' {
$out[0] | Should -BeExactly "$($PSStyle.Formatting.FormatAccent)Short : $($PSStyle.Reset)1" -Because ($out[0] | Format-Hex)
$out[1] | Should -BeExactly "$($PSStyle.Formatting.FormatAccent)LongLabelN : $($PSStyle.Reset)2"
}

It 'VT decorations in a property value should not be leaked in list view' {
$expected = @"
`e[32;1ma : `e[0mHello
`e[32;1mb : `e[0m`e[36mworld`e[0m
"@
## Format-List should append the 'reset' escape sequence to the value of 'b' property.
$obj = [pscustomobject]@{ a = "Hello"; b = $PSStyle.Foreground.Cyan + "world"; }
$obj | Format-List | Out-File "$TestDrive/outfile.txt"

$output = Get-Content "$TestDrive/outfile.txt" -Raw
$output.Trim().Replace("`r", "") | Should -BeExactly $expected.Replace("`r", "")
}
}

0 comments on commit 44098e7

Please sign in to comment.