Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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;
            }
        }