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 browsing key encapsulated by <paramref name="consoleKeyInfo"/>, /// as defined by the browsing mechanism: /// <see cref="ConsoleKey.UpArrow"/> displays less recent entries; /// <see cref="ConsoleKey.DownArrow"/> displays more recent entries. /// </summary> /// <param name="consoleKeyInfo"></param> private void handleBrowsingKey(ConsoleKeyInfo consoleKeyInfo) { // amount to increment / decrement index of currently selected input entry int selectedEntryIndexIncrementAmount = 0; if (consoleKeyInfo.Key == ConsoleKey.UpArrow) { selectedEntryIndexIncrementAmount = 1; // start of browsing procedure if (!browsingActive) { // push current user input to stack so it can be retrieved later string userInput = consoleIOHandler.GetInputBufferContent(); recentInputEntries.Push(userInput); // set browsing procedure state to active browsingActive = true; } } else // consoleKeyInfo.Key == ConsoleKey.DownArrow { selectedEntryIndexIncrementAmount = -1; // top of recent input entry stack reached (back to original user input) if (browsingActive && selectedEntryIndex == 0) { // pop original user input from stack string originalUserInput = recentInputEntries.Pop(); // set browsing procedure state to inactive browsingActive = false; } } // still within stack bounds, top or bottom not yet reached if (recentInputEntries.HasElementAt( selectedEntryIndex + selectedEntryIndexIncrementAmount)) { // increment selected input entry index selectedEntryIndex += selectedEntryIndexIncrementAmount; // get selected entry from stack string selectedInputEntry = recentInputEntries.ElementAt(selectedEntryIndex); // overwrite current console input line ConsoleGraphicsHandler.OverwriteInputLine(selectedInputEntry); // overwrite input buffer consoleIOHandler.FlushInputBuffer(); consoleIOHandler.appendToInputBuffer(selectedInputEntry); } }
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> /// writes output string <paramref name="str"/> to Console. /// </summary> /// <remarks> /// default implementation writes <paramref name="output"/> string as-is to Console. /// </remarks> /// <seealso cref="System.Console.Write(string)"/> /// <param name="output"></param> protected virtual void WriteOutput(string output) { ConsoleGraphicsHandler.HandleOutput(output); }