Private/UI/Show-CommandPreview.ps1
|
function Show-CommandPreview { param([GalleryModuleInfo]$ModuleInfo) $palette = Get-PSMBColorPalette $esc = [char]27 $reset = "${esc}[0m" $innerWidth = Get-PSMBConsoleInnerWidth $border = ([char]0x2502) $hLine = ([char]0x2500) $amberRGB = @(168, 213, 162) $roseRGB = @(94, 234, 212) $fitText = { param([string]$Text) if ($null -eq $Text) { return '' } if ($Text.Length -le $innerWidth) { return $Text } if ($innerWidth -le 3) { return $Text.Substring(0, [Math]::Max(0, $innerWidth)) } return $Text.Substring(0, $innerWidth - 3) + '...' } $writePaddedLine = { param([string]$Text, [string]$Style) $clipped = & $fitText $Text $padded = $clipped.PadRight($innerWidth) Write-Host (' {0}{1}{2}{3}{4}{5}' -f $palette.Surface, $border, $Style, $padded, $reset, ('{0}{1}{2}' -f $palette.Surface, $border, $reset)) } Write-Host '' # Top border $topLine = Get-PSMBGradientLine -Character $hLine -Length $innerWidth -StartRGB $amberRGB -EndRGB $roseRGB Write-Host (' {0}{1}{2}{3}' -f $palette.Surface, ([char]0x256D), $topLine, ([char]0x256E)) -NoNewline Write-Host $palette.Reset & $writePaddedLine -Text '' -Style $palette.Text # Title $titleText = ' Commands in {0}' -f $ModuleInfo.Name & $writePaddedLine -Text $titleText -Style ('{0}{1}' -f $palette.Bold, $palette.Amber) & $writePaddedLine -Text '' -Style $palette.Text # Separator $sepLine = Get-PSMBGradientLine -Character $hLine -Length $innerWidth -StartRGB $amberRGB -EndRGB $roseRGB Write-Host (' {0}{1}{2}{3}' -f $palette.Surface, ([char]0x251C), $sepLine, ([char]0x2524)) -NoNewline Write-Host $palette.Reset & $writePaddedLine -Text '' -Style $palette.Text $localModule = Get-InstalledModuleInfo -Name $ModuleInfo.Name if ($localModule) { $commands = @($localModule.ExportedCommands.Keys | Sort-Object) & $writePaddedLine -Text (' {0} Installed (v{1} - showing actual exports)' -f ([char]0x25CF), $localModule.Version) -Style $palette.Green } elseif ($ModuleInfo.ExportedCommands -and $ModuleInfo.ExportedCommands.Count -gt 0) { $commands = @($ModuleInfo.ExportedCommands | Sort-Object) & $writePaddedLine -Text (' {0} Not installed (showing gallery metadata)' -f ([char]0x25CB)) -Style $palette.Yellow } else { & $writePaddedLine -Text ' No command information available.' -Style $palette.Dim & $writePaddedLine -Text ' Install the module to see exported commands.' -Style $palette.Dim & $writePaddedLine -Text '' -Style $palette.Text $bottomLine = Get-PSMBGradientLine -Character $hLine -Length $innerWidth -StartRGB $amberRGB -EndRGB $roseRGB Write-Host (' {0}{1}{2}{3}' -f $palette.Surface, ([char]0x2570), $bottomLine, ([char]0x256F)) -NoNewline Write-Host $palette.Reset Write-Host '' return } & $writePaddedLine -Text (' {0} command(s)' -f $commands.Count) -Style ('{0}{1}' -f $palette.Bold, $palette.Text) & $writePaddedLine -Text '' -Style $palette.Text $triangle = [char]0x25B8 foreach ($cmd in $commands) { & $writePaddedLine -Text (' {0} {1}' -f $triangle, $cmd) -Style $palette.Amber } & $writePaddedLine -Text '' -Style $palette.Text # Bottom border $bottomLine = Get-PSMBGradientLine -Character $hLine -Length $innerWidth -StartRGB $amberRGB -EndRGB $roseRGB Write-Host (' {0}{1}{2}{3}' -f $palette.Surface, ([char]0x2570), $bottomLine, ([char]0x256F)) -NoNewline Write-Host $palette.Reset Write-Host '' } |