-
Notifications
You must be signed in to change notification settings - Fork 42
/
ps-menu.psm1
95 lines (91 loc) · 2.22 KB
/
ps-menu.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function DrawMenu {
param ($menuItems, $menuPosition, $Multiselect, $selection)
$l = $menuItems.length
for ($i = 0; $i -le $l;$i++) {
if ($menuItems[$i] -ne $null){
$item = $menuItems[$i]
if ($Multiselect)
{
if ($selection -contains $i){
$item = '[x] ' + $item
}
else {
$item = '[ ] ' + $item
}
}
if ($i -eq $menuPosition) {
Write-Host "> $($item)" -ForegroundColor Green
} else {
Write-Host " $($item)"
}
}
}
}
function Toggle-Selection {
param ($pos, [array]$selection)
if ($selection -contains $pos){
$result = $selection | where {$_ -ne $pos}
}
else {
$selection += $pos
$result = $selection
}
$result
}
function Menu {
param ([array]$menuItems, [switch]$ReturnIndex=$false, [switch]$Multiselect)
$vkeycode = 0
$pos = 0
$selection = @()
if ($menuItems.Length -gt 0)
{
try {
$startPos = [System.Console]::CursorTop
[console]::CursorVisible=$false #prevents cursor flickering
DrawMenu $menuItems $pos $Multiselect $selection
While ($vkeycode -ne 13 -and $vkeycode -ne 27) {
$press = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown")
$vkeycode = $press.virtualkeycode
If ($vkeycode -eq 38 -or $press.Character -eq 'k') {$pos--}
If ($vkeycode -eq 40 -or $press.Character -eq 'j') {$pos++}
If ($vkeycode -eq 36) { $pos = 0 }
If ($vkeycode -eq 35) { $pos = $menuItems.length - 1 }
If ($press.Character -eq ' ') { $selection = Toggle-Selection $pos $selection }
if ($pos -lt 0) {$pos = 0}
If ($vkeycode -eq 27) {$pos = $null }
if ($pos -ge $menuItems.length) {$pos = $menuItems.length -1}
if ($vkeycode -ne 27)
{
$startPos = [System.Console]::CursorTop - $menuItems.Length
[System.Console]::SetCursorPosition(0, $startPos)
DrawMenu $menuItems $pos $Multiselect $selection
}
}
}
finally {
[System.Console]::SetCursorPosition(0, $startPos + $menuItems.Length)
[console]::CursorVisible = $true
}
}
else {
$pos = $null
}
if ($ReturnIndex -eq $false -and $pos -ne $null)
{
if ($Multiselect){
return $menuItems[$selection]
}
else {
return $menuItems[$pos]
}
}
else
{
if ($Multiselect){
return $selection
}
else {
return $pos
}
}
}