PowerShellProTools.psm1

New-Alias -Name 'pad' -Value 'Show-PSScriptPad' -Force

$Platform = 'windows'
if ($IsLinux) {
    $Platform = 'linux'
}
elseif ($IsMacOS) {
    $Platform = 'mac'
}

$Runtime = "net472"
if ($IsCoreCLR) {
    if ($PSVersionTable.PSVersion.Major -eq 7 -and $PSVersionTable.PSVersion.Minor -ge 1) {
        $Runtime = 'net5.0'
    } 
    elseif ($PSVersionTable.PSVersion.Major -eq 7 -and $PSVersionTable.PSVersion.Minor -eq 0) {
        $Runtime = 'netcoreapp3.1'
    } 
}

Import-Module "$PSScriptRoot\Gui\NStack.dll" | Out-Null
Import-Module "$PSScriptRoot\Gui\Terminal.Gui.dll" | Out-Null

function Expand-Object {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [object]$InputObject
    )

    Process {
        if ($InputObject -eq $null) {
            return
        }
        $InputObject | Get-Member -MemberType Properties | ForEach-Object {
            try {
                $Value = $InputObject.($_.Name)
                $Node = [Terminal.Gui.Trees.TreeNode]::new("$($_.Name) = $Value")

                if ($Value -ne $null) {
                    $Children = Expand-Object -InputObject $Value
                    foreach ($child in $Children) {
                        $Node.Children.Add($child)
                    }
                }

                $Node
            }
            catch {
                Write-Host $_
            }
        }

    }
}

function Out-TreeView {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [object]$InputObject
    )

    Begin {
        $Objects = @() 
    }
    
    Process {
        $Objects += $InputObject
    }

    End {
        [Terminal.Gui.Application]::Init()
        $Top = [Terminal.Gui.Application]::Top

        $Win = [Terminal.Gui.Window]::new("Out-TreeView")
        $Win.Height = [Terminal.Gui.Dim]::Fill()
        $Win.Width = [Terminal.Gui.Dim]::Fill()

        $TreeView = [Terminal.Gui.TreeView]::new()
        $TreeView.Height = [Terminal.Gui.Dim]::Fill()
        $TreeView.Width = [Terminal.Gui.Dim]::Fill()

        foreach ($item in $Objects) {
            $root = [Terminal.Gui.Trees.TreeNode]::new($item.GetType().Name)
            $Children = Expand-Object $item
            $Children | ForEach-Object {
                $root.Children.Add($_)
            }
            $TreeView.AddObject($root)
        }

        $Win.Add($TreeView)

        $Top.Add($Win)
 
        [Terminal.Gui.Application]::Run()
    }
}