Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec626d7

Browse files
committedSep 26, 2024
Add PSStyle support
1 parent 3420293 commit ec626d7

10 files changed

+151
-38
lines changed
 

‎source/ErrorView.format.ps1xml

+38
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,43 @@
3030
</CustomEntries>
3131
</CustomControl>
3232
</View>
33+
<View>
34+
<Name>ExceptionInstance</Name>
35+
<OutOfBand />
36+
<ViewSelectedBy>
37+
<TypeName>System.Exception</TypeName>
38+
</ViewSelectedBy>
39+
<CustomControl>
40+
<CustomEntries>
41+
<CustomEntry>
42+
<CustomItem>
43+
<ExpressionBinding>
44+
<ScriptBlock>Write-NativeCommandError $_</ScriptBlock>
45+
</ExpressionBinding>
46+
<ExpressionBinding>
47+
<ScriptBlock>
48+
<![CDATA[
49+
if ($_.ErrorRecord) {
50+
$Record = $_.ErrorRecord
51+
if ($formatter = @(Get-Command "ConvertTo-$($ErrorView -replace "View$")ErrorView" -ListImported -ErrorAction Ignore -ParameterName InputObject -ParameterType [System.Management.Automation.ErrorRecord])) {
52+
& ($formatter[0]) -InputObject $Record
53+
} else {
54+
ConvertTo-NormalErrorView $Record
55+
}
56+
} else {
57+
if ($formatter = @(Get-Command "ConvertTo-$($ErrorView -replace "View$")ExceptionView" -ListImported -ErrorAction Ignore -ParameterName InputObject -ParameterType [System.Exception])) {
58+
& ($formatter[0]) -InputObject $_
59+
} else {
60+
ConvertTo-NormalExceptionView $_
61+
}
62+
}
63+
]]>
64+
</ScriptBlock>
65+
</ExpressionBinding>
66+
</CustomItem>
67+
</CustomEntry>
68+
</CustomEntries>
69+
</CustomControl>
70+
</View>
3371
</ViewDefinitions>
3472
</Configuration>

‎source/public/ConvertTo-CategoryErrorView.ps1

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@ filter ConvertTo-CategoryErrorView {
1515
[System.Management.Automation.ErrorRecord]
1616
$InputObject
1717
)
18-
$InputObject.CategoryInfo.GetMessage()
18+
$resetColor = ''
19+
$errorColor = ''
20+
21+
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
22+
$resetColor = "$([char]0x1b)[0m"
23+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
24+
}
25+
$errorColor + $InputObject.CategoryInfo.GetMessage() + $resetColor
1926
}

‎source/public/ConvertTo-DetailedErrorView.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ function ConvertTo-DetailedErrorView {
3232
$OutputRoot = [System.Text.StringBuilder]::new()
3333

3434
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
35-
$resetColor = $PSStyle.Reset
36-
$errorColor = $PSStyle.Formatting.Error
37-
$accentColor = $PSStyle.Formatting.FormatAccent
35+
$resetColor = "$([char]0x1b)[0m"
36+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
37+
$accentColor = if ($PSStyle.Formatting.ErrorAccent) { $PSStyle.Formatting.ErrorAccent } else { "`e[1;36m" }
3838
}
3939

