public static eInputFieldResult AsInputFieldResult(this eInputResult src) { switch (src) { case ViewTools.eInputResult.Ok: return(eInputFieldResult.Ok); case ViewTools.eInputResult.Tab: return(eInputFieldResult.NextField); case ViewTools.eInputResult.ShiftTab: return(eInputFieldResult.PrevField); case ViewTools.eInputResult.Esc: return(eInputFieldResult.Cancel); default: throw new ArgumentOutOfRangeException(nameof(src), src, null); } }
public static string Input(int left, int top, int length, string prev, ConsoleColor color, out eInputResult inputResult) { Console.ForegroundColor = color; prev = PrepareText(prev, length); StringBuilder txt = new StringBuilder(prev); void RefreshText() { Console.CursorVisible = false; Console.SetCursorPosition(left: left, top: top); Console.Write(txt.ToString()); } RefreshText(); int pos = 0; Console.CursorVisible = true; Console.SetCursorPosition(left: left, top: top); while ((inputResult = ReadKey(out var keyInfo)) == eInputResult.None) { if (keyInfo.Key == ConsoleKey.LeftArrow) { if (pos <= 0) { continue; } pos -= 1; } else if (keyInfo.Key == ConsoleKey.RightArrow) { if (pos >= length - 1) { continue; } pos += 1; } else if (keyInfo.Key == ConsoleKey.Home) { pos = 0; } else if (keyInfo.Key == ConsoleKey.End) { pos = Math.Min(txt.ToString().TrimEnd().Length, length - 1); } else if (pos < length) { if (keyInfo.Key == ConsoleKey.Delete) { txt.Remove(pos, 1).Append(' '); RefreshText(); } else if (keyInfo.Key == ConsoleKey.Backspace) { if (pos <= 0) { continue; } pos -= 1; txt.Remove(pos, 1).Append(' '); RefreshText(); } else if (char.IsLetterOrDigit(keyInfo.KeyChar) || char.IsPunctuation(keyInfo.KeyChar) || keyInfo.KeyChar == ' ') { txt[pos] = keyInfo.KeyChar; pos += 1; if (pos == length) { pos = length - 1; } RefreshText(); } } Console.CursorVisible = true; Console.SetCursorPosition(left: left + pos, top: top); } Console.ResetColor(); RefreshText(); return(txt.ToString().Trim()); }