forked from drewgreenwell/ps-menu
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTest-Module.ps1
138 lines (106 loc) · 3.44 KB
/
Test-Module.ps1
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
137
138
param([Switch]$NoNewShell, [Switch]$SmokeTest, [String]$EncCmd, [ScriptBlock] $AutoExec, [Switch]$Exit)
$MyPath = $PSScriptRoot
if ($NoNewShell -eq $false) {
Write-Host "Opening new shell..."
[string[]]$PsArgs = @()
if (!$Exit) {
$PsArgs += @("-NoExit")
}
$PsArgs += @("-File", "$MyPath\Test-Module.ps1", "-NoNewShell")
if ($SmokeTest) {
$PsArgs += @("-SmokeTest")
}
if ($AutoExec) {
$EncCmd = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($AutoExec.ToString()))
$PsArgs += @("-EncCmd", $EncCmd)
}
$Cmd = "powershell"
if (!$(Get-Command $Cmd -ErrorAction SilentlyContinue)) {
$Cmd = "pwsh"
}
Write-Host "$Cmd $PsArgs"
& $Cmd $PsArgs
Exit $LASTEXITCODE
}
Clear-Host
Import-Module .\PSMenu\PSMenu.psm1 -Verbose
function Test-MenuWithStringOptions() {
$Opts = @(
"Cheesecake",
"Fries",
"Yoghurt"
)
$Chosen = Show-Menu -MenuItems $Opts
Write-Host "You chose: $Chosen"
}
class MyMenuOption {
[String]$DisplayName
[ScriptBlock]$ExecuteCallback
[String]ToString() {
Return $This.DisplayName
}
[Void]Execute() {
& $This.ExecuteCallback
}
}
function New-MenuItem([String]$DisplayName, [ScriptBlock]$ExecuteCallback) {
$MenuItem = [MyMenuOption]::new()
$MenuItem.DisplayName = $DisplayName
$MenuItem.ExecuteCallback = $ExecuteCallback
Return $MenuItem
}
function Get-TestMenuItems() {
Return @(
$(New-MenuItem -DisplayName "Say Hello" -ExecuteCallback { Write-Host "Hello!" }),
$(New-MenuItem -DisplayName "Say Bye!" -ExecuteCallback { Write-Host "Bye!" })
)
}
function Get-TestMenuItemsByCount($Count) {
for ($Index = 1; $Index -le $Count; $Index++) {
New-MenuItem -DisplayName "Test Menuitem $Index" -ExecuteCallback { Write-Host "This was #$Index" }.GetNewClosure()
}
}
function Test-MenuWithSeparator() {
$MenuItems = @("Option A", "Option B", $(Get-MenuSeparator), "Quit")
Show-Menu $MenuItems
}
function Test-MenuWithClassOptions() {
$Opts = Get-TestMenuItems
$Chosen = Show-Menu -MenuItems $Opts
Write-Host "You chose: $Chosen"
Write-Host ""
$Chosen.Execute()
Write-Host ""
}
function Test-ScrollingMenu([int]$SurplusItems = 5, [Switch]$MultiSelect) {
$ItemsToGenerate = [Console]::WindowHeight + $SurplusItems
Write-Host "Generating $ItemsToGenerate menu items"
$MenuItems = Get-TestMenuItemsByCount -Count $ItemsToGenerate
Show-Menu -MenuItems $MenuItems -MultiSelect:$MultiSelect
}
function Test-MenuWithCustomFormatter() {
Show-Menu -MenuItems $(Get-NetAdapter) -MenuItemFormatter { Param($M) $M.Name }
}
function Test-MenuWithCallback() {
Clear-Host
Write-Host "Current time: $(Get-Date)"
Write-Host ""
Show-Menu @("Option A", "Option B") -Callback {
$lastTop = [Console]::CursorTop
[System.Console]::SetCursorPosition(0, 0)
Write-Host "Current time: $(Get-Date)"
[System.Console]::SetCursorPosition(0, $lastTop)
}
}
if ($SmokeTest) {
Write-Host "Test-MenuWithClassOptions" -ForegroundColor Cyan
Test-MenuWithClassOptions
Write-Host "Test-MenuWithCustomFormatter" -ForegroundColor Cyan
Test-MenuWithCustomFormatter
Exit 0
}
if ($EncCmd) {
$Cmd = [System.Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($EncCmd))
Write-Host "Executing: $Cmd"
Invoke-Expression -Command $Cmd
}