| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Expands an object into a formatted string for display. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function takes an object and formats it for display in the console. It includes the type name and the objec |
| | | 6 | | If a label is provided, it prefixes the output with the label. |
| | | 7 | | .PARAMETER InputObject |
| | | 8 | | The object to expand and display. This can be any PowerShell object, including complex types. |
| | | 9 | | .PARAMETER ForegroundColor |
| | | 10 | | The color to use for the output text in the console. If not specified, defaults to the console's current foregro |
| | | 11 | | .PARAMETER Label |
| | | 12 | | An optional label to prefix the output. This can be used to provide context or a name for the object being displ |
| | | 13 | | .PARAMETER PassThru |
| | | 14 | | If specified, the function will return the formatted string instead of writing it to the console. |
| | | 15 | | .EXAMPLE |
| | | 16 | | Expand-KrObject -InputObject $myObject -ForegroundColor Cyan -Label "My Object" |
| | | 17 | | Displays the $myObject with a cyan foreground color and prefixes it with "My Object". |
| | | 18 | | .NOTES |
| | | 19 | | This function is designed to be used in the context of Kestrun for debugging or logging purposes. |
| | | 20 | | #> |
| | | 21 | | function Expand-KrObject { |
| | | 22 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')] |
| | | 23 | | [KestrunRuntimeApi('Everywhere')] |
| | | 24 | | [CmdletBinding(defaultParameterSetName = 'Console')] |
| | | 25 | | [OutputType([string])] |
| | | 26 | | param( |
| | | 27 | | [Parameter(Position = 0, ValueFromPipeline = $true)] |
| | | 28 | | [object] $InputObject, |
| | | 29 | | |
| | | 30 | | [Parameter(ParameterSetName = 'Console')] |
| | | 31 | | [System.ConsoleColor] $ForegroundColor, |
| | | 32 | | |
| | | 33 | | [Parameter()] |
| | | 34 | | [string] $Label, |
| | | 35 | | |
| | | 36 | | [Parameter(ParameterSetName = 'PassThru')] |
| | | 37 | | [switch]$PassThru |
| | | 38 | | ) |
| | | 39 | | |
| | | 40 | | process { |
| | | 41 | | |
| | 0 | 42 | | if ($null -eq $InputObject) { |
| | 0 | 43 | | $InputObject = "`tNull Value" |
| | | 44 | | } else { |
| | 0 | 45 | | $type = $InputObject.gettype().FullName |
| | 0 | 46 | | $InputObject = $InputObject | Out-String |
| | 0 | 47 | | $InputObject = "`tTypeName: $type`n$InputObject" |
| | | 48 | | } |
| | 0 | 49 | | if ($Label) { |
| | 0 | 50 | | $InputObject = "`tName: $Label $InputObject" |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | # Return the object if PassThru is specified |
| | 0 | 54 | | if ($PassThru) { |
| | 0 | 55 | | return $InputObject |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if ($ForegroundColor) { |
| | 0 | 59 | | if ($pipelineValue.Count -gt 1) { |
| | 0 | 60 | | $InputObject | Write-Host -ForegroundColor $ForegroundColor |
| | | 61 | | } else { |
| | 0 | 62 | | Write-Host -Object $InputObject -ForegroundColor $ForegroundColor |
| | | 63 | | } |
| | | 64 | | } else { |
| | 0 | 65 | | if ($pipelineValue.Count -gt 1) { |
| | 0 | 66 | | $InputObject | Write-Host |
| | | 67 | | } else { |
| | 0 | 68 | | Write-Host -Object $InputObject |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |