public WithDescription <V>?ChooseTuple(int?numberOfOptions = null) { Console.WriteLine(welcomeMessage); var noOfOptsPerScreen = numberOfOptions ?? dictionary.Count; PrintOptions(0, noOfOptsPerScreen); var noOfOptsPrinted = noOfOptsPerScreen; do { var input = Console.ReadLine().Trim(); if (input == "+") { if (noOfOptsPrinted >= dictionary.Count) { continue; } PrintOptions(noOfOptsPrinted, noOfOptsPerScreen); } else { if (string.IsNullOrWhiteSpace(input)) { return(null); } var val = TryGetValue(input); if (val != null) { return(val); } SafeConsole.WriteLineColor(ConsoleColor.Red, "Plase choose a valid option!"); noOfOptsPrinted = 0; PrintOptions(noOfOptsPrinted, noOfOptsPerScreen); } noOfOptsPrinted += noOfOptsPerScreen; } while (true); }
WithDescription <V>?TryGetValue(string input) { var exact = dictionary.TryGetC(input); if (exact != null) { return(exact); } var sd = new StringDistance(); var best = dictionary.Keys.WithMin(a => sd.LevenshteinDistance(input.ToLowerInvariant(), a.ToLowerInvariant())); if (sd.LevenshteinDistance(input.ToLowerInvariant(), best.ToLowerInvariant()) <= 2) { if (SafeConsole.Ask($"Did you mean '{best}'?")) { return(dictionary.GetOrThrow(best)); } } return(null); }
public static void WaitExecute(Action action) { if (Console.IsOutputRedirected) { action(); return; } int?result = null; try { int left = Console.CursorLeft; DateTime dt = DateTime.Now; Task t = Task.Factory.StartNew(() => { while (result == null) { var str = " (" + (DateTime.Now - dt).NiceToString(DateTimePrecision.Seconds) + ")"; Console.SetCursorPosition(Math.Max(0, Math.Min(left, Console.WindowWidth - str.Length - 1)), Console.CursorTop); lock (SafeConsole.SyncKey) SafeConsole.WriteColor(ConsoleColor.DarkGray, str); Thread.Sleep(1000); } }); action(); } finally { result = -1; } }