protected virtual void OnUserInput(UserInputIndicator e) { var handler = UserInput; if (handler != null) { handler(this, e); } }
public void Run() { StopDispatcherLoop = false; while (!StopDispatcherLoop) { var userInput = new UserInputIndicator(); var startingLeft = System.Console.CursorLeft; var startingTop = System.Console.CursorTop; var lastWrittenBytes = 0; var input = System.Console.ReadKey(true); OnUserInput(userInput); if (Commands.Where(f => f.HandleKey).Any(controlerCommand => controlerCommand.Handle(input))) { History.Add(input.KeyChar.ToString()); _currentHistoryElement++; userInput.Dispose(); continue; } var fullInput = ""; if (!char.IsControl(input.KeyChar)) { fullInput += input.KeyChar; } var updated = false; HistoryLookup(input, ref fullInput, ref updated); if (ProvideLookup) { var exitLoop = false; var nextKey = input; do { CleanupLine(lastWrittenBytes, startingLeft, startingTop); lastWrittenBytes = fullInput.Length; System.Console.Write(fullInput); var currentLeft = System.Console.CursorLeft; var currentHeight = System.Console.CursorTop; var fuzzyNexts = GetLookups(fullInput); var fuzzyNext = fuzzyNexts.FirstOrDefault(); if (fuzzyNext != null) { var toAutoComplete = fuzzyNext.Substring(fullInput.Length); if (fuzzyNexts.Length > 1 && ShowAllMatchingElements) { toAutoComplete += fuzzyNexts.Skip(1) .Select(f => string.Format(" | {0}", f.Remove(0, fullInput.Length))) .Aggregate((e, f) => e + f); } var cColor = System.Console.ForegroundColor; System.Console.ForegroundColor = ConsoleColor.DarkGray; System.Console.Write(toAutoComplete); lastWrittenBytes += toAutoComplete.Length; System.Console.ForegroundColor = cColor; System.Console.CursorLeft = currentLeft; System.Console.CursorTop = currentHeight; } nextKey = System.Console.ReadKey(true); HistoryLookup(nextKey, ref fullInput, ref updated); if (nextKey.Key == ConsoleKey.Backspace) { var index = currentLeft - 1; if (index >= 0) { fullInput = fullInput.Remove(index, 1); } else { exitLoop = true; break; } } if (nextKey.Key == ConsoleKey.RightArrow) { fullInput = fuzzyNext; } if (!char.IsControl(nextKey.KeyChar)) { fullInput = fullInput + nextKey.KeyChar; } if (string.IsNullOrEmpty(fullInput)) { CleanupLine(lastWrittenBytes + 1, startingLeft, startingTop); exitLoop = true; break; } } while (nextKey.Key != ConsoleKey.Enter); if (exitLoop) { continue; } System.Console.WriteLine(); foreach (var controlerCommand in Commands.Where(f => f.HandleString)) { if (controlerCommand.Handle(fullInput)) { History.Add(fullInput); _currentHistoryElement++; userInput.Dispose(); break; } } } else { System.Console.Write(fullInput); fullInput += System.Console.ReadLine(); foreach (var controlerCommand in Commands.Where(f => f.HandleString)) { if (controlerCommand.Handle(fullInput)) { History.Add(fullInput); _currentHistoryElement++; userInput.Dispose(); break; } } } } }