From 9cd7b24e652c4010450f8ff7874db606db9b35d0 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Sat, 11 Feb 2023 16:59:16 +0100 Subject: [PATCH 1/2] 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 From 507573256430525a933624953c4aa7033de412fd Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Thu, 16 Mar 2023 09:45:42 +0100 Subject: [PATCH 2/2] Adjustments based on code review --- src/Spectre.Console/Prompts/ConfirmationPrompt.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Spectre.Console/Prompts/ConfirmationPrompt.cs b/src/Spectre.Console/Prompts/ConfirmationPrompt.cs index 9257858..4909ee2 100644 --- a/src/Spectre.Console/Prompts/ConfirmationPrompt.cs +++ b/src/Spectre.Console/Prompts/ConfirmationPrompt.cs @@ -46,7 +46,7 @@ public sealed class ConfirmationPrompt : IPrompt /// /// Defaults to . /// - public StringComparer ChoiceComparer { get; set; } = StringComparer.CurrentCultureIgnoreCase; + public StringComparer Comparer { get; set; } = StringComparer.CurrentCultureIgnoreCase; /// /// Initializes a new instance of the class. @@ -66,7 +66,9 @@ public sealed class ConfirmationPrompt : IPrompt /// public async Task ShowAsync(IAnsiConsole console, CancellationToken cancellationToken) { - var prompt = new TextPrompt(_prompt, ChoiceComparer) + var comparer = Comparer ?? StringComparer.CurrentCultureIgnoreCase; + + var prompt = new TextPrompt(_prompt, comparer) .InvalidChoiceMessage(InvalidChoiceMessage) .ValidationErrorMessage(InvalidChoiceMessage) .ShowChoices(ShowChoices) @@ -77,6 +79,6 @@ public sealed class ConfirmationPrompt : IPrompt var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false); - return ChoiceComparer.Compare(Yes.ToString(), result.ToString()) == 0; + return comparer.Compare(Yes.ToString(), result.ToString()) == 0; } } \ No newline at end of file