4040
function DetailedErrorView {

‎source/public/ConvertTo-FullErrorView.ps1

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ filter ConvertTo-FullErrorView {
33
.SYNOPSIS
44
Converts an ErrorRecord to a full error view
55
.DESCRIPTION
6-
The most verbose error view I've got, it shows everything, recursing forever.
6+
A simple, verbose error view that just shows everything, recursing forever.
77
#>
88
[CmdletBinding()]
99
param(
@@ -12,19 +12,35 @@ filter ConvertTo-FullErrorView {
1212
[System.Management.Automation.ErrorRecord]
1313
$InputObject
1414
)
15-
$PSStyle.OutputRendering, $Rendering = "Ansi", $PSStyle.OutputRendering
16-
$Detail = $InputObject | Format-List * -Force | Out-String
17-
$PSStyle.OutputRendering = $Rendering
15+
$resetColor = ''
16+
$errorColor = ''
17+
#$accentColor = ''
18+
19+
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
20+
# For Format-List to use color when piped to Out-String, OutputRendering needs to be Ansi
21+
$PSStyle.OutputRendering, $Rendering = "Ansi", $PSStyle.OutputRendering
22+
23+
$resetColor = "$([char]0x1b)[0m"
24+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
25+
#$accentColor = if ($PSStyle.Formatting.ErrorAccent) { $PSStyle.Formatting.ErrorAccent } else { "`e[1;36m" }
26+
$Detail = $InputObject | Format-List * -Force | Out-String -Width 120
27+
$Detail = $Detail -replace "((?:Exception|FullyQualifiedErrorId).*`e\[0m)(.*)", "$($PSStyle.Formatting.ErrorAccent)`$1$($PSStyle.Formatting.Error)`$2$($PSStyle.Reset)"
28+
} else {
29+
$Detail = $InputObject | Format-List * -Force | Out-String -Width 120
30+
}
1831

1932
# NOTE: ErrorViewRecurse is normally false, and only set temporarily by Format-Error -Recurse
2033
if ($ErrorViewRecurse) {
2134
$Count = 1
2235
$Exception = $InputObject.Exception
2336
while ($Exception = $Exception.InnerException) {
24-
$Detail += "`nINNER EXCEPTION $($Count): $($Exception.GetType().FullName)`n`n"
25-
$Detail += $Exception | Format-List * -Force | Out-String
37+
$Detail += $errorColor + "`nINNER EXCEPTION $($Count): $resetColor$($Exception.GetType().FullName)`n`n"
38+
$Detail += $Exception | Format-List * -Force | Out-String -Width 120
2639
$Count++
2740
}
2841
}
42+
if ($resetColor) {
43+
$PSStyle.OutputRendering = $Rendering
44+
}
2945
$Detail
3046
}

‎source/public/ConvertTo-NormalErrorView.ps1

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ filter ConvertTo-NormalErrorView {
1111
[System.Management.Automation.ErrorRecord]
1212
$InputObject
1313
)
14+
$resetColor = ''
15+
$errorColor = ''
16+
#$accentColor = ''
17+
18+
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
19+
$resetColor = "$([char]0x1b)[0m"
20+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
21+
#$accentColor = if ($PSStyle.Formatting.ErrorAccent) { $PSStyle.Formatting.ErrorAccent } else { "`e[1;36m" }
22+
}
1423

1524
if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
16-
$InputObject.Exception.Message
25+
$errorColor + $InputObject.Exception.Message + $resetColor
1726
} else {
1827
$myinv = $InputObject.InvocationInfo
1928
if ($myinv -and ($myinv.MyCommand -or ($InputObject.CategoryInfo.Category -ne 'ParserError'))) {
@@ -66,9 +75,9 @@ filter ConvertTo-NormalErrorView {
6675
}
6776

6877
if (!$InputObject.ErrorDetails -or !$InputObject.ErrorDetails.Message) {
69-
$InputObject.Exception.Message + $posmsg + "`n "
78+
$errorColor + $InputObject.Exception.Message + $posmsg + $resetColor + "`n "
7079
} else {
71-
$InputObject.ErrorDetails.Message + $posmsg
80+
$errorColor + $InputObject.ErrorDetails.Message + $posmsg + $resetColor
7281
}
7382
}
7483
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
filter ConvertTo-NormalExceptionView {
2+
<#
3+
.SYNOPSIS
4+
Converts an Exception to a NormalView message string
5+
.DESCRIPTION
6+
The original default PowerShell ErrorView, updated for VT100
7+
#>
8+
[CmdletBinding()]
9+
param(
10+
[Parameter(ValueFromPipeline)]
11+
[System.Exception]
12+
$InputObject
13+
)
14+
$resetColor = ''
15+
$errorColor = ''
16+
#$accentColor = ''
17+
18+
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
19+
$resetColor = "$([char]0x1b)[0m"
20+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
21+
#$accentColor = if ($PSStyle.Formatting.ErrorAccent) { $PSStyle.Formatting.ErrorAccent } else { "`e[1;36m" }
22+
}
23+
24+
$errorColor + $InputObject.Exception.Message + $resetColor
25+
26+
}

‎source/public/ConvertTo-SimpleErrorView.ps1

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ function ConvertTo-SimpleErrorView {
44
[System.Management.Automation.ErrorRecord]
55
$InputObject
66
)
7+
$resetColor = ''
8+
$errorColor = ''
9+
#$accentColor = ''
10+
11+
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
12+
$resetColor = "$([char]0x1b)[0m"
13+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
14+
#$accentColor = if ($PSStyle.Formatting.ErrorAccent) { $PSStyle.Formatting.ErrorAccent } else { "`e[1;36m" }
15+
}
716

817
if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
918
$InputObject.Exception.Message
@@ -46,9 +55,9 @@ function ConvertTo-SimpleErrorView {
4655
}
4756

4857
if (!$InputObject.ErrorDetails -or !$InputObject.ErrorDetails.Message) {
49-
$InputObject.Exception.Message + $posmsg + "`n "
58+
$errorColor + $InputObject.Exception.Message + $posmsg + $resetColor + "`n "
5059
} else {
51-
$InputObject.ErrorDetails.Message + $posmsg
60+
$errorColor + $InputObject.ErrorDetails.Message + $posmsg + $resetColor
5261
}
5362
}
5463
}

‎source/public/Format-Error.ps1

