From 9fb3c2e97c739710900e1272a491bb5160c6b86b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 16 Jul 2021 03:15:16 -0700 Subject: [PATCH] Highlight using error color the exception Message and underline in PositionMessage for `Get-Error` (#15786) --- .../PowerShellCore_format_ps1xml.cs | 12 +++++++++ .../Get-Error.Tests.ps1 | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 6ce956bf26..ac0a2683df 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -810,7 +810,12 @@ namespace System.Management.Automation.Runspaces $maxDepth = 10 $ellipsis = ""`u{2026}"" $resetColor = '' + $errorColor = '' if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) { + if ($null -ne $psstyle) { + $errorColor = $psstyle.Formatting.Error + } + $resetColor = [System.Management.Automation.VTUtility]::GetEscapeSequence( [System.Management.Automation.VTUtility+VT]::Reset ) @@ -947,6 +952,13 @@ namespace System.Management.Automation.Runspaces $value = $null if ([System.Management.Automation.LanguagePrimitives]::TryConvertTo($prop.Value, [string], [ref]$value) -and $value -ne $null) { + if ($prop.Name -eq 'PositionMessage') { + $value = $value.Insert($value.IndexOf('~'), $errorColor) + } + elseif ($prop.Name -eq 'Message') { + $value = $errorColor + $value + } + $isFirstLine = $true if ($value.Contains($newline)) { # the 3 is to account for ' : ' diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 index 88c3d898cf..260f1f00ae 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1 @@ -125,4 +125,29 @@ Describe 'Get-Error tests' -Tag CI { $out | Should -BeLikeExactly "*$expectedExceptionType*" } + + It 'Get-Error uses Error color for Message and PositionMessage members' -Skip:(!$EnabledExperimentalFeatures.Contains("PSAnsiRendering")) { + $suppressVT = $false + if (Test-Path env:/__SuppressAnsiEscapeSequences) { + $suppressVT = $true + $env:__SuppressAnsiEscapeSequences = $null + } + + try { + $out = pwsh -noprofile -command '$PSStyle.OutputRendering = "ANSI"; [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("BypassOutputRedirectionCheck", $true); try { 1/0 } catch { }; Get-Error' | Out-String + + # need to escape the open square bracket so the regex works + $resetColor = $PSStyle.Reset.Replace('[','\[') + $errorColor = $PSStyle.Formatting.Error.Replace('[','\[') + $out | Should -Match "Message +: ${resetColor}${errorColor}[A-z]+" + # match the position message underline + $out | Should -Match ".*?${errorColor}~~~" + } + finally + { + if ($suppressVT) { + $env:__SuppressAnsiEscapeSequences = 1 + } + } + } }