|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace HelpConsole |
| 11 | +{ |
| 12 | + public partial class HelpConsole |
| 13 | + { |
| 14 | + #region Transition of charKey to ConsoleKey |
| 15 | + |
| 16 | + //private static ConsoleKey ToConsoleKeyInfo(char charKey) |
| 17 | + //{ |
| 18 | + // return (ConsoleKey)System.Enum.Parse(typeof(ConsoleKey), charKey.ToString()); |
| 19 | + //} |
| 20 | + |
| 21 | + #endregion Transition of charKey to ConsoleKey |
| 22 | + |
| 23 | + #region Additional constructors |
| 24 | + |
| 25 | + //public static ConsoleKeyInfo ConsoleKeyInfo(char keyChar) => new ConsoleKeyInfo(keyChar: keyChar, ToConsoleKeyInfo(keyChar), false, false, false); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// ConsoleKeyInfo consructor works with keyChar and key, all modes are false. |
| 29 | + /// </summary> |
| 30 | + /// <param name="keyChar">A character on keybord represented by a char.</param> |
| 31 | + /// <param name="key">A key that is defined on ConsoleKey enum. Integere value of key must be between 0 and 255.</param> |
| 32 | + /// <returns>ConsoleKeyInfo consists keyChar, key and mods.</returns> |
| 33 | + public static ConsoleKeyInfo ConsoleKeyInfo(char keyChar, ConsoleKey key) => new ConsoleKeyInfo(keyChar: keyChar, key: key, shift: false, alt: false, control: false); |
| 34 | + |
| 35 | + #endregion Additional constructors |
| 36 | + |
| 37 | + #region List of Key-Action pair. |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Key-Action pair list that holds values which will be used by KeyAction(ConsoleKeyInfo cki). Readonly? |
| 41 | + /// </summary> |
| 42 | + public static List<Tuple<ConsoleKeyInfo, Action>> KeyActionPairList = new List<Tuple<ConsoleKeyInfo, Action>>(); |
| 43 | + |
| 44 | + #endregion List of Key-Action pair. |
| 45 | + |
| 46 | + #region Add, List and Clear of Key-Action pair list. |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Adds Key-Action pair into KeyActionPairList which will be used by KeyAction(ConsoleKeyInfo cki). |
| 50 | + /// </summary> |
| 51 | + /// <param name="cki">ConsoleKeyInfo constructed by either ConsoleKeyInfo(char keyChar, ConsoleKey consoleKey) or predefined ConsoleKeyInfo(char keyChar, ConsoleKey consoleKey, bool alt, bool shift, bool ctrl)</param> |
| 52 | + /// <param name="action">An action to be called with match of cki.</param> |
| 53 | + public static void AddKeyActionPair(ConsoleKeyInfo cki, Action action) |
| 54 | + { |
| 55 | + // Adding given value into the list. |
| 56 | + KeyActionPairList.Add(new Tuple<ConsoleKeyInfo, Action>(cki, action)); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// List Key-Action par list. |
| 61 | + /// </summary> |
| 62 | + public static void ListKeyActionPairList() |
| 63 | + { |
| 64 | + // Variable of cki value. |
| 65 | + ConsoleKeyInfo cki = new ConsoleKeyInfo(); |
| 66 | + |
| 67 | + // Loop to print all items. |
| 68 | + for (int i = 0; i < KeyActionPairList.Count; i++) |
| 69 | + { |
| 70 | + // set variable from the list with first element of tuple. |
| 71 | + cki = KeyActionPairList[i].Item1; |
| 72 | + |
| 73 | + // Checking if KeyChar is empty character. |
| 74 | + if (cki.KeyChar != ' ') |
| 75 | + { |
| 76 | + // Writing KeyChar value. |
| 77 | + Write($"KeyChar: {cki.KeyChar} "); |
| 78 | + } |
| 79 | + |
| 80 | + // Writing rest of the values of ConsoleKeyInfo. |
| 81 | + WriteLine($"Key: {cki.Key} Mods: {Enum.GetName(typeof(ConsoleModifiers), cki.Modifiers)} Action: {KeyActionPairList[i].Item2.Method.Name}"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Clear all items of Key-Action pair list. |
| 87 | + /// </summary> |
| 88 | + public static void ClearKeyActionPairList() |
| 89 | + { |
| 90 | + // Calling clear() function of List. |
| 91 | + KeyActionPairList.Clear(); |
| 92 | + } |
| 93 | + |
| 94 | + #endregion Add, List and Clear of Key-Action pair list. |
| 95 | + |
| 96 | + #region Wait |
| 97 | + |
| 98 | + // Show any message or list options could be optional parameter. |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// |
| 102 | + /// </summary> |
| 103 | + public static void WaitKeyAction() |
| 104 | + { |
| 105 | + ConsoleKeyInfo? cki = null; |
| 106 | + |
| 107 | + while (cki == null) |
| 108 | + { |
| 109 | + // Wait for a key. |
| 110 | + cki = Console.ReadKey(); |
| 111 | + } |
| 112 | + |
| 113 | + // Checking if cki is in Key-Action pair list. |
| 114 | + if (KeyActionPairList.Select(p => p.Item1).Any(q => q == cki)) |
| 115 | + { |
| 116 | + // cki is not null now. |
| 117 | + KeyAction(cki.Value); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + Debug.WriteLine("HelpConsole:Key is not defined on Key-Action pair list. Use AddKeyActionPair() or ListActionPairList()"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + #endregion Wait |
| 126 | + |
| 127 | + #region Do |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Call action by predefined on Key-Action pair list. |
| 131 | + /// </summary> |
| 132 | + /// <param name="cki">ConsoleKeyInfo to call predefined Action</param> |
| 133 | + public static void KeyAction(ConsoleKeyInfo cki) |
| 134 | + { |
| 135 | + // Get action for the list. |
| 136 | + Action action = KeyActionPairList.Where(p => p.Item1 == cki).Select(q => q.Item2).SingleOrDefault(); |
| 137 | + |
| 138 | + // Checking if it is null. |
| 139 | + if (action == null) |
| 140 | + { |
| 141 | + // Inform user that action is not defined. |
| 142 | + Debug.WriteLine("HelpConsole: Action is not defined on list."); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + // calling action. |
| 147 | + action(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + #endregion Do |
| 152 | + } |
| 153 | +} |
0 commit comments