Example #1
0
                /// <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);
                    }
                }