lib/TaskMenu.ps1
|
function Show-TaskMenu { <# .SYNOPSIS Renders a numbered menu for the available tasks and returns the selected task IDs. Returns an empty array when the user quits. #> [CmdletBinding()] param( [Parameter(Mandatory)] [System.Collections.IDictionary] $Tasks, [Parameter(Mandatory)] [System.Collections.Generic.IEnumerable[string]] $TaskOrder ) $keys = @($TaskOrder) Write-Information '' Write-Information 'Available tasks:' for ($i = 0; $i -lt $keys.Count; $i++) { $task = $Tasks[$keys[$i]] $label = if ($task -and $task.DisplayName) { "$($keys[$i]) - $($task.DisplayName)" } else { $keys[$i] } Write-Information (' {0}. {1}' -f ($i + 1), $label) } Write-Information ' A. Run all' Write-Information ' Q. Quit' Write-Information '' $selection = Read-Host 'Selection (e.g. "1,3,5" or "A")' if ([string]::IsNullOrWhiteSpace($selection)) { return @() } if ($selection -match '^\s*[Qq]\s*$') { return @() } if ($selection -match '^\s*[Aa]\s*$') { return $keys } $indices = $selection -split '[,\s]+' | Where-Object { $_ -match '^\d+$' } | ForEach-Object { [int]$_ } $selected = foreach ($idx in $indices) { if ($idx -ge 1 -and $idx -le $keys.Count) { $keys[$idx - 1] } else { Write-Status -Level Warn -Message " [WARN] Ignoring invalid selection: $idx" } } return @($selected) } |