Private/UI/Show-InstallConfirmation.ps1
|
function Show-InstallConfirmation { param( [GalleryModuleInfo]$ModuleInfo, [string]$Version, [string]$Scope = 'CurrentUser' ) $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) $displayVersion = if ($Version) { $Version } else { $ModuleInfo.Version } $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)) } Clear-Host 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 = ' Install Module Confirmation' & $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 # Module details & $writePaddedLine -Text (' Name: {0}' -f $ModuleInfo.Name) -Style $palette.Text & $writePaddedLine -Text (' Version: {0}' -f $displayVersion) -Style $palette.Text & $writePaddedLine -Text (' Author: {0}' -f $ModuleInfo.Author) -Style $palette.Text & $writePaddedLine -Text (' Scope: {0}' -f $Scope) -Style $palette.Text & $writePaddedLine -Text (' Repository: {0}' -f $ModuleInfo.Repository) -Style $palette.Text & $writePaddedLine -Text '' -Style $palette.Text if ($ModuleInfo.Description) { $desc = $ModuleInfo.Description if ($desc.Length -gt 120) { $desc = $desc.Substring(0, 117) + '...' } & $writePaddedLine -Text (' {0}' -f $desc) -Style $palette.Dim & $writePaddedLine -Text '' -Style $palette.Text } if ($ModuleInfo.Dependencies -and $ModuleInfo.Dependencies.Count -gt 0) { $notInstalled = $ModuleInfo.Dependencies | Where-Object { -not (Get-Module -Name $_ -ListAvailable -ErrorAction SilentlyContinue) } & $writePaddedLine -Text (' Dependencies: {0} listed' -f $ModuleInfo.Dependencies.Count) -Style ('{0}{1}' -f $palette.Bold, $palette.Text) if ($notInstalled -and @($notInstalled).Count -gt 0) { & $writePaddedLine -Text (' {0} +{1} additional module(s) will be installed' -f ([char]0x26A0), @($notInstalled).Count) -Style $palette.Yellow } else { & $writePaddedLine -Text (' {0} All dependencies already satisfied' -f ([char]0x2714)) -Style $palette.Green } & $writePaddedLine -Text '' -Style $palette.Text } if ($ModuleInfo.InstallStatus -eq 'Installed') { & $writePaddedLine -Text (' {0} Already installed (v{1}). This will reinstall.' -f ([char]0x26A0), $ModuleInfo.InstalledVersion) -Style $palette.Yellow & $writePaddedLine -Text '' -Style $palette.Text } elseif ($ModuleInfo.InstallStatus -eq 'UpdateAvailable') { $arrow = [char]0x2192 & $writePaddedLine -Text (' {0} Update: v{1} {2} v{3}' -f ([char]0x2191), $ModuleInfo.InstalledVersion, $arrow, $displayVersion) -Style $palette.Green & $writePaddedLine -Text '' -Style $palette.Text } # Separator Write-Host (' {0}{1}{2}{3}' -f $palette.Surface, ([char]0x251C), $sepLine, ([char]0x2524)) -NoNewline Write-Host $palette.Reset & $writePaddedLine -Text '' -Style $palette.Text & $writePaddedLine -Text ' Proceed with installation?' -Style ('{0}{1}' -f $palette.Bold, $palette.Text) & $writePaddedLine -Text (' {0}[Y]{1} Yes {2}[N/Esc]{1} Cancel' -f $palette.Green, $reset, $palette.Rose) -Style $palette.Text & $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 try { [Console]::CursorVisible = $false while ($true) { $key = [Console]::ReadKey($true) $keyChar = $key.KeyChar.ToString().ToUpper() if ($keyChar -eq 'Y' -or $key.Key -eq 'Enter') { return $true } if ($keyChar -eq 'N' -or $key.Key -eq 'Escape') { return $false } } } finally { [Console]::CursorVisible = $true } } |