mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-14 16:02:50 +08:00
Allow configuration of confirmation prompt comparison via StringComparer
This commit is contained in:
parent
04610cf492
commit
9cd7b24e65
@ -40,10 +40,13 @@ public sealed class ConfirmationPrompt : IPrompt<bool>
|
||||
public bool ShowDefaultValue { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public bool CaseInsensitive { get; set; } = true;
|
||||
/// <remarks>
|
||||
/// Defaults to <see cref="StringComparer.CurrentCultureIgnoreCase"/>.
|
||||
/// </remarks>
|
||||
public StringComparer ChoiceComparer { get; set; } = StringComparer.CurrentCultureIgnoreCase;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConfirmationPrompt"/> class.
|
||||
@ -63,8 +66,7 @@ public sealed class ConfirmationPrompt : IPrompt<bool>
|
||||
/// <inheritdoc/>
|
||||
public async Task<bool> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||
{
|
||||
var comparer = CaseInsensitive ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture;
|
||||
var prompt = new TextPrompt<char>(_prompt, comparer)
|
||||
var prompt = new TextPrompt<char>(_prompt, ChoiceComparer)
|
||||
.InvalidChoiceMessage(InvalidChoiceMessage)
|
||||
.ValidationErrorMessage(InvalidChoiceMessage)
|
||||
.ShowChoices(ShowChoices)
|
||||
@ -75,6 +77,6 @@ public sealed class ConfirmationPrompt : IPrompt<bool>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user