private void ReadFromConsole_Backspace(ConsoleKey key, ConsoleInputState state) { if (state.Cursor > 0) { state.Txt.Remove(state.Cursor - 1, 1); state.Cursor--; } else if (state.Cursor == 0) { return; } var l = state.Txt.Length - state.Cursor; if (l > 0) { Write("".PadLeft(l, ' '), ConsoleOutputStyle.Input); SetCursorPosition(state.StartX + state.Cursor, state.StartY); Write(state.Txt.ToString().Substring(state.Cursor), ConsoleOutputStyle.Input); SetCursorPosition(state.StartX + state.Cursor, state.StartY); } else { Write("\b \b", ConsoleOutputStyle.Input); } }
private void ReadFromConsole_UpDown(ConsoleKey key, ConsoleInputState state) { var strH = ""; if (_history.Count > 0) { state.HistoryIndex = state.HistoryIndex + (key == ConsoleKey.DownArrow || key == ConsoleKey.PageDown ? 1 : -1); if (state.HistoryIndex < 0) { state.HistoryIndex = _history.Count - 1; } else if (state.HistoryIndex > _history.Count - 1) { state.HistoryIndex = 0; } strH = _history[state.HistoryIndex]; state.Txt.Clear(); state.Txt.Append(strH); state.Cursor = state.Txt.Length; } CleanFromThisPoint(state.StartX, state.StartY); Write(strH, ConsoleOutputStyle.Input); }
private void ReadFromConsole_Insert(ConsoleKey key, ConsoleInputState state) { state.InsertMode = !state.InsertMode; if (IsWindows) { Console.CursorSize = !state.InsertMode ? 100 : 25; } }
private void ReadFromConsole_LeftRight(ConsoleKey key, ConsoleInputState state) { if (key == ConsoleKey.LeftArrow) { state.Cursor = Math.Max(0, state.Cursor - 1); } else { state.Cursor = Math.Min(state.Txt.Length, state.Cursor + 1); } SetCursorPosition(state.StartX + state.Cursor, state.StartY); }
private void ReadFromConsole_HomeEnd(ConsoleKey key, ConsoleInputState state) { if (key != ConsoleKey.End) { state.Cursor = 0; } else { state.Cursor = state.Txt.Length; } SetCursorPosition(state.StartX + state.Cursor, state.StartY); }
private void ReadFromConsole_Delete(ConsoleKey key, ConsoleInputState state) { if (state.Cursor >= state.Txt.Length) { return; } state.Txt.Remove(state.Cursor, 1); if (state.Txt.Length - state.Cursor != 0) { SetCursorPosition(state.StartX + state.Cursor, state.StartY); Write(state.Txt.ToString().Substring(state.Cursor) + " \b", ConsoleOutputStyle.Input); SetCursorPosition(state.StartX + state.Cursor, state.StartY); } else { Write(" \b", ConsoleOutputStyle.Input); } }
private void ReadFromConsole_Default(ConsoleKeyInfo key, ConsoleInputState state) { state.Txt.Insert(state.Cursor, key.KeyChar); state.Cursor++; if (!state.InsertMode) { Write(key.KeyChar.ToString(), ConsoleOutputStyle.Input); if (state.Cursor < state.Txt.Length) { state.Txt.Remove(state.Cursor, 1); } } else { Write(state.Txt.ToString().Substring(state.Cursor - 1), ConsoleOutputStyle.Input); if (state.Cursor != state.Txt.Length) { SetCursorPosition(state.StartX + state.Cursor, state.StartY); } } }
private void ReadFromConsole_Enter(ConsoleKey key, ConsoleInputState state) { WriteLine("", ConsoleOutputStyle.Input); }
private void ReadFromConsole_Tab(ConsoleKey key, ConsoleInputState state) { var cmdArgs = new List <CommandToken>(state.Txt.ToString().SplitCommandLine()).ToArray(); var matches = SearchCommands(cmdArgs, state.Autocomplete); if (matches == null || matches.Length <= 0) { return; } string[] allowed; var isArgument = matches.Length == 1 && matches[0].IsExact; // Search match var argInjectionPoint = -1; var argInjectonPointLength = -1; if (isArgument) { var parIndex = 0; var selectedToken = cmdArgs.Where(u => u.IsInside(state.Cursor)).FirstOrDefault(); if (selectedToken != null) { parIndex = Array.IndexOf(cmdArgs, selectedToken) - matches[0].ParameterStartIndex; argInjectionPoint = selectedToken.StartIndex; argInjectonPointLength = selectedToken.RealLength; if (selectedToken.Quoted) { argInjectionPoint++; argInjectonPointLength -= 2; } } else { parIndex = -1; argInjectionPoint = state.Txt.Length; argInjectonPointLength = 0; } var parameter = GetParameterCandidate(matches[0].Methods, parIndex); allowed = state.Autocomplete.GetParameterValues(parameter, selectedToken?.Value)?.ToArray(); // If any option have one space ... if (allowed != null && selectedToken != null && !selectedToken.Quoted && allowed.Any(u => u.Contains(" "))) { // Check if we need to append the quotes (c:\\Program files) state.Txt.Insert(selectedToken.StartIndex + selectedToken.RealLength, "\""); state.Txt.Insert(selectedToken.StartIndex, "\""); argInjectionPoint++; } } else { allowed = matches.Select(u => u.Method).ToArray(); } var maxCommonLength = ComputeMaxString(allowed, out var cmd); if (string.IsNullOrEmpty(cmd)) { return; } // Take coincidences if (isArgument) { state.Txt.Remove(argInjectionPoint, argInjectonPointLength); state.Txt.Insert(argInjectionPoint, cmd.Substring(0, maxCommonLength)); state.Cursor = argInjectionPoint + maxCommonLength; } else { // Is method state.Txt.Clear(); state.Txt.Append(cmd.Substring(0, maxCommonLength)); if (allowed.Length == 1) { state.Txt.Append(' '); } state.Cursor = state.Txt.Length; } // Print found OutFoundStrs(allowed, maxCommonLength); // Prompt WritePrompt(); state.GetCursorPosition(); Write(state.Txt.ToString(), ConsoleOutputStyle.Input); SetCursorPosition(state.StartX + state.Cursor, state.StartY); }
/// <inheritdoc /> public string ReadFromConsole(IAutoCompleteHandler autocomplete = null) { State = ConsoleReaderState.Reading; // Write prompt WritePrompt(); // If have something loaded if (TryReadManualInput(out var manual)) { State = ConsoleReaderState.ReadingDirty; // Print it WriteLine(manual, ConsoleOutputStyle.Input); // Use it State = ConsoleReaderState.None; return(manual); } // Read from console var state = new ConsoleInputState(this) { Autocomplete = autocomplete }; if (IsWindows) { // In Mac don't work Console.CursorSize = !state.InsertMode ? 100 : 25; } ConsoleKeyInfo i; do { i = Console.ReadKey(true); if (_keyHandle.TryGetValue(i.Key, out var action)) { action(i.Key, state); } else { ReadFromConsole_Default(i, state); } State = state.Txt.Length > 0 ? ConsoleReaderState.ReadingDirty : ConsoleReaderState.Reading; }while (i.Key != ConsoleKey.Enter); // Process return var ret = state.Txt.ToString(); // Append to history if (_history.LastOrDefault() != ret) { if (_history.Count > MaxHistorySize) { _history.RemoveAt(0); } _history.Add(ret); } // return text State = ConsoleReaderState.None; return(ret); }