Skip to content

Commit 6352072

Browse files
committed
Remember script block for custom key bindings
For various reasons, we may need to know that a key binding came from a script block so that is now saved in the key binding.
1 parent a4d4cef commit 6352072

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,20 +513,19 @@ public class SetPSReadlineKeyHandlerCommand : PSCmdlet, IDynamicParameters
513513
[ExcludeFromCodeCoverage]
514514
protected override void EndProcessing()
515515
{
516-
Action<ConsoleKeyInfo?, object> keyHandler;
517516
if (ParameterSetName.Equals(FunctionParameterSet))
518517
{
519518
var function = (string)_dynamicParameters.Value[FunctionParameter].Value;
520-
keyHandler = (Action<ConsoleKeyInfo?, object>)
519+
var keyHandler = (Action<ConsoleKeyInfo?, object>)
521520
Delegate.CreateDelegate(typeof (Action<ConsoleKeyInfo?, object>),
522-
typeof (PSConsoleReadLine).GetMethod(function));
521+
typeof (PSConsoleReadLine).GetMethod(function));
523522
BriefDescription = function;
523+
PSConsoleReadLine.SetKeyHandler(Chord, keyHandler, BriefDescription, Description);
524524
}
525525
else
526526
{
527-
keyHandler = (key, arg) => ScriptBlock.Invoke(key, arg);
527+
PSConsoleReadLine.SetKeyHandler(Chord, ScriptBlock, BriefDescription, Description);
528528
}
529-
PSConsoleReadLine.SetKeyHandler(Chord, keyHandler, BriefDescription, Description);
530529
}
531530

532531
private readonly Lazy<RuntimeDefinedParameterDictionary> _dynamicParameters =

PSReadLine/KeyBindings.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Management.Automation;
34
using System.Text;
45

56
namespace PSConsoleUtilities
@@ -43,6 +44,7 @@ class KeyHandler
4344
public Action<ConsoleKeyInfo?, object> Action;
4445
public string BriefDescription;
4546
public string LongDescription;
47+
public ScriptBlock ScriptBlock;
4648
}
4749

4850
internal class ConsoleKeyInfoComparer : IEqualityComparer<ConsoleKeyInfo>
@@ -58,7 +60,7 @@ public int GetHashCode(ConsoleKeyInfo obj)
5860
}
5961
}
6062

61-
static KeyHandler MakeKeyHandler(Action<ConsoleKeyInfo?, object> action, string briefDescription, string longDescription = null)
63+
static KeyHandler MakeKeyHandler(Action<ConsoleKeyInfo?, object> action, string briefDescription, string longDescription = null, ScriptBlock scriptBlock = null)
6264
{
6365
if (string.IsNullOrWhiteSpace(longDescription))
6466
longDescription = PSReadLineResources.ResourceManager.GetString(briefDescription + "Description");
@@ -67,7 +69,8 @@ static KeyHandler MakeKeyHandler(Action<ConsoleKeyInfo?, object> action, string
6769
{
6870
Action = action,
6971
BriefDescription = briefDescription,
70-
LongDescription = longDescription
72+
LongDescription = longDescription,
73+
ScriptBlock = scriptBlock,
7174
};
7275
}
7376

PSReadLine/Options.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Management.Automation;
34
using System.Reflection;
45
using System.Threading;
56

@@ -155,7 +156,7 @@ private void SetOptionsInternal(SetPSReadlineOption options)
155156
}
156157
}
157158

158-
private void SetKeyHandlerInternal(string[] keys, Action<ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription)
159+
private void SetKeyHandlerInternal(string[] keys, Action<ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription, ScriptBlock scriptBlock)
159160
{
160161
foreach (var key in keys)
161162
{
@@ -196,12 +197,22 @@ public static PSConsoleReadlineOptions GetOptions()
196197
return _singleton._options;
197198
}
198199

200+
/// <summary>
201+
/// Helper function for the Set-PSReadlineKeyHandler cmdlet.
202+
/// </summary>
203+
public static void SetKeyHandler(string[] key, ScriptBlock scriptBlock, string briefDescription, string longDescription)
204+
{
205+
Action<ConsoleKeyInfo?, object> handler =
206+
(k, arg) => scriptBlock.Invoke(k, arg);
207+
_singleton.SetKeyHandlerInternal(key, handler, briefDescription, longDescription, scriptBlock);
208+
}
209+
199210
/// <summary>
200211
/// Helper function for the Set-PSReadlineKeyHandler cmdlet.
201212
/// </summary>
202213
public static void SetKeyHandler(string[] key, Action<ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription)
203214
{
204-
_singleton.SetKeyHandlerInternal(key, handler, briefDescription, longDescription);
215+
_singleton.SetKeyHandlerInternal(key, handler, briefDescription, longDescription, null);
205216
}
206217

207218
/// <summary>

0 commit comments

Comments
 (0)