using System; 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; } /// /// Replaces prompt user input with asterixes 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; } } }