Skip to content

Commit 1e2dee3

Browse files
committed
Unbound functions in Get-PSReadlineKeyHandler
Get-PSReadlineKeyHandler will now return unbound functions by default. A couple of parameters were added so you can get just the bound/unbound parameters if so desired. OutputType was also added to Get-PSReadlineKeyHandler. The OutputType was cleaned up a bit - BriefDescription was renamed to Function and LongDescription was renamed to ShortDescription.
1 parent bfaf374 commit 1e2dee3

File tree

4 files changed

+164
-77
lines changed

4 files changed

+164
-77
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,14 @@ public class SetKeyHandlerCommand : PSCmdlet
439439
public string BriefDescription { get; set; }
440440

441441
[Parameter(ParameterSetName = "ScriptBlock")]
442-
public string LongDescription { get; set; }
442+
[Alias("LongDescription")] // Alias to stay comptible with previous releases
443+
public string Description { get; set; }
443444

444445
// ValidateSet attribute is generated by the script GenerateBuiltinList
446+
// It would be cleaner to use Function in the ScriptBlock parameter set (instead of BriefDescription)
447+
// but then we couldn't use ValidateSet because ValidateSet only makes sense in the Function
448+
// parameter set. A custom Validate attribute would be better, but I don't know of a way
449+
// for the attribute to determine the parameter set.
445450
[ValidateSet("Abort", "AcceptAndGetNext", "AcceptLine", "AddLine", "BackwardChar",
446451
"BackwardDeleteChar", "BackwardDeleteLine", "BackwardKillLine", "BackwardKillWord", "BackwardWord",
447452
"BeginningOfHistory", "BeginningOfLine", "CancelLine", "CharacterSearch", "CharacterSearchBackward",
@@ -471,17 +476,51 @@ protected override void EndProcessing()
471476
{
472477
keyHandler = (key, arg) => ScriptBlock.Invoke(key, arg);
473478
}
474-
PSConsoleReadLine.SetKeyHandler(Chord, keyHandler, BriefDescription, LongDescription);
479+
PSConsoleReadLine.SetKeyHandler(Chord, keyHandler, BriefDescription, Description);
475480
}
476481
}
477482

478483
[Cmdlet("Get", "PSReadlineKeyHandler")]
484+
[OutputType(typeof(KeyHandler))]
479485
public class GetKeyHandlerCommand : PSCmdlet
480486
{
487+
[Parameter]
488+
public SwitchParameter Bound
489+
{
490+
get { return _bound.GetValueOrDefault(); }
491+
set { _bound = value; }
492+
}
493+
private SwitchParameter? _bound;
494+
495+
[Parameter]
496+
public SwitchParameter Unbound
497+
{
498+
get { return _unbound.GetValueOrDefault(); }
499+
set { _unbound = value; }
500+
}
501+
private SwitchParameter? _unbound;
502+
481503
[ExcludeFromCodeCoverage]
482504
protected override void EndProcessing()
483505
{
484-
WriteObject(PSConsoleReadLine.GetKeyHandlers(), true);
506+
bool bound = true;
507+
bool unbound = true;
508+
if (_bound.HasValue && _unbound.HasValue)
509+
{
510+
bound = _bound.Value.IsPresent;
511+
unbound = _unbound.Value.IsPresent;
512+
}
513+
else if (_bound.HasValue)
514+
{
515+
bound = _bound.Value.IsPresent;
516+
unbound = false;
517+
}
518+
else if (_unbound.HasValue)
519+
{
520+
bound = false;
521+
unbound = _unbound.Value.IsPresent;
522+
}
523+
WriteObject(PSConsoleReadLine.GetKeyHandlers(bound, unbound), true);
485524
}
486525
}
487526
}

PSReadLine/PSReadLine.format.ps1xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<Width>20</Width>
1616
</TableColumnHeader>
1717
<TableColumnHeader>
18-
<Label>BriefDescription</Label>
19-
<Width>22</Width>
18+
<Label>Function</Label>
19+
<Width>24</Width>
2020
</TableColumnHeader>
2121
<TableColumnHeader/>
2222
</TableHeaders>
@@ -27,10 +27,10 @@
2727
<PropertyName>Key</PropertyName>
2828
</TableColumnItem>
2929
<TableColumnItem>
30-
<PropertyName>BriefDescription</PropertyName>
30+
<PropertyName>Function</PropertyName>
3131
</TableColumnItem>
3232
<TableColumnItem>
33-
<PropertyName>LongDescription</PropertyName>
33+
<PropertyName>Description</PropertyName>
3434
</TableColumnItem>
3535
</TableColumnItems>
3636
</TableRowEntry>

