/// <summary> /// handles a user-pressed console key, encapsulated by <paramref name="consoleKeyInfo"/>. /// if <paramref name="consoleKeyInfo"/> represents a textual key it is appended to input buffer, /// else it is treated accordingly. /// </summary> /// <seealso cref="AppendToInputBuffer(char)"/> /// <seealso cref="handleInputEnterKey"/> /// <seealso cref="handleInputBackspaceKey"/> /// <seealso cref="handleInputLeftArrowKey"/> /// <seealso cref="handleInputRightArrowKey"/> /// <seealso cref="handleInputBackspaceKey"/> /// <seealso cref="AppendToInputBuffer"/> /// <param name="consoleKeyInfo">a user-pressed console key</param> private void handleInputConsoleKey(ConsoleKeyInfo consoleKeyInfo) { // handle input browsing this.inputHistoryManager.HandleInputHistoryBrowsing(consoleKeyInfo); if (consoleKeyInfo.Key == ConsoleKey.Enter) // key represents enter { handleInputEnterKey(); } else if (consoleKeyInfo.Key == ConsoleKey.Backspace) // key represents backspace { handleInputBackspaceKey(); } // only keys with textual representation are inserted to input buffer else if (ConsoleIOUtils.IsTextualKey(consoleKeyInfo)) { if (consoleKeyInfo.Key == ConsoleKey.Backspace) { handleInputBackspaceKey(); } else { if (GetInputBufferCount() < ConsoleIOUtils.WindowWidth - 1) // input line is not full { // insert to input buffer at current cursor position insertToInputBuffer(ConsoleIOUtils.CursorLeft, consoleKeyInfo.KeyChar); } } } }
/// <summary> /// manages input history browsing based on user key press, /// encapsulated by <paramref name="consoleKeyInfo"/>. /// </summary> /// <remarks> /// <para> /// this method should be called each time user presses a key, /// in order for browsing mechanism to be responsive. /// </para> /// <para> /// if user selects a particular entry, either by hitting an end-of-input key ('Enter' by default) /// or by editing the entry, all entries up to (i.e less recent than) selected entry /// are removed from the history. /// </para> /// </remarks> /// <param name="consoleKeyInfo">user-pressed key</param> public void HandleInputHistoryBrowsing(ConsoleKeyInfo consoleKeyInfo) { if (isBrowsingKey(consoleKeyInfo)) // pressed key was a browsing key { handleBrowsingKey(consoleKeyInfo); } // pressed key is a textual key (i.e it changes the input), // and browsing procedure is active else if (ConsoleIOUtils.IsTextualKey(consoleKeyInfo) && browsingActive) { // set browsing procedure state to inactive browsingActive = false; // pop original user input (from before browsing started) recentInputEntries.Pop(); // reset selected entry index selectedEntryIndex = 0; } }
/// <summary> /// handles key press encapsulated in <paramref name="consoleKeyInfo"/>, /// and dislays its textual representation to console, if one exists. /// </summary> /// <param name="consoleKeyInfo"></param> internal static void HandleInputKey(ConsoleKeyInfo consoleKeyInfo) { if (consoleKeyInfo.Key == ConsoleKey.Enter) // key represents enter { handleInputEnterKey(); } else if (consoleKeyInfo.Key == ConsoleKey.Backspace) // key represents backspace { handleInputBackspaceKey(); } else if (consoleKeyInfo.Key == ConsoleKey.LeftArrow) // key represents left-arrow { handleInputLeftArrowKey(); } else if (consoleKeyInfo.Key == ConsoleKey.RightArrow) // key represents right-arrow { handleInputRightArrowKey(); } // key has textual representation else if ( ConsoleIOUtils.IsTextualKey(consoleKeyInfo) && ConsoleIOUtils.CursorLeft < ConsoleIOUtils.WindowWidth - 1) { // insert key char representation to line buffer insertToLineBufferAtCurrentPosition(consoleKeyInfo.KeyChar.ToString()); if (ConsoleIOUtils.CursorLeft == inputLineBuffer.Length - 1) // append to end of line { Console.Write(consoleKeyInfo.KeyChar.ToString()); } else // insert within line { // rewrite current line so that insert within line buffer is visible rewriteInputLineToCosole(); // move 1 character to the right ConsoleIOUtils.MoveCursorHorizontal(1); } } }