Skip to content

Commit b3d7d59

Browse files
committed
Initial Commit
1 parent ee5679d commit b3d7d59

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
# InteractivePSMenu
22
Generates a dynamic console menu featuring a list of options, allowing users to navigate and select choices using their keyboard arrows.
3+
4+
## Description
5+
The Show-Menu function is used to display a dynamic menu in the console. It takes a title and a list of options as parameters. The title is optional and defaults to "Please make a selection...". The list of options is mandatory. The function will display the title in green, followed by the list of options. The user can then make a selection from the options provided.
6+
7+
## Usage
8+
```
9+
# This example shows how to use the Show-Menu function to display a menu with a custom title and three options.
10+
$MenuData += [PSCustomObject]@{Id = 1; DisplayName = "Menu Option 1"}, `
11+
[PSCustomObject]@{Id = 2; DisplayName = "Menu Option 2"}, `
12+
[PSCustomObject]@{Id = 3; DisplayName = "Menu Option 3"}
13+
Show-Menu -DynamicMenuTitle "Main Menu" -DynamicMenuList $MenuData
14+
```
15+
![Show-Menu](Show-Menu.png)

Show-Menu.png

10.3 KB
Loading

Show-Menu.ps1

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
function Show-Menu {
2+
<#
3+
.SYNOPSIS
4+
Generates a dynamic console menu featuring a list of options, allowing users to
5+
navigate and select choices using their keyboard arrows.
6+
.DESCRIPTION
7+
The Show-Menu function is used to display a dynamic menu in the console. It takes
8+
a title and a list of options as parameters. The title is optional and defaults to
9+
"Please make a selection...". The list of options is mandatory. The function will
10+
display the title in green, followed by the list of options. The user can then make
11+
a selection from the options provided.
12+
.EXAMPLE
13+
$MenuData += [PSCustomObject]@{Id = 1; DisplayName = "Menu Option 1"}, `
14+
[PSCustomObject]@{Id = 2; DisplayName = "Menu Option 2"}, `
15+
[PSCustomObject]@{Id = 3; DisplayName = "Menu Option 3"}
16+
Show-Menu -DynamicMenuTitle "Main Menu" -DynamicMenuList $MenuData
17+
This example shows how to use the Show-Menu function to display a menu with a custom title and three options.
18+
.NOTES
19+
Version: 20231115.01
20+
Author: Ryan Dunton https://github.com/ryandunton
21+
#>
22+
23+
[CmdletBinding()]
24+
param (
25+
[Parameter(Mandatory=$false)]
26+
[string]
27+
$DynamicMenuTitle = "Please make a selection...",
28+
[Parameter(Mandatory=$true)]
29+
[array]
30+
$DynamicMenuList
31+
)
32+
33+
begin {
34+
Write-Host "[+] $DynamicMenuTitle" -ForegroundColor Green
35+
# Create space for the menu
36+
$i=0
37+
while ($i -lt $DynamicMenuList.Count) {
38+
$i++
39+
Write-Host ""
40+
}
41+
}
42+
43+
process {
44+
# Set initial selection index
45+
$SelectedValueIndex = 0
46+
# Display the menu and handle user input
47+
do {
48+
# Move cursor to top of menu area
49+
[Console]::SetCursorPosition(0, [Console]::CursorTop - $DynamicMenuList.Count)
50+
for ($i = 0; $i -lt $DynamicMenuList.Count; $i++) {
51+
if ($i -eq $SelectedValueIndex) {
52+
Write-Host "[>] $($DynamicMenuList[$i].DisplayName)" -NoNewline
53+
} else {
54+
Write-Host "[ ] $($DynamicMenuList[$i].DisplayName)" -NoNewline
55+
}
56+
#Clear any extra characters from previous lines
57+
$SpacesToClear = [Math]::Max(0, ($DynamicMenuList[0].Length - $DynamicMenuList[$i].Length))
58+
Write-Host (" " * $SpacesToClear) -NoNewline
59+
Write-Host ""
60+
}
61+
# Get user input
62+
$KeyInfo = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown')
63+
# Process arrow key input
64+
switch ($KeyInfo.VirtualKeyCode) {
65+
38 { # Up arrow
66+
$SelectedValueIndex = [Math]::Max(0, $SelectedValueIndex - 1)
67+
}
68+
40 { # Down arrow
69+
$SelectedValueIndex = [Math]::Min($DynamicMenuList.Count - 1, $SelectedValueIndex + 1)
70+
}
71+
}
72+
} while ($KeyInfo.VirtualKeyCode -ne 13) # Enter key
73+
$SelectedValue = $DynamicMenuList[$SelectedValueIndex]
74+
}
75+
76+
end {
77+
return $SelectedValue.Id
78+
}
79+
}
80+
81+
$MenuData += [PSCustomObject]@{Id = 1; DisplayName = "Menu Option 1"}, `
82+
[PSCustomObject]@{Id = 2; DisplayName = "Menu Option 2"}, `
83+
[PSCustomObject]@{Id = 3; DisplayName = "Menu Option 3"}
84+
$SelectedItem = Show-Menu -DynamicMenuTitle "Main Menu" -DynamicMenuList $MenuData

0 commit comments

Comments
 (0)