Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/PowerShell/PowerShell.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAditya Patwardhan <adityap@microsoft.com>2022-09-15 00:44:22 +0300
committerGitHub <noreply@github.com>2022-09-15 00:44:22 +0300
commitf5a725cfc6d1a1e1ea33f07b5035f9e142710aa0 (patch)
tree1a71914dba7f6485ef3cdef27f29175deac3971d
parentec7bb207e93ff158ae64bf711fed37444e146c14 (diff)
Backport of 'Fix error formatting to use color defined in $PSStyle.Formatting' (#18086)
-rw-r--r--src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs185
-rw-r--r--src/System.Management.Automation/engine/InitialSessionState.cs2
-rw-r--r--test/powershell/Host/ConsoleHost.Tests.ps15
3 files changed, 73 insertions, 119 deletions
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 f4b9d03489..9b2f6596bd 100644
--- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs
+++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs
@@ -811,33 +811,18 @@ namespace System.Management.Automation.Runspaces
$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
- )
- }
+ $accentColor = ''
- function Get-VT100Color([ConsoleColor] $color) {
- if (!$Host.UI.SupportsVirtualTerminal -or !([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
- return ''
- }
-
- return [System.Management.Automation.VTUtility]::GetEscapeSequence($color)
+ if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
+ $resetColor = $PSStyle.Reset
+ $errorColor = $psstyle.Formatting.Error
+ $accentColor = $PSStyle.Formatting.FormatAccent
}
function Show-ErrorRecord($obj, [int]$indent = 0, [int]$depth = 1) {
$newline = [Environment]::Newline
$output = [System.Text.StringBuilder]::new()
$prefix = ' ' * $indent
- $accentColor = ''
-
- if ($null -ne $Host.PrivateData) {
- $accentColor = Get-VT100Color ($Host.PrivateData.FormatAccentColor ?? $Host.PrivateData.ErrorForegroundColor)
- }
$expandTypes = @(
'Microsoft.Rest.HttpRequestMessageWrapper'
@@ -1066,36 +1051,17 @@ namespace System.Management.Automation.Runspaces
Set-StrictMode -Off
$newline = [Environment]::Newline
- function Get-ConciseViewPositionMessage {
-
- $resetColor = ''
- if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
- $resetColor = [System.Management.Automation.VTUtility]::GetEscapeSequence(
- [System.Management.Automation.VTUtility+VT]::Reset
- )
- }
-
- function Get-VT100Color([ConsoleColor] $color) {
- if (!$Host.UI.SupportsVirtualTerminal -or !([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
- return ''
- }
-
- return [System.Management.Automation.VTUtility]::GetEscapeSequence($color)
- }
+ $resetColor = ''
+ $errorColor = ''
+ $accentColor = ''
- # return length of string sans VT100 codes
- function Get-RawStringLength($string) {
- $vtCodes = ""`e[0m"", ""`e[2;30m"", ""`e[2;31m"", ""`e[2;32m"", ""`e[2;33m"", ""`e[2;34m"",
- ""`e[2;35m"", ""`e[2;36m"", ""`e[2;37m"", ""`e[1;30m"", ""`e[1;31m"", ""`e[1;32m"",
- ""`e[1;33m"", ""`e[1;34m"", ""`e[1;35m"", ""`e[1;36m"", ""`e[1;37m""
-
- $newString = $string
- foreach ($vtCode in $vtCodes) {
- $newString = $newString.Replace($vtCode, '')
- }
+ if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
+ $resetColor = $PSStyle.Reset
+ $errorColor = $PSStyle.Formatting.Error
+ $accentColor = $PSStyle.Formatting.ErrorAccent
+ }
- return $newString.Length
- }
+ function Get-ConciseViewPositionMessage {
# returns a string cut to last whitespace
function Get-TruncatedString($string, [int]$length) {
@@ -1107,14 +1073,6 @@ namespace System.Management.Automation.Runspaces
return ($string.Substring(0,$length) -split '\s',-2)[0]
}
- $errorColor = ''
- $accentColor = ''
-
- if ($null -ne $Host.PrivateData) {
- $errorColor = Get-VT100Color $Host.PrivateData.ErrorForegroundColor
- $accentColor = Get-VT100Color ($Host.PrivateData.ErrorAccentColor ?? $errorColor)
- }
-
$posmsg = ''
$headerWhitespace = ''
$offsetWhitespace = ''
@@ -1217,7 +1175,7 @@ namespace System.Management.Automation.Runspaces
# if rendering line information, break up the message if it's wider than the console
if ($myinv -and $myinv.ScriptName -or $err.CategoryInfo.Category -eq 'ParserError') {
- $prefixLength = Get-RawStringLength -string $prefix
+ $prefixLength = [System.Management.Automation.Internal.StringDecorated]::new($prefix).ContentLength
$prefixVtLength = $prefix.Length - $prefixLength
# replace newlines in message so it lines up correct
@@ -1296,82 +1254,77 @@ namespace System.Management.Automation.Runspaces
$myinv = $err.InvocationInfo
}
- if ($ErrorView -ne 'ConciseView' -and $null -ne $host.PrivateData) {
- if ($null -ne $host.PrivateData.ErrorForegroundColor) {
- [Console]::ForegroundColor = $host.PrivateData.ErrorForegroundColor
- }
+ if ($err.FullyQualifiedErrorId -eq 'NativeCommandErrorMessage' -or $err.FullyQualifiedErrorId -eq 'NativeCommandError') {
+ return ""${errorColor}$($err.Exception.Message)${resetcolor}""
+ }
- if ($null -ne $host.PrivateData.ErrorBackgroundColor) {
- [Console]::BackgroundColor = $host.PrivateData.ErrorBackgroundColor
- }
+ $myinv = $err.InvocationInfo
+ if ($ErrorView -eq 'DetailedView') {
+ $message = Get-Error | Out-String
+ return ""${errorColor}${message}${resetcolor}""
}
- if ($err.FullyQualifiedErrorId -eq 'NativeCommandErrorMessage' -or $err.FullyQualifiedErrorId -eq 'NativeCommandError') {
- $err.Exception.Message
+ if ($ErrorView -eq 'CategoryView') {
+ $message = $err.CategoryInfo.GetMessage()
+ return ""${errorColor}${message}${resetcolor}""
}
- else
- {
- $myinv = $err.InvocationInfo
- if ($ErrorView -eq 'DetailedView') {
- return (Get-Error | Out-String)
- }
- elseif ($ErrorView -eq 'ConciseView') {
- $posmsg = Get-ConciseViewPositionMessage
- }
- elseif ($myinv -and ($myinv.MyCommand -or ($err.CategoryInfo.Category -ne 'ParserError'))) {
- $posmsg = $myinv.PositionMessage
- } else {
- $posmsg = ''
- }
- if ($posmsg -ne '')
- {
- $posmsg = $newline + $posmsg
- }
+ $posmsg = ''
+ if ($ErrorView -eq 'ConciseView') {
+ $posmsg = Get-ConciseViewPositionMessage
+ }
+ elseif ($myinv -and ($myinv.MyCommand -or ($err.CategoryInfo.Category -ne 'ParserError'))) {
+ $posmsg = $myinv.PositionMessage
+ }
- if ($err.PSMessageDetails) {
- $posmsg = ' : ' + $err.PSMessageDetails + $posmsg
- }
+ if ($posmsg -ne '') {
+ $posmsg = $newline + $posmsg
+ }
+
+ if ($err.PSMessageDetails) {
+ $posmsg = ' : ' + $err.PSMessageDetails + $posmsg
+ }
- if ($ErrorView -eq 'ConciseView') {
- return $posmsg
+ if ($ErrorView -eq 'ConciseView') {
+ if ($err.PSMessageDetails) {
+ $posmsg = ""${errorColor}${posmsg}""
}
+ return $posmsg
+ }
- $indent = 4
+ $indent = 4
- $errorCategoryMsg = $err.ErrorCategory_Message
+ $errorCategoryMsg = $err.ErrorCategory_Message
- if ($null -ne $errorCategoryMsg)
- {
- $indentString = '+ CategoryInfo : ' + $err.ErrorCategory_Message
- }
- else
- {
- $indentString = '+ CategoryInfo : ' + $err.CategoryInfo
- }
+ if ($null -ne $errorCategoryMsg)
+ {
+ $indentString = '+ CategoryInfo : ' + $err.ErrorCategory_Message
+ }
+ else
+ {
+ $indentString = '+ CategoryInfo : ' + $err.CategoryInfo
+ }
- $posmsg += $newline + $indentString
+ $posmsg += $newline + $indentString
- $indentString = ""+ FullyQualifiedErrorId : "" + $err.FullyQualifiedErrorId
- $posmsg += $newline + $indentString
+ $indentString = ""+ FullyQualifiedErrorId : "" + $err.FullyQualifiedErrorId
+ $posmsg += $newline + $indentString
- $originInfo = $err.OriginInfo
+ $originInfo = $err.OriginInfo
- if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName))
- {
- $indentString = ""+ PSComputerName : "" + $originInfo.PSComputerName
- $posmsg += $newline + $indentString
- }
+ if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName))
+ {
+ $indentString = ""+ PSComputerName : "" + $originInfo.PSComputerName
+ $posmsg += $newline + $indentString
+ }
- if ($ErrorView -eq 'CategoryView') {
- $err.CategoryInfo.GetMessage()
- }
- elseif (! $err.ErrorDetails -or ! $err.ErrorDetails.Message) {
- $err.Exception.Message + $posmsg
- } else {
- $err.ErrorDetails.Message + $posmsg
- }
+ $finalMsg = if ($err.ErrorDetails.Message) {
+ $err.ErrorDetails.Message + $posmsg
+ } else {
+ $err.Exception.Message + $posmsg
}
+
+ ""${errorColor}${finalMsg}${resetcolor}""
")
.EndEntry()
.EndControl());
diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs
index 072323c7a0..e75ca018f8 100644
--- a/src/System.Management.Automation/engine/InitialSessionState.cs
+++ b/src/System.Management.Automation/engine/InitialSessionState.cs
@@ -4335,7 +4335,7 @@ end {
SpecialVariables.PSStyle,
PSStyle.Instance,
RunspaceInit.PSStyleDescription,
- ScopedItemOptions.None),
+ ScopedItemOptions.Constant),
// Variable which controls the encoding for piping data to a NativeCommand
new SessionStateVariableEntry(
diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1
index ff2d537d83..ccec3d7455 100644
--- a/test/powershell/Host/ConsoleHost.Tests.ps1
+++ b/test/powershell/Host/ConsoleHost.Tests.ps1
@@ -420,14 +420,15 @@ export $envVarName='$guid'
It "errors are in text if error is redirected, encoded command, non-interactive, and outputformat specified" {
$p = [Diagnostics.Process]::new()
$p.StartInfo.FileName = "pwsh"
- $encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('$ErrorView="NormalView";throw "boom"'))
+ $encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('throw "boom"'))
$p.StartInfo.Arguments = "-EncodedCommand $encoded -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -OutputFormat text"
$p.StartInfo.UseShellExecute = $false
$p.StartInfo.RedirectStandardError = $true
$p.Start() | Out-Null
$out = $p.StandardError.ReadToEnd()
$out | Should -Not -BeNullOrEmpty
- $out.Split([Environment]::NewLine)[0] | Should -BeExactly "boom"
+ $out = $out.Split([Environment]::NewLine)[0]
+ [System.Management.Automation.Internal.StringDecorated]::new($out).ToString("PlainText") | Should -BeExactly "Exception: boom"
}
}