public static bool ReadOrCancel <T>(this ReadcharConfiguration <T> configuration, IConsole console, out T result)
        {
            if (configuration.Options.Count == 0)
            {
                throw new System.ArgumentException("Configuration must support at least one option.");
            }

            var start = console.GetCursorPosition();

            console.Write(configuration.Prompt);

            System.ConsoleKeyInfo input;
            do
            {
                input = console.ReadKey(true);

                if (input.Key == System.ConsoleKey.Escape)
                {
                    console.WriteLine();

                    result = default;
                    return(false);
                }
            } while (!configuration.Options.TryGetValue(input.KeyChar, out result));

            console.WriteLine();

            return(true);
        }
Beispiel #2
0
 public static ReadcharConfiguration <T> WithPrompt <T>(this ReadcharConfiguration <T> composer, ConsoleString prompt)
 {
     return(new ReadcharConfiguration <T>
            (
                prompt: prompt,
                options: composer.Options
            ));
 }
Beispiel #3
0
 public static ReadcharConfiguration <T> WithOption <T>(this ReadcharConfiguration <T> composer, char character, T value)
 {
     return(new ReadcharConfiguration <T>
            (
                prompt: composer.Prompt,
                options: composer.Options.SetItem(character, value)
            ));
 }
        public static T Read <T>(this ReadcharConfiguration <T> configuration, IConsole console)
        {
            if (configuration.Options.Count == 0)
            {
                throw new System.ArgumentException("Configuration must support at least one option.");
            }

            var start = console.GetCursorPosition();

            console.Write(configuration.Prompt);

            System.ConsoleKeyInfo input;
            T value;

            do
            {
                input = console.ReadKey(true);
            } while (!configuration.Options.TryGetValue(input.KeyChar, out value));

            console.WriteLine();

            return(value);
        }
Beispiel #5
0
 public static ReadcharConfiguration <T> WithoutPrompt <T>(this ReadcharConfiguration <T> composer) => composer.WithPrompt(ConsoleString.Empty);