From efd3c91b9293a707f6895ecbd020a869f8a2e609 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 6 Aug 2022 10:32:52 -0700 Subject: [PATCH] Update regex used to remove ANSI escape sequences to be more specific to decoration and hyperlinks (#17822) --- .../FormatAndOutput/common/PSStyle.cs | 2 +- .../FormatAndOutput/common/StringDecorated.cs | 8 +++++++- test/powershell/engine/Formatting/PSStyle.Tests.ps1 | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index eba904dee6..52ca35dff4 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -562,7 +562,7 @@ namespace System.Management.Automation /// /// Gets or sets the rendering mode for output. /// - public OutputRendering OutputRendering { get; set; } = OutputRendering.Ansi; + public OutputRendering OutputRendering { get; set; } = OutputRendering.Host; /// /// Gets value to turn off all attributes. diff --git a/src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs b/src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs index 03a360ff5c..9b82cf3d7a 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs @@ -101,8 +101,14 @@ namespace System.Management.Automation.Internal } } + // graphics/color mode ESC[1;2;...m + private const string GraphicsRegex = @"(\x1b\[\d+(;\d+)*m)"; + + // CSI escape sequences + private const string CsiRegex = @"(\x1b\[\?\d+[hl])"; + // replace regex with .NET 6 API once available - internal static readonly Regex AnsiRegex = new Regex(@"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", RegexOptions.Compiled); + internal static readonly Regex AnsiRegex = new Regex($"{GraphicsRegex}|{CsiRegex}", RegexOptions.Compiled); /// /// Initializes a new instance of the struct. diff --git a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 index 02ea4f5798..04c13c5e04 100644 --- a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 +++ b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 @@ -83,7 +83,7 @@ Describe 'Tests for $PSStyle automatic variable' { It '$PSStyle has correct default for OutputRendering' { $PSStyle | Should -Not -BeNullOrEmpty - $PSStyle.OutputRendering | Should -BeExactly 'Ansi' + $PSStyle.OutputRendering | Should -BeExactly 'Host' } It '$PSStyle has correct defaults for style ' -TestCases (Get-TestCases $styleDefaults) {