mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52: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;
|
public bool ShowDefaultValue { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether the confirmation
|
/// Gets or sets the string comparer to use when comparing user input
|
||||||
/// should use case insensitive matching.
|
/// against Yes/No choices.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CaseInsensitive { get; set; } = true;
|
/// <remarks>
|
||||||
|
/// Defaults to <see cref="StringComparer.CurrentCultureIgnoreCase"/>.
|
||||||
|
/// </remarks>
|
||||||
|
public StringComparer ChoiceComparer { get; set; } = StringComparer.CurrentCultureIgnoreCase;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ConfirmationPrompt"/> class.
|
/// Initializes a new instance of the <see cref="ConfirmationPrompt"/> class.
|
||||||
@ -63,8 +66,7 @@ public sealed class ConfirmationPrompt : IPrompt<bool>
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<bool> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
public async Task<bool> ShowAsync(IAnsiConsole console, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var comparer = CaseInsensitive ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture;
|
var prompt = new TextPrompt<char>(_prompt, ChoiceComparer)
|
||||||
var prompt = new TextPrompt<char>(_prompt, comparer)
|
|
||||||
.InvalidChoiceMessage(InvalidChoiceMessage)
|
.InvalidChoiceMessage(InvalidChoiceMessage)
|
||||||
.ValidationErrorMessage(InvalidChoiceMessage)
|
.ValidationErrorMessage(InvalidChoiceMessage)
|
||||||
.ShowChoices(ShowChoices)
|
.ShowChoices(ShowChoices)
|
||||||
@ -75,6 +77,6 @@ public sealed class ConfirmationPrompt : IPrompt<bool>
|
|||||||
|
|
||||||
var result = await prompt.ShowAsync(console, cancellationToken).ConfigureAwait(false);
|
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