forked from chrisseroka/ps-menu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps-menu.psm1
136 lines (130 loc) · 3.24 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function DrawMenu {
param ([array]$menuItems, $menuPosition, $Multiselect, $selection, [array]$commands)
$l = $menuItems.length
$wh = (Get-Host).UI.RawUI.WindowSize.Height - 2;
$ww = (Get-Host).UI.RawUI.WindowSize.Width;
$idx = 0;
if($l -gt $wh) {
$l = $wh;
if($menuPosition -gt $l) {
$idx = $menuPosition - $l;
}
}
for ($i = 0;$i -le $l;$i++) {
if ($menuItems[$idx] -ne $null){
$item = $menuItems[$idx]
if ($Multiselect)
{
if ($selection -contains $idx){
$item = '[x] ' + $item
}
else {
$item = '[ ] ' + $item
}
}
#pad the text to ensure no artifacts when scrolling list
$menuText = $item.PadRight($ww - ($item.Length + 2), ' ');
if ($idx -eq $menuPosition) {
Write-Host "> $menuText" -ForegroundColor Green
} else {
Write-Host " $menuText"
}
}
$idx++;
}
if($commands.Count -ne 0) {
Write-Host ($commands -join ", ")
}
}
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, [array]$commands)
$vkeycode = 0
$pos = 0
$cmd = ''
$cmdHash = @{}
$cmdText = @(0) * $commands.Count
if ($commands.Count -ne 0) {
for ($i = 0; $commands.Count -1; $i++){
$c = $commands[$i];
if ($c -eq $null){
break;
}
$k = $c[0];
$cmdHash[$k] = $c;
$cmdText[$i] = '[' + $k + ']' + $c.Substring(1);
}
}
$selection = @()
$cur_pos = [System.Console]::CursorTop
[console]::CursorVisible=$false #prevents cursor flickering
if ($menuItems.Length -gt 0)
{
DrawMenu $menuItems $pos $Multiselect $selection $cmdText
While ($vkeycode -ne 13 -and $vkeycode -ne 27 -and $cmd -eq '') {
$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 ($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 ($commands.Count -ne 0) {
foreach($key in $cmdHash.keys) {
If ($press.Character -eq $key) {
$val = $cmdHash[$key];
$cmd = $val;
break;
}
}
}
If ($vkeycode -ne 27)
{
[System.Console]::SetCursorPosition(0,$cur_pos)
DrawMenu $menuItems $pos $Multiselect $selection $cmdText
}
}
}
else
{
$pos = $null
}
[console]::CursorVisible=$true
if ($ReturnIndex -eq $false -and $pos -ne $null)
{
if ($Multiselect){
return get-SelectedValue -cmd $cmd -value $menuItems[$selection]
}
else {
return get-SelectedValue -cmd $cmd -value $menuItems[$pos]
}
}
else
{
if ($Multiselect){
return get-SelectedValue -cmd $cmd -value $selection
}
else {
return get-SelectedValue -cmd $cmd -value $pos
}
}
}
function get-SelectedValue {
Param($cmd, $value)
if($cmd -ne '') {
return @($cmd, @($value));
}else {
return $value;
}
}