diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index 8f8d6c29d55..3a641edc429 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -572,7 +572,7 @@ public FileInfoFormatting() /// /// 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 03a360ff5c1..9b82cf3d7a7 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs @@ -101,8 +101,14 @@ private string PlainText } } + // 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 02ea4f57981..04c13c5e04e 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) {