Exemple #1
0
        /// <inheritdoc/>
        public async Task <T> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
        {
            if (console is null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            return(await console.RunExclusive(async() =>
            {
                var promptStyle = PromptStyle ?? Style.Plain;
                var converter = Converter ?? TypeConverterHelper.ConvertToString;
                var choices = Choices.Select(choice => converter(choice)).ToList();
                var choiceMap = Choices.ToDictionary(choice => converter(choice), choice => choice, _comparer);

                WritePrompt(console);

                while (true)
                {
                    var input = await console.ReadLine(promptStyle, IsSecret, choices, cancellationToken).ConfigureAwait(false);

                    // Nothing entered?
                    if (string.IsNullOrWhiteSpace(input))
                    {
                        if (DefaultValue != null)
                        {
                            console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
                            console.WriteLine();
                            return DefaultValue.Value;
                        }

                        if (!AllowEmpty)
                        {
                            continue;
                        }
                    }

                    console.WriteLine();

                    T?result;
                    if (Choices.Count > 0)
                    {
                        if (choiceMap.TryGetValue(input, out result) && result != null)
                        {
                            return result;
                        }
                        else
                        {
                            console.MarkupLine(InvalidChoiceMessage);
                            WritePrompt(console);
                            continue;
                        }
                    }
                    else if (!TypeConverterHelper.TryConvertFromString <T>(input, out result) || result == null)
                    {
                        console.MarkupLine(ValidationErrorMessage);
                        WritePrompt(console);
                        continue;
                    }

                    // Run all validators
                    if (!ValidateResult(result, out var validationMessage))
                    {
                        console.MarkupLine(validationMessage);
                        WritePrompt(console);
                        continue;
                    }

                    return result;
                }
            }).ConfigureAwait(false));
        }
        /// <summary>
        /// Shows the prompt and requests input from the user.
        /// </summary>
        /// <param name="console">The console to show the prompt in.</param>
        /// <returns>The user input converted to the expected type.</returns>
        /// <inheritdoc/>
        public T Show(IAnsiConsole console)
        {
            if (console is null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            var promptStyle = PromptStyle ?? Style.Plain;

            WritePrompt(console);

            while (true)
            {
                var input = console.ReadLine(promptStyle, IsSecret);

                // Nothing entered?
                if (string.IsNullOrWhiteSpace(input))
                {
                    if (DefaultValue != null)
                    {
                        console.Write(TextPrompt <T> .GetTypeConverter().ConvertToInvariantString(DefaultValue.Value), promptStyle);
                        console.WriteLine();
                        return(DefaultValue.Value);
                    }

                    if (!AllowEmpty)
                    {
                        continue;
                    }
                }

                console.WriteLine();

                // Try convert the value to the expected type.
                if (!TextPrompt <T> .TryConvert(input, out var result) || result == null)
                {
                    console.MarkupLine(ValidationErrorMessage);
                    WritePrompt(console);
                    continue;
                }

                if (Choices.Count > 0)
                {
                    if (Choices.Contains(result))
                    {
                        return(result);
                    }
                    else
                    {
                        console.MarkupLine(InvalidChoiceMessage);
                        WritePrompt(console);
                        continue;
                    }
                }

                // Run all validators
                if (!ValidateResult(result, out var validationMessage))
                {
                    console.MarkupLine(validationMessage);
                    WritePrompt(console);
                    continue;
                }

                return(result);
            }
        }