public static ReadlineConfiguration <T> WithPrompt <T>(this ReadlineConfiguration <T> composer, ConsoleString prompt)
 {
     return(new ReadlineConfiguration <T>
            (
                prompt: prompt,
                @default: composer.Default,
                cleanup: composer.Cleanup,
                parser: composer.Parser,
                validator: composer.Validator
            ));
 }
Exemple #2
0
        public static bool ReadOrCancel <T>(this ReadlineConfiguration <T> configuration, IConsole console, out T result)
        {
            var start = console.GetCursorPosition();

            console.Write(configuration.Prompt);
            var reader = new ConsoleReader(console)
            {
                Text = configuration.Default
            };

            var         cancelled = false;
            Message <T> msg;

            do
            {
                if (!reader.ReadLineOrCancel(out var input))
                {
                    result    = default;
                    cancelled = true;
                    break;
                }

                var arrayInput = typeof(T).IsArray ? Command.SimulateParse(input) : new[] { input };

                msg = configuration.Parser.Parse(arrayInput);
                msg = configuration.Validator.Validate(msg);

                if (msg.IsError)
                {
                    ShowError(console, reader, msg.Content);
                    result = default;
                }
                else
                {
                    result = msg.Value;
                }
            } while (msg.IsError);

            ApplyCleanup
            (
                prompt: configuration.Prompt,
                console: console,
                start: start,
                reader: reader,
                cleanup: cancelled ? configuration.Cleanup.Cancel : configuration.Cleanup.Success
            );

            return(!cancelled);
        }
 public static ReadlineConfiguration <T> WithCleanup <T>(this ReadlineConfiguration <T> composer, ReadlineCleanup success, ReadlineCleanup cancel)
 {
     return(new ReadlineConfiguration <T>
            (
                prompt: composer.Prompt,
                @default: composer.Default,
                cleanup: new ReadlineCleanupConfiguration
                (
                    success: success,
                    cancel: cancel
                ),
                parser: composer.Parser,
                validator: composer.Validator
            ));
 }
Exemple #4
0
        public static T Read <T>(this ReadlineConfiguration <T> configuration, IConsole console)
        {
            var start = console.GetCursorPosition();

            console.Write(configuration.Prompt);
            var reader = new ConsoleReader(console)
            {
                Text = configuration.Default
            };

            Message <T> msg;

            do
            {
                var input      = reader.ReadLine();
                var arrayInput = typeof(T).IsArray ? Command.SimulateParse(input) : new[] { input };

                msg = configuration.Parser.Parse(arrayInput);
                msg = configuration.Validator.Validate(msg);

                if (msg.IsError)
                {
                    ShowError(console, reader, msg.Content);
                }
            } while (msg.IsError);

            ApplyCleanup
            (
                prompt: configuration.Prompt,
                console: console,
                start: start,
                reader: reader,
                cleanup: configuration.Cleanup.Success
            );

            return(msg.Value);
        }
 public static ReadlineConfiguration <T> WithoutCleanup <T>(this ReadlineConfiguration <T> composer) => composer.WithCleanup(ReadlineCleanup.None);
 public static ReadlineConfiguration <T> WithCleanup <T>(this ReadlineConfiguration <T> composer, ReadlineCleanup cleanup = ReadlineCleanup.RemoveAll)
 {
     return(composer.WithCleanup(cleanup, cleanup));
 }
 public static ReadlineConfiguration <T> WithoutDefault <T>(this ReadlineConfiguration <T> composer) => composer.WithDefault(string.Empty);
 public static ReadlineConfiguration <T> WithoutPrompt <T>(this ReadlineConfiguration <T> composer) => composer.WithPrompt(ConsoleString.Empty);