|
13 | 13 |
|
14 | 14 | # Optionally, a limit on the depth to recurse properties (defaults to 16)
|
15 | 15 | [parameter()]
|
16 |
| - [int]$depth = 16, |
| 16 | + [int]$depth = 1, |
17 | 17 |
|
18 | 18 | # If set, include empty and null properties in the output
|
19 | 19 | [switch]$IncludeEmpty,
|
|
27 | 27 | [switch]$XmlAsXml
|
28 | 28 | )
|
29 | 29 | 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 | + |
31 | 44 | @(
|
32 | 45 | if ($Null -eq $InputObject) { return 'null' } # if it is null return null
|
33 | 46 | 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 = |
42 | 47 |
|
43 | 48 | try {
|
44 | 49 | switch ($InputObject) {
|
|
57 | 62 | }
|
58 | 63 | { $InputObject -is [System.Xml.XmlDocument] -or $InputObject -is [System.Xml.XmlElement] } {
|
59 | 64 | "|"
|
60 |
| - $InputObject.OuterXml | WrapString $Wrap $padding -Colors:$LineColors |
| 65 | + $InputObject.OuterXml | WrapString @Wrap |
61 | 66 | break
|
62 | 67 | }
|
63 | 68 | { $InputObject -is [datetime] -or $InputObject -is [datetimeoffset] } {
|
|
77 | 82 | }
|
78 | 83 | # If we're going to go over our depth, just output like it's a value type
|
79 | 84 | # 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 } { |
81 | 86 | "$InputObject"
|
82 | 87 | break
|
83 | 88 | }
|
|
87 | 92 | { $InputObject -is [System.Collections.IDictionary] } {
|
88 | 93 | foreach ($kvp in $InputObject.GetEnumerator()) {
|
89 | 94 | # Write-Verbose "$($padding)Enumerate $($property.Name)"
|
90 |
| - "$padding$accentColor$($kvp.Name):$resetColor " + |
| 95 | + "$newline$padding$accentColor$($kvp.Name):$resetColor " + |
91 | 96 | (GetYamlRecursive -InputObject $kvp.Value @Recurse)
|
92 | 97 | }
|
93 | 98 | break
|
|
98 | 103 | # Write-Verbose "$($padding)Enumerate $($property.Name)"
|
99 | 104 | $Value = GetYamlRecursive -InputObject $item @Recurse
|
100 | 105 | # if ($Value -ne 'null' -or $IncludeEmpty) {
|
101 |
| - "$accentColor$padding$resetColor- $Value" |
| 106 | + "$newline$accentColor$padding$resetColor- $Value" |
102 | 107 | # }
|
103 | 108 | }
|
104 | 109 | break
|
105 | 110 | }
|
106 | 111 |
|
107 | 112 | # 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 |
109 | 114 | $InputObject.PSTypeNames[0] -in @(
|
110 |
| - 'System.Exception' |
111 |
| - 'System.Management.Automation.ErrorRecord' |
| 115 | + # 'System.Exception' |
| 116 | + # 'System.Management.Automation.ErrorRecord' |
112 | 117 | 'Microsoft.Rest.HttpRequestMessageWrapper'
|
113 | 118 | 'Microsoft.Rest.HttpResponseMessageWrapper'
|
114 | 119 | 'System.Management.Automation.InvocationInfo'
|
115 | 120 | ) } {
|
| 121 | + if ($depth -ge $maxDepth) { |
| 122 | + $null = $output.Append($ellipsis) |
| 123 | + } |
116 | 124 | # For exceptions, output a fake property for the exception type
|
117 | 125 | 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 |
119 | 127 | }
|
120 | 128 | foreach ($property in $InputObject.PSObject.Properties) {
|
121 | 129 | if ($property.Value) {
|
|
128 | 136 | $Value = "$errorColor$Value$resetColor"
|
129 | 137 | }
|
130 | 138 | 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 |
132 | 140 | }
|
133 | 141 | }
|
134 | 142 | }
|
|
148 | 156 | $StringValue = $null
|
149 | 157 | if ([System.Management.Automation.LanguagePrimitives]::TryConvertTo($InputObject, [string], [ref]$StringValue) -and $null -ne $StringValue) {
|
150 | 158 | $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 |
154 | 162 | } elseif ($StringValue.Contains(":")) {
|
155 | 163 | "'$($StringValue -replace '''', '''''')'" # single quote it
|
156 | 164 | } else {
|
|
162 | 170 | }
|
163 | 171 | }
|
164 | 172 | } 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) " |
166 | 174 | }
|
167 | 175 | ) -join ""
|
168 | 176 | }
|
|
0 commit comments