From 9cd7b24e652c4010450f8ff7874db606db9b35d0 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Sat, 11 Feb 2023 16:59:16 +0100 Subject: [PATCH] Allow configuration of confirmation prompt comparison via StringComparer --- src/Spectre.Console/Prompts/ConfirmationPrompt.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Spectre.Console/Prompts/ConfirmationPrompt.cs b/src/Spectre.Console/Prompts/ConfirmationPrompt.cs index d1f5b82..9257858 100644 --- a/src/Spectre.Console/Prompts/ConfirmationPrompt.cs +++ b/src/Spectre.Console/Prompts/ConfirmationPrompt.cs @@ -40,10 +40,13 @@ public sealed class ConfirmationPrompt : IPrompt public bool ShowDefaultValue { get; set; } = true; /// - /// Gets or sets a value indicating whether the confirmation - /// should use case insensitive matching. + /// Gets or sets the string comparer to use when comparing user input + /// against Yes/No choices. /// - public bool CaseInsensitive { get; set; } = true; + /// + /// Defaults to . + /// + public StringComparer ChoiceComparer { get; set; } = StringComparer.CurrentCultureIgnoreCase; /// /// Initializes a new instance of the class. @@ -63,8 +66,7 @@ public sealed class ConfirmationPrompt : IPrompt /// public async Task ShowAsync(IAnsiConsole console, CancellationToken cancellationToken) { - var comparer = CaseInsensitive ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture; - var prompt = new TextPrompt(_prompt, comparer) + var prompt = new TextPrompt(_prompt, ChoiceComparer) .InvalidChoiceMessage(InvalidChoiceMessage) .ValidationErrorMessage(InvalidChoiceMessage) .ShowChoices(ShowChoices) @@ -75,6 +77,6 @@ public sealed class ConfirmationPrompt : IPrompt var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false); - return Yes.ToString().Equals(result.ToString(), CaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture); + return ChoiceComparer.Compare(Yes.ToString(), result.ToString()) == 0; } } \ No newline at end of file