Skip to content

Commit e877cd2

Browse files
committed
Simplify adding handlers to builtin functions
1 parent 67584a4 commit e877cd2

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

GenerateBuiltinList.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# Generate the ValidateSet attribute for the Builtin parameter
3+
# Set-PSReadlineKeyHandler
4+
5+
$methods = ([PSConsoleUtilities.PSConsoleReadLine] |
6+
Get-Member -Static -Type Method |
7+
Where-Object Definition -match .*Nullable.*).Name
8+
9+
$indent = " " * 8
10+
$prefix = "[ValidateSet("
11+
$suffix = "]"
12+
13+
function GenerateStrings
14+
{
15+
param([Parameter(ValueFromPipeline)]$Item)
16+
17+
begin
18+
{
19+
$count = 0
20+
$comma = ""
21+
$buffer = New-Object System.Text.StringBuilder
22+
}
23+
24+
process
25+
{
26+
$null = $buffer.Append($comma)
27+
$count++
28+
if (($count % 5) -eq 0)
29+
{
30+
# Remove the trailing space
31+
$null = $buffer.Remove($buffer.Length - 1, 1)
32+
$null = $buffer.Append("`n$indent$(' ' * $prefix.Length)")
33+
}
34+
$null = $buffer.Append('"')
35+
$null = $buffer.Append($Item)
36+
$null = $buffer.Append('"')
37+
38+
$comma = ", "
39+
}
40+
41+
end
42+
{
43+
$buffer.ToString()
44+
}
45+
}
46+
47+
"$indent$prefix$($methods | GenerateStrings))$suffix"

PSReadLine/Cmdlets.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,45 @@ public class SetKeyHandlerCommand : PSCmdlet
186186
[ValidateNotNullOrEmpty]
187187
public string[] Chord { get; set; }
188188

189-
[Parameter(Position = 1, Mandatory = true)]
189+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = "Custom")]
190190
[ValidateNotNull]
191-
public Action<ConsoleKeyInfo?, object> Handler { get; set; }
191+
public ScriptBlock Handler { get; set; }
192192

193-
[Parameter(Mandatory = true)]
193+
[Parameter(ParameterSetName = "Custom")]
194194
public string BriefDescription { get; set; }
195195

196-
[Parameter]
196+
[Parameter(ParameterSetName = "Custom")]
197197
public string LongDescription { get; set; }
198198

199+
// ValidateSet attribute is generated by the script GenerateBuiltinList
200+
[ValidateSet("AcceptLine", "AddLine", "BackwardChar", "BackwardDeleteChar",
201+
"BackwardDeleteLine", "BackwardKillLine", "BackwardWord", "BeginningOfHistory", "BeginningOfLine",
202+
"CancelLine", "Complete", "DeleteChar", "DisableDemoMode", "EmacsBackwardWord",
203+
"EmacsForwardWord", "EnableDemoMode", "EndOfHistory", "EndOfLine", "ExchangePointAndMark",
204+
"ForwardChar", "ForwardDeleteLine", "ForwardWord", "HistorySearchBackward", "HistorySearchForward",
205+
"KillBackwardWord", "KillLine", "KillWord", "NextHistory", "Paste",
206+
"PossibleCompletions", "PreviousHistory", "Redo", "RevertLine", "SetKeyHandler",
207+
"SetMark", "TabCompleteNext", "TabCompletePrevious", "Undo", "Yank",
208+
"YankPop")]
209+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = "Builtin")]
210+
public string Builtin { get; set; }
211+
199212
[ExcludeFromCodeCoverage]
200213
protected override void EndProcessing()
201214
{
202-
PSConsoleReadLine.SetKeyHandler(Chord, Handler, BriefDescription, LongDescription);
215+
Action<ConsoleKeyInfo?, object> keyHandler;
216+
if (ParameterSetName.Equals("Builtin"))
217+
{
218+
keyHandler = (Action<ConsoleKeyInfo?, object>)
219+
Delegate.CreateDelegate(typeof (Action<ConsoleKeyInfo?, object>),
220+
typeof (PSConsoleReadLine).GetMethod(Builtin));
221+
BriefDescription = Builtin;
222+
}
223+
else
224+
{
225+
keyHandler = (key, arg) => Handler.Invoke(key, arg);
226+
}
227+
PSConsoleReadLine.SetKeyHandler(Chord, keyHandler, BriefDescription, LongDescription);
203228
}
204229
}
205230

0 commit comments

Comments
 (0)