+8-7
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,26 @@ function Format-Error {
3333
})]
3434
$View = "Detailed",
3535

36-
[Parameter(ParameterSetName="Count", Mandatory)]
36+
[Parameter(ParameterSetName="Count")]
3737
[int]$Newest = 1,
3838

3939
# Error records (e.g. from $Error). Defaults to the most recent error: $Error[0]
4040
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName="InputObject", Mandatory)]
4141
[Alias("ErrorRecord")]
42-
[System.Management.Automation.ErrorRecord]$InputObject = $(
43-
$e = $Error[0..($Newest-1)]
44-
if ($e -is ([System.Management.Automation.ErrorRecord])) { $e }
45-
elseif ($e.ErrorRecord -is ([System.Management.Automation.ErrorRecord])) { $e.ErrorRecord }
46-
elseif ($Error.Count -eq 0) { Write-Warning "The global `$Error collection is empty" }
42+
[PSObject]$InputObject = $(
43+
if ($global:Error.Count -eq 0) {
44+
Write-Warning "The global `$Error collection is empty"
45+
} else {
46+
$global:Error[0..($Newest-1)]
47+
}
4748
),
4849

4950
# Allows ErrorView functions to recurse to InnerException
5051
[switch]$Recurse
5152
)
5253
begin {
5354
$ErrorActionPreference = "Continue"
54-
$View, $ErrorView = $ErrorView, $View
55+
$ErrorView, $View = $View, $ErrorView
5556
[bool]$Recurse, [bool]$ErrorViewRecurse = [bool]$ErrorViewRecurse, $Recurse
5657
}
5758
process {

‎source/public/Set-ErrorView.ps1

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ filter Set-ErrorView {
2727
).Name -replace "ConvertTo-(\w+)ErrorView", '$1View' | Select-Object -Unique
2828

2929
$ofs = ';'
30-
[ScriptBlock]::Create("enum ErrorView { $Names }").Invoke()
31-
30+
.([ScriptBlock]::Create("enum ErrorView { $Names }"))
3231
[ErrorView]$global:ErrorView = $View
3332
}
+22-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
11
function Write-NativeCommandError {
22
[CmdletBinding()]
33
param(
4-
[System.Management.Automation.ErrorRecord]
54
$InputObject
65
)
6+
$resetColor = ''
7+
$errorColor = ''
8+
$accentColor = ''
9+
10+
if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) {
11+
$resetColor = "$([char]0x1b)[0m"
12+
$errorColor = if ($PSStyle.Formatting.Error) { $PSStyle.Formatting.Error } else { "`e[1;31m" }
13+
$accentColor = $PSStyle.Formatting.ErrorAccent
14+
}
715

816
if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") { return }
917

10-
$myinv = $InputObject.InvocationInfo
11-
if ($myinv -and $myinv.MyCommand) {
12-
switch -regex ( $myinv.MyCommand.CommandType ) {
18+
$invoc = $InputObject.InvocationInfo
19+
if ($invoc -and $invoc.MyCommand) {
20+
switch -regex ( $invoc.MyCommand.CommandType ) {
1321
([System.Management.Automation.CommandTypes]::ExternalScript) {
14-
if ($myinv.MyCommand.Path) {
15-
$myinv.MyCommand.Path + " : "
22+
if ($invoc.MyCommand.Path) {
23+
$accentColor + $invoc.MyCommand.Path + " : " + $resetColor
1624
}
1725
break
1826
}
1927
([System.Management.Automation.CommandTypes]::Script) {
20-
if ($myinv.MyCommand.ScriptBlock) {
21-
$myinv.MyCommand.ScriptBlock.ToString() + " : "
28+
if ($invoc.MyCommand.ScriptBlock) {
29+
$accentColor + $invoc.MyCommand.ScriptBlock.ToString() + " : " + $resetColor
2230
}
2331
break
2432
}
2533
default {
26-
if ($myinv.InvocationName -match '^[&amp;\.]?$') {
27-
if ($myinv.MyCommand.Name) {
28-
$myinv.MyCommand.Name + " : "
34+
if ($invoc.InvocationName -match '^[&amp;\.]?$') {
35+
if ($invoc.MyCommand.Name) {
36+
$accentColor + $invoc.MyCommand.Name + " : " + $resetColor
2937
}
3038
} else {
31-
$myinv.InvocationName + " : "
39+
$accentColor + $invoc.InvocationName + " : " + $resetColor
3240
}
3341
break
3442
}
3543
}
36-
} elseif ($myinv -and $myinv.InvocationName) {
37-
$myinv.InvocationName + " : "
44+
} elseif ($invoc -and $invoc.InvocationName) {
45+
$accentColor + $invoc.InvocationName + " : " + $resetColor
3846
}
3947
}

0 commit comments

Comments
 (0)
Please sign in to comment.