ps/Modules/Cole.PowerShell.Developer/Public/Show-Help.ps1
2023-05-30 22:51:22 -07:00

158 lines
6.4 KiB
PowerShell

function Show-Help {
[CmdletBinding()]
param (
[Alias('Command')]
[Alias('CommandName')]
$Name
)
$allHelp = Get-Help -Name $Name
foreach ($help in $allHelp) {
$titleLines = @()
$textLines = @()
$helpName = $help.details.name
$verb = $help.details.verb
if ([string]::IsNullOrWhiteSpace($verb)) {
$verb = ($helpName -split '-')[0]
}
$maxWidth = (Get-ConsoleDisplayWidth) - 30
if ($maxWidth -lt 50) {
$maxWidth = Get-ConsoleDisplayWidth
}
$verbType = (Get-Verb -Verb $verb).Group
$moduleDisplay = ""
if (![string]::IsNullOrWhiteSpace($help.ModuleName)) {
$moduleDisplay = "$($PSStyle.ForegroundColor.White)$($help.ModuleName)\$($PSStyle.Reset)"
}
$titleLines += "$($help.category) $moduleDisplay$($PSStyle.Bold)$helpName$($PSStyle.BoldOff) ($verb is a $verbType type Verb)"
$titleLines += "`t$($help.Synopsis)"
$description = Format-TextWrapToDisplay -MaxWidth $maxWidth -Text ($help.description.text -join ' ')
if ($description.Count -gt 0) {
$textLines += "$($PSStyle.ForegroundColor.White)Description:$($PSStyle.Reset)"
foreach($line in $description) {
$textLines += "`t$line"
}
}
foreach ($inputType in $help.inputTypes.inputType) {
$type = Convert-TypeForHelpDisplay -TypeName $inputType.type.name
$description = Format-TextWrapToDisplay -MaxWidth $maxWidth -Text $inputType.description.Text
$textLines += "$($PSStyle.ForegroundColor.White)InputType:$($PSStyle.Reset) $($PSStyle.ForegroundColor.DarkGray)[$type]$($PSStyle.Reset)"
foreach($line in $description) {
$textLines += "`t$line"
}
}
foreach ($returnValue in $help.returnValues.returnValue) {
$type = Convert-TypeForHelpDisplay -TypeName $returnValue.type.name
$description = Format-TextWrapToDisplay -MaxWidth $maxWidth -Text $returnValue.description.Text
$textLines += "$($PSStyle.ForegroundColor.White)Returns:$($PSStyle.Reset) $($PSStyle.ForegroundColor.DarkGray)[$type]$($PSStyle.Reset)"
foreach($line in $description) {
$textLines += "`t$line"
}
}
$textLines += " "
foreach ($parameter in $help.parameters.parameter) {
$parameterName = $parameter.Name
$type = Convert-TypeForHelpDisplay -TypeName $parameter.type.name
$parameterValues = $help.syntax.syntaxItem.parameter.Where({$_.Name -eq $parameterName})[0].parameterValueGroup.parameterValue
$globbing = $parameter.globbing
$parameterValue = $parameter.parameterValue
$defaultValue = $parameter.defaultValue
if ($defaultValue -eq 'none') {
$defaultValue = ''
}
$aliases = ($parameter.aliases -split ',' | Foreach-Object { $_.Trim() }).Where({$_ -ne 'none' -and ![string]::IsNullOrWhiteSpace($_)})
$required = $parameter.required
if ($required -eq 'true') {
$required = "*"
} else {
$required = " "
}
$defaultValue = ""
if (![string]::IsNullOrWhiteSpace($defaultValue)) {
$defaultValue = "(=$defaultValue)"
}
$wildcardText = ""
if ($globbing -eq 'true') {
$wildcardText = " $($PSStyle.ForegroundColor.LightCyan)(allows wildcards)$($PSStyle.Reset)"
}
$textLines += "$($PSStyle.ForegroundColor.White)Parameter:$($PSStyle.Reset) $required $($PSStyle.ForegroundColor.DarkGray)[$type]$($PSStyle.Reset) $($PSStyle.Bold)$parameterName$($PSStyle.BoldOff)$defaultValue$wildcardText"
$usage = @("$($PSStyle.Bold)-$parameterName$($PSStyle.BoldOff)")
foreach ($alias in $aliases) {
$usage += "|-$alias"
}
$usage = $usage -join ''
$textLines += "$($PSStyle.ForegroundColor.White)Usage:$($PSStyle.Reset) $usage"
if ($null -ne $parameterValues) {
$values = $parameterValues -join ','
$textLines += "$($PSStyle.ForegroundColor.White)Values:$($PSStyle.Reset) [$values]"
}
$pipelineInput = $parameter.pipelineInput
$pipelineInputByValue = $pipelineInput -contains "ByValue"
$pipelineInputByName = $pipelineInput -contains "ByPropertyName"
if ($pipelineInputByName) {
$textLines += "Accepts Pipeline Input By Name"
}
if ($pipelineInputByValue) {
$textLines += "Accepts Pipeline Input By Value"
}
$position = $parameter.position.Trim()
if ($position -match "^[0-9]+$") {
$textLines += "$($PSStyle.ForegroundColor.White)Position:$($PSStyle.Reset) $position"
}
$description = ($parameter.description.text -join ' ')
if (![string]::IsNullOrWhiteSpace($description)) {
$descriptionLines = Format-TextWrapToDisplay -MaxWidth $maxWidth -Text $description
foreach($line in $descriptionLines) {
$textLines += "`t$line"
}
}
}
foreach ($link in $help.relatedLinks.navigationLink) {
$linkText = $link.linkText
if ([string]::IsNullOrWhiteSpace($link.uri)) {
$verbProbable = ($linkText -split '-')[0]
if ($verbProbable -eq (Get-Verb -Verb $verbProbable).Verb) {
$commandProbable = Get-Command -Name $linkText
if ($null -ne $commandProbable) {
$textLines += "See also: $($commandProbable.ModuleName)\$linkText"
} else {
$textLines += "Related: $linkText"
}
} else {
$textLines += "Related: $linkText"
}
} else {
$textLines += "Link: $linkText"
$textLines += "`t`t$($link.uri)"
}
}
foreach ($alert in $help.alertSet) {
if (![string]::IsNullOrWhiteSpace($alert.text)) {
$textLines += "AlertSet: $($alert.text)"
}
}
Show-Box -TextContent $textLines -TitleText $titleLines -Padding 1 -TextTruncate
}
}