namespace Spectre.Console; /// /// A prompt that is answered with a yes or no. /// public sealed class ConfirmationPrompt : IPrompt { private readonly string _prompt; /// /// Gets or sets the character that represents "yes". /// public char Yes { get; set; } = 'y'; /// /// Gets or sets the character that represents "no". /// public char No { get; set; } = 'n'; /// /// Gets or sets a value indicating whether "yes" is the default answer. /// public bool DefaultValue { get; set; } = true; /// /// Gets or sets the message for invalid choices. /// public string InvalidChoiceMessage { get; set; } = "[red]Please select one of the available options[/]"; /// /// Gets or sets a value indicating whether or not /// choices should be shown. /// public bool ShowChoices { get; set; } = true; /// /// Gets or sets a value indicating whether or not /// default values should be shown. /// public bool ShowDefaultValue { get; set; } = true; /// /// Gets or sets the style in which the default value is displayed. Defaults to green when . /// public Style? DefaultValueStyle { get; set; } /// /// Gets or sets the style in which the list of choices is displayed. Defaults to blue when . /// public Style? ChoicesStyle { get; set; } /// /// Gets or sets the string comparer to use when comparing user input /// against Yes/No choices. /// /// /// Defaults to . /// public StringComparer Comparer { get; set; } = StringComparer.CurrentCultureIgnoreCase; /// /// Initializes a new instance of the class. /// /// The prompt markup text. public ConfirmationPrompt(string prompt) { _prompt = prompt ?? throw new System.ArgumentNullException(nameof(prompt)); } /// public bool Show(IAnsiConsole console) { return ShowAsync(console, CancellationToken.None).GetAwaiter().GetResult(); } /// public async Task ShowAsync(IAnsiConsole console, CancellationToken cancellationToken) { var comparer = Comparer ?? StringComparer.CurrentCultureIgnoreCase; var prompt = new TextPrompt(_prompt, comparer) .InvalidChoiceMessage(InvalidChoiceMessage) .ValidationErrorMessage(InvalidChoiceMessage) .ShowChoices(ShowChoices) .ChoicesStyle(ChoicesStyle) .ShowDefaultValue(ShowDefaultValue) .DefaultValue(DefaultValue ? Yes : No) .DefaultValueStyle(DefaultValueStyle) .AddChoice(Yes) .AddChoice(No); var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false); return comparer.Compare(Yes.ToString(), result.ToString()) == 0; } }