Skip to content

Commit 85ab35e

Browse files
committed
This is YamlView which is almost the same as Detailed
1 parent 7618f00 commit 85ab35e

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

source/private/GetYamlRecursive.ps1

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
# Optionally, a limit on the depth to recurse properties (defaults to 16)
1515
[parameter()]
16-
[int]$depth = 16,
16+
[int]$depth = 1,
1717

1818
# If set, include empty and null properties in the output
1919
[switch]$IncludeEmpty,
@@ -27,18 +27,23 @@
2727
[switch]$XmlAsXml
2828
)
2929
process {
30-
$wrap = [Console]::BufferWidth - 1 - ($NestingLevel * 2)
30+
$Width = $Host.UI.RawUI.BufferSize.Width - 1 - ($NestingLevel * 2)
31+
$__hasoutput = $true
32+
$padding = ' ' * $NestingLevel # # lets just create our left-padding for the block
33+
$Recurse = @{
34+
'Depth' = $depth + 1
35+
'NestingLevel' = $NestingLevel + 1
36+
'XmlAsXml' = $XmlAsXml
37+
}
38+
$Wrap = @{
39+
Width = $Host.UI.RawUI.BufferSize.Width - 2
40+
IndentPadding = $padding
41+
Colors = $LineColors
42+
}
43+
3144
@(
3245
if ($Null -eq $InputObject) { return 'null' } # if it is null return null
3346
if ($NestingLevel -eq 0 -and $local:__hasoutput) { '---' } # if we have output before, add a yaml separator
34-
$__hasoutput = $true
35-
$padding = "`n$(' ' * $NestingLevel)" # lets just create our left-padding for the block
36-
$Recurse = @{
37-
'Depth' = $depth - 1
38-
'NestingLevel' = $NestingLevel + 1
39-
'XmlAsXml' = $XmlAsXml
40-
}
41-
$Wrap =
4247

4348
try {
4449
switch ($InputObject) {
@@ -57,7 +62,7 @@
5762
}
5863
{ $InputObject -is [System.Xml.XmlDocument] -or $InputObject -is [System.Xml.XmlElement] } {
5964
"|"
60-
$InputObject.OuterXml | WrapString $Wrap $padding -Colors:$LineColors
65+
$InputObject.OuterXml | WrapString @Wrap
6166
break
6267
}
6368
{ $InputObject -is [datetime] -or $InputObject -is [datetimeoffset] } {
@@ -77,7 +82,7 @@
7782
}
7883
# If we're going to go over our depth, just output like it's a value type
7984
# ValueTypes are just output with no possibility of wrapping or recursion
80-
{ $InputObject -is [Enum] -or $InputObject.GetType().BaseType -eq [ValueType] -or $depth -eq 1 } {
85+
{ $InputObject -is [Enum] -or $InputObject.GetType().BaseType -eq [ValueType] -or $depth -gt $maxDepth } {
8186
"$InputObject"
8287
break
8388
}
@@ -87,7 +92,7 @@
8792
{ $InputObject -is [System.Collections.IDictionary] } {
8893
foreach ($kvp in $InputObject.GetEnumerator()) {
8994
# Write-Verbose "$($padding)Enumerate $($property.Name)"
90-
"$padding$accentColor$($kvp.Name):$resetColor " +
95+
"$newline$padding$accentColor$($kvp.Name):$resetColor " +
9196
(GetYamlRecursive -InputObject $kvp.Value @Recurse)
9297
}
9398
break
@@ -98,24 +103,27 @@
98103
# Write-Verbose "$($padding)Enumerate $($property.Name)"
99104
$Value = GetYamlRecursive -InputObject $item @Recurse
100105
# if ($Value -ne 'null' -or $IncludeEmpty) {
101-
"$accentColor$padding$resetColor- $Value"
106+
"$newline$accentColor$padding$resetColor- $Value"
102107
# }
103108
}
104109
break
105110
}
106111

107112
# Limit recursive enumeration to specific types:
108-
{ $InputObject -is [Exception] -or $InputObject -is [System.Management.Automation.ErrorRecord] -or
113+
{ $InputObject -is [Exception] -or ($InputObject -is [System.Management.Automation.ErrorRecord] -and $depth -lt 2) -or
109114
$InputObject.PSTypeNames[0] -in @(
110-
'System.Exception'
111-
'System.Management.Automation.ErrorRecord'
115+
# 'System.Exception'
116+
# 'System.Management.Automation.ErrorRecord'
112117
'Microsoft.Rest.HttpRequestMessageWrapper'
113118
'Microsoft.Rest.HttpResponseMessageWrapper'
114119
'System.Management.Automation.InvocationInfo'
115120
) } {
121+
if ($depth -ge $maxDepth) {
122+
$null = $output.Append($ellipsis)
123+
}
116124
# For exceptions, output a fake property for the exception type
117125
if ($InputObject -is [Exception]) {
118-
"$padding${accentColor}#Type:$resetColor ${errorAccentColor}" + $InputObject.GetType().FullName + $resetColor
126+
"$newline$padding${accentColor}#Type:$resetColor ${accentColor}" + $InputObject.GetType().FullName + $resetColor
119127
}
120128
foreach ($property in $InputObject.PSObject.Properties) {
121129
if ($property.Value) {
@@ -128,7 +136,7 @@
128136
$Value = "$errorColor$Value$resetColor"
129137
}
130138
if ((-not [string]::IsNullOrEmpty($Value) -and $Value -ne 'null' -and $Value.Count -gt 0) -or $IncludeEmpty) {
131-
"$padding$accentColor$($property.Name):$resetColor " + $Value
139+
"$newline$padding$accentColor$($property.Name):$resetColor " + $Value
132140
}
133141
}
134142
}
@@ -148,9 +156,9 @@
148156
$StringValue = $null
149157
if ([System.Management.Automation.LanguagePrimitives]::TryConvertTo($InputObject, [string], [ref]$StringValue) -and $null -ne $StringValue) {
150158
$StringValue = $StringValue.Trim()
151-
if ($StringValue -match '[\r\n]' -or $StringValue.Length -gt $wrap) {
152-
">" # signal that we are going to use the readable 'newlines-folded' format
153-
$StringValue | WrapString $Wrap $padding -Colors:$LineColors
159+
if ($StringValue -match '[\r\n]' -or $StringValue.Length -gt $Width) {
160+
">$newline" # signal that we are going to use the readable 'newlines-folded' format
161+
$StringValue | WrapString @Wrap -EmphasizeOriginalNewlines
154162
} elseif ($StringValue.Contains(":")) {
155163
"'$($StringValue -replace '''', '''''')'" # single quote it
156164
} else {
@@ -162,7 +170,7 @@
162170
}
163171
}
164172
} catch {
165-
Write-Error "Error'$($_)' in script $($_.InvocationInfo.ScriptName) $($_.InvocationInfo.Line.Trim()) (line $($_.InvocationInfo.ScriptLineNumber)) char $($_.InvocationInfo.OffsetInLine) executing $($_.InvocationInfo.MyCommand) on $type object '$($InputObject)' Class: $($InputObject.GetType().Name) BaseClass: $($InputObject.GetType().BaseType.Name) "
173+
"Error formatting error ($($_)) in script $($_.InvocationInfo.ScriptName) $($_.InvocationInfo.Line.Trim()) (line $($_.InvocationInfo.ScriptLineNumber)) char $($_.InvocationInfo.OffsetInLine) executing $($_.InvocationInfo.MyCommand) on $type object '$($InputObject)' Class: $($InputObject.GetType().Name) BaseClass: $($InputObject.GetType().BaseType.Name) "
166174
}
167175
) -join ""
168176
}

source/public/ConvertTo-YamlErrorView.ps1

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,14 @@
1212
[System.Management.Automation.ErrorRecord]
1313
$InputObject,
1414

15-
# Optionally, a limit on the depth to recurse properties (defaults to 16)
16-
[parameter()]
17-
[int]$depth = 16,
15+
# The maximum depth to recurse into the object
16+
[int]$maxDepth = 10,
1817

1918
# If set, include empty and null properties in the output
20-
[switch]$IncludeEmpty,
21-
22-
# Recursive use only. Handles indentation for formatting
23-
[parameter(DontShow)]
24-
[int]$NestingLevel = 0,
25-
26-
# use OuterXml instead of treating XmlDocuments like objects
27-
[parameter(DontShow)]
28-
[switch]$XmlAsXml
19+
[switch]$IncludeEmpty
2920
)
21+
begin { ResetColor }
3022
process {
31-
GetYamlRecursive $InputObject
23+
GetYamlRecursive -InputObject $InputObject -IncludeEmpty:$IncludeEmpty
3224
}
3325
}

0 commit comments

Comments
 (0)