static void WriteNextCommand() { var newText = CommandsHistory.GetNextCommand(); if (newText == null) { return; } EraseCurrentCommand(); currentText = newText; Console.Write(currentText); }
/// <summary> /// Console.WriteLine() replacement with auto-completion and clear console (CTRL + L) /// </summary> /// <returns>Command issued by the user</returns> public static string ReadLine() { // Resets here firstCharHeight = Console.CursorTop; currentText = ""; CommandsHistory.GoToLastCommand(); AutoCompletionManager.ResetAutoCompletionState(); while (true) { keyInfo = Console.ReadKey(true); switch (keyInfo.Key) { case ConsoleKey.Enter: CommandsHistory.AddCommand(currentText); Console.Write('\n'); // Don't return whitespaces only return(currentText.All(x => x == ' ') ? "" : currentText); case ConsoleKey.Tab: AutoCompletionManager.AutoComplete(ref currentText); break; case ConsoleKey.UpArrow: WritePreviousCommand(); break; case ConsoleKey.DownArrow: WriteNextCommand(); break; case ConsoleKey.LeftArrow: if (keyInfo.Modifiers == ConsoleModifiers.Control) { SkipLeft(); } else { MoveCursorLeft(); } break; case ConsoleKey.RightArrow: if (keyInfo.Modifiers == ConsoleModifiers.Control) { SkipRight(); } else { MoveCursorRight(); } break; case ConsoleKey.Home: Console.SetCursorPosition(ConsoleTools.PROMPT.Length, firstCharHeight); break; case ConsoleKey.End: Console.SetCursorPosition(ConsoleTools.PROMPT.Length, lastCharHeight); Console.SetCursorPosition(lineLenght, lastCharHeight); break; case ConsoleKey.Delete: EraseNextChar(); break; case ConsoleKey.Backspace: ErasePreviousChar(); break; case ConsoleKey.Insert: insertMode = !insertMode; Console.CursorSize = Console.CursorSize == 100 ? initialCursorSize : 100; break; default: if (!printableKeys.Contains(keyInfo.Key)) { break; } if (keyInfo.Modifiers == ConsoleModifiers.Control) { if (keyInfo.Key == ConsoleKey.L) { ClearConsole(); } else if (keyInfo.Key == ConsoleKey.V) { PasteText(); } break; } PrintCurrentChar(); break; } } }