Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix list view to not leak VT decorations #17262

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
daxian-dbw marked this conversation as resolved.
Show resolved Hide resolved
{
_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", "")
}
}