private void flushOutputBuffer() { // save the line currently pointed to by cursor, // remove it from Console and write it back after the output buffer flush. // this is done in order to avoid deletion of user input from console, // in case user was in the middle of typing. // it is assumed that user input spans at most one line in console. bool restoreInputToConsoleFlag = false; // cursor doesn't point to beginning of line, user might be in the middle of typing if (!ConsoleIOUtils.IsCursorPointingToBeginningOfLine()) { // clear the current console line (holding user input) ConsoleGraphicsHandler.ClearConsoleInputLine(); // restore user input after flush restoreInputToConsoleFlag = true; } // write all entries in output buffer to Console // in insert order. while (outputBuffer.Count > 0) { string outPutEntry = outputBuffer.Dequeue(); WriteOutput(outPutEntry); } // restore input if (restoreInputToConsoleFlag) { writeInputToConsole(); } }
/// <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> /// clears the console input line. /// </summary> internal static void ClearConsoleInputLine() { ConsoleIOUtils.ClearCurrentConsoleLine(); ConsoleIOUtils.PointCursorToBeginningOfLine(); // clear input line buffer inputLineBuffer.Clear(); }
/// <summary> /// handles left arrow key user key press. /// </summary> private static void handleInputLeftArrowKey() { // if not at beginning of line, move cursor one character backwards if (!ConsoleIOUtils.IsCursorPointingToBeginningOfLine()) { ConsoleIOUtils.MoveCursorHorizontal(-1); } }
/// <summary> /// handles backspace key press. removes character to the left of cursor from console. /// </summary> private static void handleInputBackspaceKey() { if (ConsoleIOUtils.CursorLeft > 0) // not at beginning of line { // move cursor one character backwards ConsoleIOUtils.MoveCursorHorizontal(-1); // remove current character from line buffer removeCharacterFromLineBufferAtCurrentPosition(); // rewrite console input line without removed character rewriteInputLineToCosole(); } }
private void readKeyIfAvailable() { ConsoleKeyInfo consoleKeyInfo = ConsoleIOUtils.ReadKey(out bool keyAvailable); // key available in console input buffer if (keyAvailable) { if (registerInput) { handleInputConsoleKey(consoleKeyInfo); ConsoleGraphicsHandler.HandleInputKey(consoleKeyInfo); } else { // discard Console input ConsoleIOUtils.ClearConsoleInputBuffer(); } } }
/// <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); } } }
/// <summary> /// handles <paramref name="output"/> and displays it to console. /// </summary> /// <param name="output"></param> internal static void HandleOutput(string output) { ConsoleIOUtils.ConsoleWrite(output); setLineBufferToOutputLastLine(output); }
/// <summary> /// handles end-of-input key press. /// </summary> private static void handleInputEnterKey() { // move cursor to beginning of next line ConsoleIOUtils.SetCursorToBeginningOfNextLine(); }