diff --git a/src/client/spark_core/utils/ConsoleEx.cs b/src/client/spark_core/utils/ConsoleEx.cs index 4d9f09da7..4abf7891a 100644 --- a/src/client/spark_core/utils/ConsoleEx.cs +++ b/src/client/spark_core/utils/ConsoleEx.cs @@ -25,19 +25,30 @@ namespace bts.test { public static T prompt(string prompt, params T[] options) { return ConsoleEx.prompt(prompt, (IEnumerable)options); } + private static (bool,int) read_small_number() { + var key = Console.ReadKey(); + return (Int32.TryParse(key.KeyChar.ToString(), out int value), value); + } + private static (bool,int) read_big_number() { + var line = Console.ReadLine(); + return (Int32.TryParse(line, out int value), value); + } + private static int read_number(int max) { + do { + (var success,var value) = max > 9 ? read_big_number() : read_small_number(); + if (success && value >= 0 && value <= max) { + return value; + } + }while(true); + } + public static T prompt(string prompt, IEnumerable options) { WriteLine(prompt, ConsoleColor.Yellow); for (int x = 0; x < options.Count(); ++x) { Write($"{x}. ", ConsoleColor.Yellow); WriteLine(options.ElementAt(x).ToString()); } - int response = 0; - bool success = false; - while(!success) { - var key = Console.ReadKey(); - success = Int32.TryParse(key.KeyChar.ToString(), out response); - if (response > options.Count()) success = false; - } + int response = read_number(options.Count()); var value = options.ElementAt(response); Console.WriteLine(" >> " + value.ToString()); Console.WriteLine();