Exemple #1
0
 private void SetKeyHandlerInternal(string[] keys, Action <ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription, ScriptBlock scriptBlock)
 {
     foreach (var key in keys)
     {
         var chord = ConsoleKeyChordConverter.Convert(key);
         if (chord.Length == 1)
         {
             _dispatchTable[chord[0]] = MakeKeyHandler(handler, briefDescription, longDescription, scriptBlock);
         }
         else
         {
             _dispatchTable[chord[0]] = MakeKeyHandler(Chord, "ChordFirstKey");
             if (!_chordDispatchTable.TryGetValue(chord[0], out var secondDispatchTable))
             {
                 secondDispatchTable           = new Dictionary <ConsoleKeyInfo, KeyHandler>(ConsoleKeyInfoComparer.Instance);
                 _chordDispatchTable[chord[0]] = secondDispatchTable;
             }
             secondDispatchTable[chord[1]] = MakeKeyHandler(handler, briefDescription, longDescription, scriptBlock);
         }
     }
 }
Exemple #2
0
 private void RemoveKeyHandlerInternal(string[] keys)
 {
     foreach (var key in keys)
     {
         var chord = ConsoleKeyChordConverter.Convert(key);
         if (chord.Length == 1)
         {
             _dispatchTable.Remove(chord[0]);
         }
         else
         {
             if (_chordDispatchTable.TryGetValue(chord[0], out var secondDispatchTable))
             {
                 secondDispatchTable.Remove(chord[1]);
                 if (secondDispatchTable.Count == 0)
                 {
                     _dispatchTable.Remove(chord[0]);
                 }
             }
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// Return key handlers bound to specified chords.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <PowerShell.KeyHandler> GetKeyHandlers(string[] Chord)
        {
            var boundFunctions = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            if (Chord == null || Chord.Length == 0)
            {
                yield break;
            }

            foreach (string Key in Chord)
            {
                ConsoleKeyInfo[] consoleKeyChord = ConsoleKeyChordConverter.Convert(Key);
                PSKeyInfo        firstKey        = PSKeyInfo.FromConsoleKeyInfo(consoleKeyChord[0]);

                if (_singleton._dispatchTable.TryGetValue(firstKey, out KeyHandler entry))
                {
                    if (consoleKeyChord.Length == 1)
                    {
                        yield return(new PowerShell.KeyHandler
                        {
                            Key = firstKey.KeyStr,
                            Function = entry.BriefDescription,
                            Description = entry.LongDescription,
                            Group = GetDisplayGrouping(entry.BriefDescription),
                        });
                    }
                    else
                    {
                        PSKeyInfo secondKey = PSKeyInfo.FromConsoleKeyInfo(consoleKeyChord[1]);
                        if (_singleton._chordDispatchTable.TryGetValue(firstKey, out var secondDispatchTable) &&
                            secondDispatchTable.TryGetValue(secondKey, out entry))
                        {
                            yield return(new PowerShell.KeyHandler
                            {
                                Key = firstKey.KeyStr + "," + secondKey.KeyStr,
                                Function = entry.BriefDescription,
                                Description = entry.LongDescription,
                                Group = GetDisplayGrouping(entry.BriefDescription),
                            });
                        }
                    }
                }

                // If in Vi mode, also check Vi's command mode list.
                if (_singleton._options.EditMode == EditMode.Vi)
                {
                    if (_viCmdKeyMap.TryGetValue(firstKey, out entry))
                    {
                        if (consoleKeyChord.Length == 1)
                        {
                            if (entry.BriefDescription == "Ignore")
                            {
                                continue;
                            }

                            yield return(new PowerShell.KeyHandler
                            {
                                Key = "<" + firstKey.KeyStr + ">",
                                Function = entry.BriefDescription,
                                Description = entry.LongDescription,
                                Group = GetDisplayGrouping(entry.BriefDescription),
                            });
                        }
                        else
                        {
                            PSKeyInfo secondKey = PSKeyInfo.FromConsoleKeyInfo(consoleKeyChord[1]);
                            if (_viCmdChordTable.TryGetValue(firstKey, out var secondDispatchTable) &&
                                secondDispatchTable.TryGetValue(secondKey, out entry))
                            {
                                if (entry.BriefDescription == "Ignore")
                                {
                                    continue;
                                }

                                yield return(new PowerShell.KeyHandler
                                {
                                    Key = "<" + firstKey.KeyStr + "," + secondKey.KeyStr + ">",
                                    Function = entry.BriefDescription,
                                    Description = entry.LongDescription,
                                    Group = GetDisplayGrouping(entry.BriefDescription),
                                });
                            }
                        }
                    }
                }
            }
        }
Exemple #4
0
 public ConsoleKeyInfo AsConsoleKeyInfo()
 {
     return(ConsoleKeyChordConverter.Convert(KeyStr)[0]);
 }