Highlight using error color the exception Message and underline in PositionMessage for Get-Error (#15786)

This commit is contained in:
Steve Lee
2021-07-16 15:15:16 +05:00
committed by GitHub
parent 671af8991c
commit 9fb3c2e97c
2 changed files with 37 additions and 0 deletions
@@ -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 ' : '
@@ -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
}
}
}
}