namespace Spectre.Console;
///
/// Contains extension methods for .
///
public static class TextPromptExtensions
{
///
/// Allow empty input.
///
/// The prompt result type.
/// The prompt.
/// The same instance so that multiple calls can be chained.
public static TextPrompt AllowEmpty(this TextPrompt obj)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.AllowEmpty = true;
return obj;
}
///
/// Sets the prompt style.
///
/// The prompt result type.
/// The prompt.
/// The prompt style.
/// The same instance so that multiple calls can be chained.
public static TextPrompt PromptStyle(this TextPrompt obj, Style style)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
obj.PromptStyle = style;
return obj;
}
///
/// Show or hide choices.
///
/// The prompt result type.
/// The prompt.
/// Whether or not choices should be visible.
/// The same instance so that multiple calls can be chained.
public static TextPrompt ShowChoices(this TextPrompt obj, bool show)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.ShowChoices = show;
return obj;
}
///
/// Shows choices.
///
/// The prompt result type.
/// The prompt.
/// The same instance so that multiple calls can be chained.
public static TextPrompt ShowChoices(this TextPrompt obj)
{
return ShowChoices(obj, true);
}
///
/// Hides choices.
///
/// The prompt result type.
/// The prompt.
/// The same instance so that multiple calls can be chained.
public static TextPrompt HideChoices(this TextPrompt obj)
{
return ShowChoices(obj, false);
}
///
/// Show or hide the default value.
///
/// The prompt result type.
/// The prompt.
/// Whether or not the default value should be visible.
/// The same instance so that multiple calls can be chained.
public static TextPrompt ShowDefaultValue(this TextPrompt obj, bool show)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.ShowDefaultValue = show;
return obj;
}
///
/// Shows the default value.
///
/// The prompt result type.
/// The prompt.
/// The same instance so that multiple calls can be chained.
public static TextPrompt ShowDefaultValue(this TextPrompt obj)
{
return ShowDefaultValue(obj, true);
}
///
/// Hides the default value.
///
/// The prompt result type.
/// The prompt.
/// The same instance so that multiple calls can be chained.
public static TextPrompt HideDefaultValue(this TextPrompt obj)
{
return ShowDefaultValue(obj, false);
}
///
/// Sets the validation error message for the prompt.
///
/// The prompt result type.
/// The prompt.
/// The validation error message.
/// The same instance so that multiple calls can be chained.
public static TextPrompt ValidationErrorMessage(this TextPrompt obj, string message)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.ValidationErrorMessage = message;
return obj;
}
///
/// Sets the "invalid choice" message for the prompt.
///
/// The prompt result type.
/// The prompt.
/// The "invalid choice" message.
/// The same instance so that multiple calls can be chained.
public static TextPrompt InvalidChoiceMessage(this TextPrompt obj, string message)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.InvalidChoiceMessage = message;
return obj;
}
///
/// Sets the default value of the prompt.
///
/// The prompt result type.
/// The prompt.
/// The default value.
/// The same instance so that multiple calls can be chained.
public static TextPrompt DefaultValue(this TextPrompt obj, T value)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.DefaultValue = new DefaultPromptValue(value);
return obj;
}
///
/// Sets the validation criteria for the prompt.
///
/// The prompt result type.
/// The prompt.
/// The validation criteria.
/// The validation error message.
/// The same instance so that multiple calls can be chained.
public static TextPrompt Validate(this TextPrompt obj, Func validator, string? message = null)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Validator = result =>
{
if (validator(result))
{
return ValidationResult.Success();
}
return ValidationResult.Error(message);
};
return obj;
}
///
/// Sets the validation criteria for the prompt.
///
/// The prompt result type.
/// The prompt.
/// The validation criteria.
/// The same instance so that multiple calls can be chained.
public static TextPrompt Validate(this TextPrompt obj, Func validator)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Validator = validator;
return obj;
}
///
/// Adds a choice to the prompt.
///
/// The prompt result type.
/// The prompt.
/// The choice to add.
/// The same instance so that multiple calls can be chained.
public static TextPrompt AddChoice(this TextPrompt obj, T choice)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Choices.Add(choice);
return obj;
}
///
/// Adds multiple choices to the prompt.
///
/// The prompt result type.
/// The prompt.
/// The choices to add.
/// The same instance so that multiple calls can be chained.
public static TextPrompt AddChoices(this TextPrompt obj, IEnumerable choices)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
if (choices is null)
{
throw new ArgumentNullException(nameof(choices));
}
foreach (var choice in choices)
{
obj.Choices.Add(choice);
}
return obj;
}
///
/// Replaces prompt user input with asterisks in the console.
///
/// The prompt type.
/// The prompt.
/// The same instance so that multiple calls can be chained.
public static TextPrompt Secret(this TextPrompt obj)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.IsSecret = true;
return obj;
}
///
/// Replaces prompt user input with mask in the console.
///
/// The prompt type.
/// The prompt.
/// The masking character to use for the secret.
/// The same instance so that multiple calls can be chained.
public static TextPrompt Secret(this TextPrompt obj, char? mask)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.IsSecret = true;
obj.Mask = mask;
return obj;
}
///
/// Sets the function to create a display string for a given choice.
///
/// The prompt type.
/// The prompt.
/// The function to get a display string for a given choice.
/// The same instance so that multiple calls can be chained.
public static TextPrompt WithConverter(this TextPrompt obj, Func? displaySelector)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Converter = displaySelector;
return obj;
}
///
/// Sets the style in which the default value is displayed.
///
/// The prompt result type.
/// The prompt.
/// The default value style or to use the default style (green).
/// The same instance so that multiple calls can be chained.
public static TextPrompt DefaultValueStyle(this TextPrompt obj, Style? style)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.DefaultValueStyle = style;
return obj;
}
///
/// Sets the style in which the list of choices is displayed.
///
/// The prompt result type.
/// The prompt.
/// The style to use for displaying the choices or to use the default style (blue).
/// The same instance so that multiple calls can be chained.
public static TextPrompt ChoicesStyle(this TextPrompt obj, Style? style)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.ChoicesStyle = style;
return obj;
}
}