Ejemplo n.º 1
0
        /// <summary>
        /// Writes the prompt to the console.
        /// </summary>
        /// <param name="console">The console to write the prompt to.</param>
        private void WritePrompt(IAnsiConsole console)
        {
            if (console is null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            var builder = new StringBuilder();

            builder.Append(_prompt.TrimEnd());

            if (ShowChoices && Choices.Count > 0)
            {
                var converter = Converter ?? TypeConverterHelper.ConvertToString;
                var choices   = string.Join("/", Choices.Select(choice => converter(choice)));
                builder.AppendFormat(CultureInfo.InvariantCulture, " [blue][[{0}]][/]", choices);
            }

            if (ShowDefaultValue && DefaultValue != null)
            {
                builder.AppendFormat(
                    CultureInfo.InvariantCulture,
                    " [green]({0})[/]",
                    TypeConverterHelper.ConvertToString(DefaultValue.Value));
            }

            var markup = builder.ToString().Trim();

            if (!markup.EndsWith("?", StringComparison.OrdinalIgnoreCase) &&
                !markup.EndsWith(":", StringComparison.OrdinalIgnoreCase))
            {
                markup += ":";
            }

            console.Markup(markup + " ");
        }
Ejemplo n.º 2
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));
        }