feat(menu): arrow-key navigation for Elysium.ps1
Console.ReadKey + cursor-painted highlight, no new dependency - works on 5.1 Desktop and 7. Number keys still jump straight to and pick an item. Falls back to the old plain Read-Host prompt when input is redirected or the host has no real console. The keypress-to-selection logic (Resolve-ElysiumMenuKeyPress) is a pure function in Private/Menu.ps1 so it's covered by Pester without a real terminal.
This commit is contained in:
@@ -8,6 +8,9 @@ Starting with **v2.2.0**, Elysium uses a **unified project version**. All script
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- **`Elysium.ps1` menu:** navigate with Up/Down + Enter, or still type a number directly; Esc/Q exits. Falls back to the old plain numbered prompt when input is redirected or the host has no real console. The keypress logic (`Resolve-ElysiumMenuKeyPress`) is a separate, Pester-tested function.
|
||||
|
||||
### Changed
|
||||
- **KHDB backups:** the previous `khdb.txt` is now moved aside instead of copied (instant instead of minutes for a 40+ GB file), and only the newest one is kept. `KhdbBackupCount` in `ElysiumSettings.txt` sets how many (default 1, `0` = none; was a fixed 5).
|
||||
|
||||
|
||||
+64
-10
@@ -27,6 +27,9 @@ Elysium.ps1 offers a menu to perform various actions:
|
||||
# Safer defaults
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version Latest
|
||||
# Pure keypress logic for the arrow-key menu below; kept in the module's Private/ so Pester can test
|
||||
# it without a real console.
|
||||
. (Join-Path -Path $PSScriptRoot -ChildPath 'src/Elysium/Private/Menu.ps1')
|
||||
# Define the path to the settings file
|
||||
$settingsFilePath = Join-Path -Path $PSScriptRoot -ChildPath "ElysiumSettings.txt"
|
||||
|
||||
@@ -89,25 +92,76 @@ function Invoke-WindowsPowerShellScript {
|
||||
}
|
||||
}
|
||||
|
||||
$script:MenuItems = @(
|
||||
'Download/Update Known-Hashes Database (KHDB)'
|
||||
'Test Weak AD Passwords'
|
||||
'Extract and Send Current Hashes for KHDB Update'
|
||||
'Update Lithnet Password Protection Store'
|
||||
'Uninstall'
|
||||
'Exit'
|
||||
)
|
||||
|
||||
function Show-Menu {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Draws the main menu and returns the chosen 1-based item, or $null if the operator backed out.
|
||||
|
||||
.DESCRIPTION
|
||||
Arrow keys plus Enter, or a direct number key, drive the selection (logic in Resolve-ElysiumMenuKeyPress);
|
||||
Escape or Q backs out. Falls back to the plain numbered Read-Host prompt when input is redirected
|
||||
or the host has no real console (CI, some IDE terminals) - [Console]::ReadKey throws there.
|
||||
#>
|
||||
param (
|
||||
[string]$Title = 'Elysium Tool Main Menu'
|
||||
)
|
||||
Clear-Host
|
||||
Write-Host "================ $Title ================"
|
||||
Write-Host "1: Download/Update Known-Hashes Database (KHDB)"
|
||||
Write-Host "2: Test Weak AD Passwords"
|
||||
Write-Host "3: Extract and Send Current Hashes for KHDB Update"
|
||||
Write-Host "4: Update Lithnet Password Protection Store"
|
||||
Write-Host "5: Uninstall"
|
||||
Write-Host "6: Exit"
|
||||
|
||||
$useFallback = [System.Console]::IsInputRedirected
|
||||
if (-not $useFallback) {
|
||||
try { [void]$Host.UI.RawUI.CursorPosition } catch { $useFallback = $true }
|
||||
}
|
||||
|
||||
if ($useFallback) {
|
||||
while ($true) {
|
||||
Clear-Host
|
||||
Write-Host "================ $Title ================"
|
||||
for ($i = 0; $i -lt $script:MenuItems.Count; $i++) { Write-Host ("{0}: {1}" -f ($i + 1), $script:MenuItems[$i]) }
|
||||
$choice = Read-Host "Please make a selection"
|
||||
if ($choice -match '^[1-9]$' -and [int]$choice -le $script:MenuItems.Count) { return [int]$choice }
|
||||
if ($choice -match '^(?i:q)$') { return $null }
|
||||
Write-Host "Invalid selection, please try again."
|
||||
}
|
||||
}
|
||||
|
||||
$index = 0
|
||||
$originalCursorVisible = [System.Console]::CursorVisible
|
||||
try {
|
||||
[System.Console]::CursorVisible = $false
|
||||
while ($true) {
|
||||
Clear-Host
|
||||
Write-Host "================ $Title ================"
|
||||
Write-Host "(Up/Down + Enter, or a number. Esc/Q to exit.)"
|
||||
for ($i = 0; $i -lt $script:MenuItems.Count; $i++) {
|
||||
$line = "{0}: {1}" -f ($i + 1), $script:MenuItems[$i]
|
||||
if ($i -eq $index) { Write-Host $line -BackgroundColor DarkCyan -ForegroundColor Black }
|
||||
else { Write-Host $line }
|
||||
}
|
||||
$keyInfo = [System.Console]::ReadKey($true)
|
||||
if ($keyInfo.Key -eq [System.ConsoleKey]::Q) { return $null }
|
||||
$step = Resolve-ElysiumMenuKeyPress -CurrentIndex $index -ItemCount $script:MenuItems.Count -Key $keyInfo.Key
|
||||
if ($step.Cancelled) { return $null }
|
||||
$index = $step.Index
|
||||
if ($step.Committed) { return $index + 1 }
|
||||
}
|
||||
} finally {
|
||||
[System.Console]::CursorVisible = $originalCursorVisible
|
||||
}
|
||||
}
|
||||
|
||||
Start-OrchestratorTranscript -BasePath $PSScriptRoot
|
||||
try {
|
||||
do {
|
||||
Show-Menu
|
||||
$userSelection = Read-Host "Please make a selection"
|
||||
$picked = Show-Menu
|
||||
$userSelection = if ($null -eq $picked) { '6' } else { "$picked" }
|
||||
try {
|
||||
switch ($userSelection) {
|
||||
'1' {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Pure keypress-to-selection logic for Elysium.ps1's interactive console menu. No console I/O here,
|
||||
# so it's testable without a real terminal; Elysium.ps1 does the reading and drawing.
|
||||
|
||||
function Resolve-ElysiumMenuKeyPress {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Turns one console keypress into the menu's next highlighted index and whether it chose or cancelled.
|
||||
|
||||
.DESCRIPTION
|
||||
Up/Down move the highlight with wraparound. Enter commits the highlighted item. A digit key
|
||||
jumps straight to and commits that 1-based item (matching the old numbered prompt), ignored if
|
||||
out of range. Escape cancels. Anything else leaves the index unchanged.
|
||||
#>
|
||||
param(
|
||||
[Parameter(Mandatory)][int]$CurrentIndex,
|
||||
[Parameter(Mandatory)][int]$ItemCount,
|
||||
[Parameter(Mandatory)][System.ConsoleKey]$Key
|
||||
)
|
||||
switch ($Key) {
|
||||
'UpArrow' { return [pscustomobject]@{ Index = (($CurrentIndex - 1 + $ItemCount) % $ItemCount); Committed = $false; Cancelled = $false } }
|
||||
'DownArrow' { return [pscustomobject]@{ Index = (($CurrentIndex + 1) % $ItemCount); Committed = $false; Cancelled = $false } }
|
||||
'Enter' { return [pscustomobject]@{ Index = $CurrentIndex; Committed = $true; Cancelled = $false } }
|
||||
'Escape' { return [pscustomobject]@{ Index = $CurrentIndex; Committed = $false; Cancelled = $true } }
|
||||
default {
|
||||
if ($Key -ge [System.ConsoleKey]::D1 -and $Key -le [System.ConsoleKey]::D9) {
|
||||
$picked = [int]$Key - [int][System.ConsoleKey]::D1
|
||||
if ($picked -lt $ItemCount) {
|
||||
return [pscustomobject]@{ Index = $picked; Committed = $true; Cancelled = $false }
|
||||
}
|
||||
}
|
||||
return [pscustomobject]@{ Index = $CurrentIndex; Committed = $false; Cancelled = $false }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -293,6 +293,42 @@ Describe 'Update-ElysiumLithnetStore' {
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'Interactive menu keypress logic' {
|
||||
It 'moves the highlight with Up/Down and wraps at both ends' {
|
||||
(Resolve-ElysiumMenuKeyPress -CurrentIndex 1 -ItemCount 3 -Key DownArrow).Index | Should -Be 2
|
||||
(Resolve-ElysiumMenuKeyPress -CurrentIndex 2 -ItemCount 3 -Key DownArrow).Index | Should -Be 0
|
||||
(Resolve-ElysiumMenuKeyPress -CurrentIndex 0 -ItemCount 3 -Key UpArrow).Index | Should -Be 2
|
||||
}
|
||||
|
||||
It 'commits the highlighted item on Enter, without moving it' {
|
||||
$step = Resolve-ElysiumMenuKeyPress -CurrentIndex 2 -ItemCount 6 -Key Enter
|
||||
$step.Index | Should -Be 2
|
||||
$step.Committed | Should -BeTrue
|
||||
$step.Cancelled | Should -BeFalse
|
||||
}
|
||||
|
||||
It 'jumps to and commits an in-range digit, and ignores one past the item count' {
|
||||
$step = Resolve-ElysiumMenuKeyPress -CurrentIndex 0 -ItemCount 6 -Key D4
|
||||
$step.Index | Should -Be 3
|
||||
$step.Committed | Should -BeTrue
|
||||
|
||||
$step = Resolve-ElysiumMenuKeyPress -CurrentIndex 0 -ItemCount 3 -Key D4
|
||||
$step.Index | Should -Be 0
|
||||
$step.Committed | Should -BeFalse
|
||||
}
|
||||
|
||||
It 'cancels on Escape and leaves everything else as a no-op' {
|
||||
$step = Resolve-ElysiumMenuKeyPress -CurrentIndex 1 -ItemCount 6 -Key Escape
|
||||
$step.Cancelled | Should -BeTrue
|
||||
$step.Committed | Should -BeFalse
|
||||
|
||||
$step = Resolve-ElysiumMenuKeyPress -CurrentIndex 1 -ItemCount 6 -Key A
|
||||
$step.Index | Should -Be 1
|
||||
$step.Committed | Should -BeFalse
|
||||
$step.Cancelled | Should -BeFalse
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'Password-quality helpers' {
|
||||
It 'selects a domain by name (case-insensitive) or by prompted number, and rejects bad choices' {
|
||||
$domains = Get-DomainDetailsFromSettings -Settings @{
|
||||
|
||||
Reference in New Issue
Block a user