mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Highlight using error color the exception Message and underline in PositionMessage for Get-Error (#15786)
This commit is contained in:
+12
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user