-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.psm1
More file actions
480 lines (383 loc) · 17 KB
/
Input.psm1
File metadata and controls
480 lines (383 loc) · 17 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
Using module .\Utils.psm1
Using module .\Logging.psm1
Using module .\Scope.psm1
Using module .\Assert.psm1
Using module @{
ModuleName = 'PSReadLine';
ModuleVersion = '2.3.2';
}
$Script:Validations = @{
Email = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$';
}
[HashTable]$Script:WriteStyle = @{
PSColour = 'DarkCyan';
PSPrefix = '▶';
ShouldWrite = $true;
};
function Clear-HostLight (
[Parameter(Position = 1)]
[int32]$Count = 1
) {
$CurrentLine = $Host.UI.RawUI.CursorPosition.Y
$ConsoleWidth = $Host.UI.RawUI.BufferSize.Width
$i = 1
for ($i; $i -le $Count; $i++) {
[Console]::SetCursorPosition(0, ($CurrentLine - $i))
[Console]::Write("{0,-$ConsoleWidth}" -f ' ')
}
[Console]::SetCursorPosition(0, ($CurrentLine - $Count))
}
function Register-CustomReadLineHandlers([Switch]$DontSaveInputs) {
[Object]$Local:PreviousEnterFunction = (Get-PSReadLineKeyHandler -Chord Enter).Function;
[Boolean]$Script:PressedEnter = $False;
Set-PSReadLineKeyHandler -Chord Enter -ScriptBlock {
Param([System.ConsoleKeyInfo]$Key, $Arg)
$Script:PressedEnter = $True;
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine($Key, $Arg);
};
[Object]$Local:PreviousCtrlCFunction = (Get-PSReadLineKeyHandler -Chord Ctrl+c).Function;
[Boolean]$Script:ShouldAbort = $False;
Set-PSReadLineKeyHandler -Chord Ctrl+c -ScriptBlock {
Param([System.ConsoleKeyInfo]$Key, $Arg)
Invoke-Info 'Ctrl+C was pressed, aborting...';
$Script:ShouldAbort = $True;
[Microsoft.PowerShell.PSConsoleReadLine]::CancelLine($Key, $Arg);
};
[Int]$Script:ExtraLines = 0;
[ScriptBlock]$Private:EnterScriptBlock = {
Param([System.ConsoleKeyInfo]$Key, $Arg)
$Script:ExtraLines++;
[Microsoft.PowerShell.PSConsoleReadLine]::AddLine($Key, $Arg);
};
[Object]$Local:PreviousCtrlEnterFunction = (Get-PSReadLineKeyHandler -Chord Ctrl+Enter).Function;
[Object]$Local:PreviousShiftEnterFunction = (Get-PSReadLineKeyHandler -Chord Shift+Enter).Function;
Set-PSReadLineKeyHandler -Chord Ctrl+Enter -ScriptBlock $Private:EnterScriptBlock;
Set-PSReadLineKeyHandler -Chord Shift+Enter -ScriptBlock $Private:EnterScriptBlock;
[System.Func[String, Object]]$Local:HistoryHandler = (Get-PSReadLineOption).AddToHistoryHandler;
if ($DontSaveInputs) {
Set-PSReadLineOption -AddToHistoryHandler {
Param([String]$Line)
$False;
}
}
return @{
Enter = $Local:PreviousEnterFunction;
CtrlC = $Local:PreviousCtrlCFunction;
CtrlShift = $Local:PreviousCtrlEnterFunction;
ShiftEnter = $Local:PreviousShiftEnterFunction;
HistoryHandler = $Local:HistoryHandler;
}
}
function Unregister-CustomReadLineHandlers([HashTable]$PreviousHandlers) {
Set-PSReadLineKeyHandler -Chord Enter -Function $PreviousHandlers.Enter;
Set-PSReadLineKeyHandler -Chord Ctrl+c -Function $PreviousHandlers.CtrlC;
Set-PSReadLineKeyHandler -Chord Ctrl+Enter -Function $PreviousHandlers.CtrlShift;
Set-PSReadLineKeyHandler -Chord Shift+Enter -Function $PreviousHandlers.ShiftEnter;
Set-PSReadLineOption -AddToHistoryHandler $PreviousHandlers.HistoryHandler;
}
<#
.SYNOPSIS
Prompts the user for input.
.DESCRIPTION
Prompts the user for input, with the ability to validate the input and return it as a SecureString.
If the input is invalid, the user will be prompted to try again.
.PARAMETER Title
The title of the prompt.
.PARAMETER Question
The question to ask the user.
.PARAMETER Validate
A script block to validate the user input.
The script block is invoked with the raw string the user entered.
.PARAMETER AsSecureString
If set, the user input will be returned as a SecureString.
This setting implies DontSaveInputs.
.PARAMETER DontSaveInputs
If set, the user input will not be saved in the history.
.PARAMETER SaveInputAsUniqueHistory
If set, the user input will be saved as a unique history item.
This will result in a history file being made which is only avaialble when this exact
Get-UserInput function is called, this is done by combining the title and question and hashing it.
When this is set, the input will not be saved to normal history and the user will only be able to
use the up and down arrow keys to navigate through the history of this specific prompt.
.EXAMPLE
```
Get-UserInput `
-Title 'Enter your name' `
-Question 'What is your name?' `
-Validate { Param([String]$Input) $Input.Length -gt 0; }
```
This will prompt the user with the title "Enter your name" and the question "What is your name?".
.EXAMPLE
```
Get-UserInput `
-Title 'Enter your password' `
-Question 'What is your password?' `
-AsSecureString
```
This will prompt the user with the title "Enter your password" and the question "What is your password?".
The user input will be returned as a SecureString.
#>
function Get-UserInput {
Param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Title,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Alias('Prompt')]
[String]$Question,
[Parameter(HelpMessage = 'Validation script block to validate the user input.')]
[ValidateNotNullOrEmpty()]
[ScriptBlock]$Validate,
[Parameter()]
[Switch]$AsSecureString,
[Parameter()]
[Switch]$DontSaveInputs = $AsSecureString,
[Parameter()]
[Switch]$SaveInputAsUniqueHistory,
[Parameter()]
[Switch]$AllowEmpty
)
begin { Enter-Scope; }
end { Exit-Scope -ReturnValue $Local:UserInput; }
process {
Invoke-Write @Script:WriteStyle -PSMessage $Title;
Invoke-Write @Script:WriteStyle -PSMessage $Question;
[HashTable]$Local:PreviousFunctions = Register-CustomReadLineHandlers -DontSaveInputs:$DontSaveInputs;
if ($SaveInputAsUniqueHistory) {
$Local:PreviousHistorySavePath = (Get-PSReadLineOption).HistorySavePath;
$Local:Hash = [System.Security.Cryptography.SHA256]::Create().ComputeHash([System.Text.Encoding]::UTF8.GetBytes("$Title$Question"));
$Local:HashString = [System.BitConverter]::ToString($Local:Hash).Replace('-', '');
$Local:HashString = $Local:HashString.Substring(0, [Math]::Min(8, $Local:HashString.Length));
$Private:FileLocation = "$env:APPDATA\Roaming\Microsoft\Windows\PowerShell\PSReadLine\UniqueHistory-$Local:HashString.txt";
Set-PSReadLineOption -HistorySavePath $Private:FileLocation;
}
$Host.UI.RawUI.FlushInputBuffer();
Clear-HostLight -Count 0; # Clear the line buffer to get rid of the >> prompt.
Write-Host "`r>> " -NoNewline;
do {
[String]$Local:UserInput = ([Microsoft.PowerShell.PSConsoleReadLine]::ReadLine($Host.Runspace, $ExecutionContext, $?)).Trim();
if ((-not $AllowEmpty -and -not $Local:UserInput) -or ($Validate -and (-not $Validate.InvokeWithContext($null, [PSVariable]::new('_', $Local:UserInput))))) {
$Local:ClearLines = if ($Local:FailedAtLeastOnce -and $Script:PressedEnter) {
$Private:Value = $Script:ExtraLines + 2;
$Script:ExtraLines = 0;
$Private:Value;
} else {
1
};
Clear-HostLight -Count $Local:ClearLines;
Invoke-Write @Script:WriteStyle -PSMessage 'Invalid input, please try again...';
$Host.UI.Write('>> ');
$Local:FailedAtLeastOnce = $true;
$Script:PressedEnter = $false;
} else {
Clear-HostLight -Count 1;
break;
}
} while (-not $Script:ShouldAbort);
Unregister-CustomReadLineHandlers -PreviousHandlers $Local:PreviousFunctions;
if ($Script:ShouldAbort) {
throw [System.Management.Automation.PipelineStoppedException]::new();
}
if ($SaveInputAsUniqueHistory) {
Set-PSReadLineOption -HistorySavePath $Local:PreviousHistorySavePath;
}
if ($AllowEmpty -and -not $Local:UserInput) {
return $null;
}
if ($AsSecureString) {
[SecureString]$Local:UserInput = ConvertTo-SecureString -String $Local:UserInput -AsPlainText -Force;
}
return $Local:UserInput;
}
}
function Get-UserConfirmation {
Param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Title,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Question,
[Parameter()]
[ValidateNotNullOrEmpty()]
[Boolean]$DefaultChoice
)
$Local:DefaultChoice = if ($null -eq $DefaultChoice) { 1 } elseif ($DefaultChoice) { 0 } else { 1 };
$Local:Result = Get-UserSelection -Title $Title -Question $Question -Choices @('Yes', 'No') -DefaultChoice $Local:DefaultChoice;
switch ($Local:Result) {
'Yes' { $true }
'No' { $false }
}
}
function Get-UserSelection {
Param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Title,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Question,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Alias('Items')]
[Array]$Choices,
[Parameter()]
[Int]$DefaultChoice = 0,
[Parameter()]
[Switch]$AllowNone,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ScriptBlock]$FormatChoice = { Param([String]$Choice) $Choice.ToString(); }
)
begin {
Enter-Scope;
[Console]::TreatControlCAsInput = $True;
#region Setup PSReadLine Key Handlers
[HashTable]$Local:PreviousFunctions = Register-CustomReadLineHandlers -DontSaveInputs;
$Local:PreviousTabFunction = (Get-PSReadLineKeyHandler -Chord Tab).Function;
if (-not $Local:PreviousTabFunction) {
$Local:PreviousTabFunction = 'TabCompleteNext';
}
$Local:PreviousShiftTabFunction = (Get-PSReadLineKeyHandler -Chord Shift+Tab).Function;
if (-not $Local:PreviousShiftTabFunction) {
$Local:PreviousShiftTabFunction = 'TabCompletePrevious';
}
[String[]]$Script:ChoicesList = $Choices | ForEach-Object {
$Formatted = $FormatChoice.InvokeReturnAsIs($_);
if (-not $Formatted -or $Formatted -eq '') {
throw [System.ArgumentException]::new('FormatChoice script block must return a non-empty string.');
}
$Formatted;
};
Set-PSReadLineKeyHandler -Chord Tab -ScriptBlock {
Param([System.ConsoleKeyInfo]$Key, $Arg)
$Line = $null;
$Cursor = $null;
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$Line, [ref]$Cursor);
$MatchingInput = $Line.Substring(0, $Cursor);
if ($Script:PreviewingChoices -and $Line -eq $Script:PreviewingInput) {
if ($Script:ChoicesGoneThrough -eq $Script:MatchedChoices.Count - 1) {
$Script:ChoicesGoneThrough = 0;
} else {
$Script:ChoicesGoneThrough++;
}
$Script:PreviewingInput = $Script:MatchedChoices[$Script:ChoicesGoneThrough];
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $MatchingInput.Length, $Script:PreviewingInput);
return;
}
$Script:PreviewingChoices = $false;
$Script:PreviewingInput = $null;
$Script:ChoicesGoneThrough = 0;
$Script:MatchedChoices = $Script:ChoicesList | Where-Object { $_ -like "$MatchingInput*" };
if ($Script:MatchedChoices.Count -gt 1) {
$Script:PreviewingChoices = $true;
$Script:PreviewingInput = $Script:MatchedChoices[$Script:ChoicesGoneThrough];
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $MatchingInput.Length, $Script:MatchedChoices[$Script:ChoicesGoneThrough]);
} elseif ($Script:MatchedChoices.Count -eq 1) {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $MatchingInput.Length, $Script:MatchedChoices);
}
}
Set-PSReadLineKeyHandler -Chord Shift+Tab -ScriptBlock {
Param([System.ConsoleKeyInfo]$Key, $Arg)
$Line = $null;
$Cursor = $null;
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$Line, [ref]$Cursor);
$MatchingInput = $Line.Substring(0, $Cursor);
if ($Script:PreviewingChoices -and $Line -eq $Script:PreviewingInput) {
if ($Script:ChoicesGoneThrough -eq 0) {
$Script:ChoicesGoneThrough = $Script:MatchedChoices.Count - 1;
} else {
$Script:ChoicesGoneThrough--;
}
$Script:PreviewingInput = $Script:MatchedChoices[$Script:ChoicesGoneThrough];
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $MatchingInput.Length, $Script:PreviewingInput);
return;
}
};
#endregion
}
process {
Invoke-Write @Script:WriteStyle -PSMessage $Title;
Invoke-Write @Script:WriteStyle -PSMessage $Question;
[Boolean]$Local:FirstRun = $true;
$Host.UI.RawUI.FlushInputBuffer();
Clear-HostLight -Count 0; # Clear the line buffer to get rid of the >> prompt.
Invoke-Write @Script:WriteStyle -PSMessage "Enter one of the following: $($ChoicesList -join ', ')";
Write-Host '>> ' -NoNewline;
if ($null -ne $DefaultChoice) {
Write-Host "$($PSStyle.Foreground.FromRgb(40, 44, 52))$($ChoicesList[$DefaultChoice])" -NoNewline;
}
Write-Host "`r>> " -NoNewline;
do {
if ([Console]::KeyAvailable) {
$Key = [Console]::ReadKey($True);
if ($Key.Modifiers -eq 'Control' -and $Key.Key -eq 'C') {
Invoke-Info 'Ctrl+C was pressed, aborting...';
$Script:ShouldAbort = $True;
break;
}
}
$Local:Selection = ([Microsoft.PowerShell.PSConsoleReadLine]::ReadLine($Host.Runspace, $ExecutionContext, $?)).Trim();
if (-not $Local:Selection -and $Local:FirstRun -and $null -ne $DefaultChoice) {
$Local:Selection = $Choices[$DefaultChoice];
Clear-HostLight -Count 1;
} elseif ($Local:Selection -notin $ChoicesList) {
$Local:ClearLines = if ($Local:FailedAtLeastOnce -and $Script:PressedEnter) { 2 } else { 1 };
Clear-HostLight -Count $Local:ClearLines;
Invoke-Write @Script:WriteStyle -PSMessage 'Invalid selection, please try again...';
$Host.UI.Write('>> ');
$Local:FailedAtLeastOnce = $true;
$Script:PressedEnter = $false;
}
$Local:FirstRun = $false;
} while ($Local:Selection -notin $ChoicesList -and -not $Script:ShouldAbort);
if ($Script:ShouldAbort) {
if (-not $AllowNone) {
throw [System.Management.Automation.PipelineStoppedException]::new();
} else {
return $null;
}
}
return $Choices[$ChoicesList.IndexOf($Local:Selection)];
}
end {
Exit-Scope -ReturnValue $Local:Selection;
if ($Local:PreviousTabFunction -ne 'CustomAction') { Set-PSReadLineKeyHandler -Chord Tab -Function $Local:PreviousTabFunction; }
if ($Local:PreviousShiftTabFunction -ne 'CustomAction') { Set-PSReadLineKeyHandler -Chord Shift+Tab -Function $Local:PreviousShiftTabFunction; }
if ($Local:PreviousFunctions) { Unregister-CustomReadLineHandlers -PreviousHandlers $Local:PreviousFunctions; }
[Console]::TreatControlCAsInput = $False;
}
}
function Get-PopupSelection {
Param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$Title = 'Select a(n) Item',
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Object[]]$Items,
[Parameter()]
[Switch]$AllowNone,
[Parameter()]
[Switch]$AllowMultiple
)
$ViewArgs = @{
Title = $Title;
};
if ($AllowMultiple) {
$ViewArgs.OutputMode = 'Multiple';
} else {
$ViewArgs.PassThru = $True;
}
$Local:Selection;
while (-not $Local:Selection) {
$Local:Selection = $Items | Out-GridView @ViewArgs;
if ((-not $AllowNone) -and (-not $Local:Selection)) {
Invoke-Info 'No Item was selected, re-running selection...';
}
}
$Local:Selection -and -not $AllowNone | Assert-NotNull -Message "Failed to select a $ItemName.";
return $Local:Selection;
}
Export-ModuleMember -Function Get-UserInput, Get-UserConfirmation, Get-UserSelection, Get-PopupSelection -Variable Validations;