Пример #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 choices = string.Join("/", Choices.Select(choice => TextPrompt <T> .GetTypeConverter().ConvertToInvariantString(choice)));
                builder.AppendFormat(CultureInfo.InvariantCulture, " [blue][[{0}]][/]", choices);
            }

            if (ShowDefaultValue && DefaultValue != null)
            {
                builder.AppendFormat(
                    CultureInfo.InvariantCulture,
                    " [green]({0})[/]",
                    TextPrompt <T> .GetTypeConverter().ConvertToInvariantString(DefaultValue.Value));
            }

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

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

            console.Markup(markup + " ");
        }
Пример #2
0
        private static bool TryConvert(string input, [MaybeNull] out T result)
        {
            try
            {
                result = (T)TextPrompt <T> .GetTypeConverter().ConvertFromInvariantString(input);

                return(true);
            }
            catch
            {
#pragma warning disable CS8601 // Possible null reference assignment.
                result = default;
#pragma warning restore CS8601 // Possible null reference assignment.
                return(false);
            }
        }
Пример #3
0
        /// <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);
            }
        }