PSReadLine/ReadLine.cs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Management.Automation;
77
using System.Management.Automation.Language;
8+
using System.Reflection;
89
using System.Runtime.InteropServices;
910
using System.Text;
1011
using System.Text.RegularExpressions;
@@ -46,21 +47,21 @@ public enum TokenClassification
4647
public class KeyHandler
4748
{
4849
public string Key { get; set; }
49-
public string BriefDescription { get; set; }
50-
public string LongDescription
50+
public string Function { get; set; }
51+
public string Description
5152
{
5253
get
5354
{
54-
var result = _longDescription;
55+
var result = _description;
5556
if (string.IsNullOrWhiteSpace(result))
56-
result = PSReadLineResources.ResourceManager.GetString(BriefDescription + "Description");
57+
result = PSReadLineResources.ResourceManager.GetString(Function + "Description");
5758
if (string.IsNullOrWhiteSpace(result))
58-
result = BriefDescription;
59+
result = Function;
5960
return result;
6061
}
61-
set { _longDescription = value; }
62+
set { _description = value; }
6263
}
63-
private string _longDescription;
64+
private string _description;
6465
}
6566

6667
public class PSConsoleReadLine
@@ -3462,33 +3463,71 @@ public static void SetKeyHandler(string[] key, Action<ConsoleKeyInfo?, object> h
34623463
/// Helper function for the Get-PSReadlineKeyHandler cmdlet.
34633464
/// </summary>
34643465
/// <returns></returns>
3465-
public static IEnumerable<PSConsoleUtilities.KeyHandler> GetKeyHandlers()
3466+
public static IEnumerable<PSConsoleUtilities.KeyHandler> GetKeyHandlers(bool includeBound = true, bool includeUnbound = false)
34663467
{
3468+
var boundFunctions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
3469+
34673470
foreach (var entry in _singleton._dispatchTable)
34683471
{
3469-
if (entry.Value.BriefDescription == "Ignore"
3472+
if (entry.Value.BriefDescription == "Ignore"
34703473
|| entry.Value.BriefDescription == "ChordFirstKey")
34713474
{
34723475
continue;
34733476
}
3474-
yield return new PSConsoleUtilities.KeyHandler
3477+
boundFunctions.Add(entry.Value.BriefDescription);
3478+
if (includeBound)
34753479
{
3476-
Key = entry.Key.ToGestureString(),
3477-
BriefDescription = entry.Value.BriefDescription,
3478-
LongDescription = entry.Value.LongDescription,
3479-
};
3480+
yield return new PSConsoleUtilities.KeyHandler
3481+
{
3482+
Key = entry.Key.ToGestureString(),
3483+
Function = entry.Value.BriefDescription,
3484+
Description = entry.Value.LongDescription,
3485+
};
3486+
}
34803487
}
34813488

34823489
foreach (var entry in _singleton._chordDispatchTable)
34833490
{
34843491
foreach (var secondEntry in entry.Value)
34853492
{
3486-
yield return new PSConsoleUtilities.KeyHandler
3493+
boundFunctions.Add(secondEntry.Value.BriefDescription);
3494+
if (includeBound)
34873495
{
3488-
Key = entry.Key.ToGestureString() + "," + secondEntry.Key.ToGestureString(),
3489-
BriefDescription = secondEntry.Value.BriefDescription,
3490-
LongDescription = secondEntry.Value.LongDescription,
3491-
};
3496+
yield return new PSConsoleUtilities.KeyHandler
3497+
{
3498+
Key = entry.Key.ToGestureString() + "," + secondEntry.Key.ToGestureString(),
3499+
Function = secondEntry.Value.BriefDescription,
3500+
Description = secondEntry.Value.LongDescription,
3501+
};
3502+
}
3503+
}
3504+
}
3505+
3506+
if (includeUnbound)
3507+
{
3508+
// SelfInsert isn't really unbound, but we don't want UI to show it that way
3509+
boundFunctions.Add("SelfInsert");
3510+
3511+
var methods = typeof (PSConsoleReadLine).GetMethods(BindingFlags.Public | BindingFlags.Static);
3512+
foreach (var method in methods)
3513+
{
3514+
var parameters = method.GetParameters();
3515+
if (parameters.Length != 2 ||
3516+
parameters[0].ParameterType != typeof (ConsoleKeyInfo?) ||
3517+
parameters[1].ParameterType != typeof (object))
3518+
{
3519+
continue;
3520+
}
3521+
3522+
if (!boundFunctions.Contains(method.Name))
3523+
{
3524+
yield return new PSConsoleUtilities.KeyHandler
3525+
{
3526+
Key = "Unbound",
3527+
Function = method.Name,
3528+
Description = null,
3529+
};
3530+
}
34923531
}
34933532
}
34943533
}

0 commit comments

Comments
 (0)