/// <summary> /// Handle Tab Input /// </summary> /// <param name="consoleInput"> Console Input </param> private void HandleTabInput(ConsoleInput consoleInput) { // current input string currentInput = consoleInput; // check if input is already a part of the commands list int indexOfInput = ListOfCommands.IndexOf(currentInput); // match string match; // if input is a part of the commands list : // then display the next command in alphabetical order if (-1 != indexOfInput) { match = indexOfInput + 1 < ListOfCommands.Count() ? ListOfCommands[indexOfInput + 1] : ""; } // if input isnt in the commands list, find the first match else { match = ListOfCommands.FirstOrDefault(m => m.StartsWith(currentInput, true, CultureInfo.InvariantCulture)); } // no match if (string.IsNullOrEmpty(match)) { return; } // clear line and current input ClearCurrentLine(); consoleInput.Clear(); // set line and current input to the current match Console.Write(match); consoleInput.Append(match); }
/// <summary> /// Processes the user input which is not a tab /// </summary> /// <param name="consoleInput"> Console Input which stores the user input till now </param> /// <param name="userInput"> the current user input </param> private void HandleKeyInput(ConsoleInput consoleInput, ConsoleKeyInfo userInput) { // current input string currentInput = consoleInput; // Handle backspace if (ConsoleKey.Backspace == userInput.Key) { // if user has pressed backspace, remove the last character from the console output and current input if (currentInput.Length > 0 && consoleInput.CurrentIndex > 0) { var number = consoleInput.CurrentIndex > 0 ? consoleInput.CurrentIndex - 1 : 0; consoleInput.Remove(number, 1); // clear the line ClearCurrentLine(); // remove from string and print on console currentInput = consoleInput; Console.Write(currentInput); Terminal.SetCursorPosition(number, Console.CursorTop); } } else if (ConsoleKey.UpArrow == userInput.Key) { ClearCurrentLine(); consoleInput.Clear(); currentInput = _commandHistory.GetPrevious(); consoleInput.Append(currentInput); Console.Write($"{currentInput}"); } else if (ConsoleKey.DownArrow == userInput.Key) { ClearCurrentLine(); consoleInput.Clear(); currentInput = _commandHistory.GetNext(); consoleInput.Append(currentInput); Console.Write($"{currentInput}"); } else if (ConsoleKey.LeftArrow == userInput.Key) { var number = consoleInput.CurrentIndex > 0 ? --consoleInput.CurrentIndex : 0; Terminal.SetCursorPosition(number, Console.CursorTop); } else if (ConsoleKey.RightArrow == userInput.Key) { var number = consoleInput.CurrentIndex < consoleInput.Length? ++consoleInput.CurrentIndex : consoleInput.Length; Terminal.SetCursorPosition(number, Console.CursorTop); } // all other keys else { // To Lower is done because when we read a key using Console.ReadKey(), // the uppercase is returned irrespective of the case of the user input. var key = userInput.KeyChar; consoleInput.Insert(key); ClearCurrentLine(); Console.Write(consoleInput); Terminal.SetCursorPosition(consoleInput.CurrentIndex, Console.CursorTop); } }