Skip to content

Commit e9f115b

Browse files
committed
Merge remote-tracking branch 'howlettt/callback'
2 parents 359c818 + 076d098 commit e9f115b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

PSMenu/Public/Show-Menu.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ is very useful.
5555
.PARAMETER InitialSelection
5656
Set initial selections if multi-select mode. This is an array of indecies.
5757
58+
.PARAMETER Callback
59+
A function/scriptblock which is called every 10 milliseconds while the menu is shown
60+
5861
.INPUTS
5962
6063
None. You cannot pipe objects to Show-Menu.
@@ -88,7 +91,8 @@ function Show-Menu {
8891
[Switch]$MultiSelect,
8992
[ConsoleColor] $ItemFocusColor = [ConsoleColor]::Green,
9093
[ScriptBlock] $MenuItemFormatter = { Param($M) Format-MenuItemDefault $M },
91-
[Array] $InitialSelection = @()
94+
[Array] $InitialSelection = @(),
95+
[ScriptBlock] $Callback = $null
9296
)
9397

9498
Test-HostSupported
@@ -127,8 +131,11 @@ function Show-Menu {
127131
Break
128132
}
129133

130-
$CurrentPress = Read-VKey
131-
$VKeyCode = $CurrentPress.VirtualKeyCode
134+
$VKeyCode = $null
135+
if ([Console]::KeyAvailable) {
136+
$CurrentPress = Read-VKey
137+
$VKeyCode = $CurrentPress.VirtualKeyCode
138+
}
132139

133140
If (Test-KeySpace $VKeyCode) {
134141
$CurrentSelection = Toggle-Selection $Position $CurrentSelection
@@ -140,6 +147,12 @@ function Show-Menu {
140147
[System.Console]::SetCursorPosition(0, [Console]::CursorTop - $MenuHeight)
141148
& $WriteMenu
142149
}
150+
151+
if ($Callback) {
152+
& $Callback
153+
}
154+
155+
Start-Sleep -Milliseconds 10
143156
}
144157
}
145158
finally {

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ Separators are unselectable items used for visual distinction in the menu.
6767

6868
![Separator support](./docs/separator-support.gif)
6969

70+
## Callback
71+
72+
The Callback option can be used to perform actions while the menu is displayed.
73+
Note: always save & restore the cursor position like in the following example if the host output is changed in the callback.
74+
75+
```powershell
76+
Clear-Host
77+
Write-Host "Current time: $(Get-Date)"
78+
Write-Host ""
79+
Show-Menu @("Option A", "Option B") -Callback {
80+
$lastTop = [Console]::CursorTop
81+
[System.Console]::SetCursorPosition(0, 0)
82+
Write-Host "Current time: $(Get-Date)"
83+
[System.Console]::SetCursorPosition(0, $lastTop)
84+
}
85+
```
86+
87+
![Callback example](./docs/callback.gif)
88+
7089
# Installation
7190

7291
You can install it from the PowerShellGallery using PowerShellGet
@@ -85,6 +104,7 @@ Install-Module PSMenu
85104
- Support for separators
86105
- Esc key quits the menu (`$null` returned)
87106
- Extensively documented
107+
- Perform actions while the menu is displayed (using `-Callback`)
88108

89109
# Documentation
90110

docs/callback.gif

119 KB
Loading

0 commit comments

Comments
 (0)