|
5 | 5 | using System.Linq;
|
6 | 6 | using System.Management.Automation;
|
7 | 7 | using System.Management.Automation.Language;
|
| 8 | +using System.Reflection; |
8 | 9 | using System.Runtime.InteropServices;
|
9 | 10 | using System.Text;
|
10 | 11 | using System.Text.RegularExpressions;
|
@@ -46,21 +47,21 @@ public enum TokenClassification
|
46 | 47 | public class KeyHandler
|
47 | 48 | {
|
48 | 49 | 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 |
51 | 52 | {
|
52 | 53 | get
|
53 | 54 | {
|
54 |
| - var result = _longDescription; |
| 55 | + var result = _description; |
55 | 56 | if (string.IsNullOrWhiteSpace(result))
|
56 |
| - result = PSReadLineResources.ResourceManager.GetString(BriefDescription + "Description"); |
| 57 | + result = PSReadLineResources.ResourceManager.GetString(Function + "Description"); |
57 | 58 | if (string.IsNullOrWhiteSpace(result))
|
58 |
| - result = BriefDescription; |
| 59 | + result = Function; |
59 | 60 | return result;
|
60 | 61 | }
|
61 |
| - set { _longDescription = value; } |
| 62 | + set { _description = value; } |
62 | 63 | }
|
63 |
| - private string _longDescription; |
| 64 | + private string _description; |
64 | 65 | }
|
65 | 66 |
|
66 | 67 | public class PSConsoleReadLine
|
@@ -3462,33 +3463,71 @@ public static void SetKeyHandler(string[] key, Action<ConsoleKeyInfo?, object> h
|
3462 | 3463 | /// Helper function for the Get-PSReadlineKeyHandler cmdlet.
|
3463 | 3464 | /// </summary>
|
3464 | 3465 | /// <returns></returns>
|
3465 |
| - public static IEnumerable<PSConsoleUtilities.KeyHandler> GetKeyHandlers() |
| 3466 | + public static IEnumerable<PSConsoleUtilities.KeyHandler> GetKeyHandlers(bool includeBound = true, bool includeUnbound = false) |
3466 | 3467 | {
|
| 3468 | + var boundFunctions = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| 3469 | + |
3467 | 3470 | foreach (var entry in _singleton._dispatchTable)
|
3468 | 3471 | {
|
3469 |
| - if (entry.Value.BriefDescription == "Ignore" |
| 3472 | + if (entry.Value.BriefDescription == "Ignore" |
3470 | 3473 | || entry.Value.BriefDescription == "ChordFirstKey")
|
3471 | 3474 | {
|
3472 | 3475 | continue;
|
3473 | 3476 | }
|
3474 |
| - yield return new PSConsoleUtilities.KeyHandler |
| 3477 | + boundFunctions.Add(entry.Value.BriefDescription); |
| 3478 | + if (includeBound) |
3475 | 3479 | {
|
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 | + } |
3480 | 3487 | }
|
3481 | 3488 |
|
3482 | 3489 | foreach (var entry in _singleton._chordDispatchTable)
|
3483 | 3490 | {
|
3484 | 3491 | foreach (var secondEntry in entry.Value)
|
3485 | 3492 | {
|
3486 |
| - yield return new PSConsoleUtilities.KeyHandler |
| 3493 | + boundFunctions.Add(secondEntry.Value.BriefDescription); |
| 3494 | + if (includeBound) |
3487 | 3495 | {
|
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 | + } |
3492 | 3531 | }
|
3493 | 3532 | }
|
3494 | 3533 | }
|
|
0 commit comments