using System; using System.Collections.Generic; namespace Spectre.Console { /// /// Contains extension methods for . /// public static class MultiSelectionPromptExtensions { /// /// Sets the selection mode. /// /// The prompt result type. /// The prompt. /// The selection mode. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt Mode(this MultiSelectionPrompt obj, SelectionMode mode) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.Mode = mode; return obj; } /// /// Adds a choice. /// /// The prompt result type. /// The prompt. /// The choice to add. /// The configurator for the choice. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt AddChoices(this MultiSelectionPrompt obj, T choice, Action> configurator) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } if (configurator is null) { throw new ArgumentNullException(nameof(configurator)); } var result = obj.AddChoice(choice); configurator(result); return obj; } /// /// Adds multiple choices. /// /// The prompt result type. /// The prompt. /// The choices to add. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt AddChoices(this MultiSelectionPrompt obj, params T[] choices) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } foreach (var choice in choices) { obj.AddChoice(choice); } return obj; } /// /// Adds multiple choices. /// /// The prompt result type. /// The prompt. /// The choices to add. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt AddChoices(this MultiSelectionPrompt obj, IEnumerable choices) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } foreach (var choice in choices) { obj.AddChoice(choice); } return obj; } /// /// Adds multiple grouped choices. /// /// The prompt result type. /// The prompt. /// The group. /// The choices to add. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt AddChoiceGroup(this MultiSelectionPrompt obj, T group, IEnumerable choices) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } var root = obj.AddChoice(group); foreach (var choice in choices) { root.AddChild(choice); } return obj; } /// /// Marks an item as selected. /// /// The prompt result type. /// The prompt. /// The item to select. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt Select(this MultiSelectionPrompt obj, T item) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } var node = obj.Tree.Find(item); node?.Select(); return obj; } /// /// Sets the title. /// /// The prompt result type. /// The prompt. /// The title markup text. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt Title(this MultiSelectionPrompt obj, string? title) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.Title = title; return obj; } /// /// Sets how many choices that are displayed to the user. /// /// The prompt result type. /// The prompt. /// The number of choices that are displayed to the user. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt PageSize(this MultiSelectionPrompt obj, int pageSize) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } if (pageSize <= 2) { throw new ArgumentException("Page size must be greater or equal to 3.", nameof(pageSize)); } obj.PageSize = pageSize; return obj; } /// /// Sets the highlight style of the selected choice. /// /// The prompt result type. /// The prompt. /// The highlight style of the selected choice. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt HighlightStyle(this MultiSelectionPrompt obj, Style highlightStyle) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.HighlightStyle = highlightStyle; return obj; } /// /// Sets the text that will be displayed if there are more choices to show. /// /// The prompt result type. /// The prompt. /// The text to display. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt MoreChoicesText(this MultiSelectionPrompt obj, string? text) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.MoreChoicesText = text; return obj; } /// /// Sets the text that instructs the user of how to select items. /// /// The prompt result type. /// The prompt. /// The text to display. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt InstructionsText(this MultiSelectionPrompt obj, string? text) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.InstructionsText = text; return obj; } /// /// Requires no choice to be selected. /// /// The prompt result type. /// The prompt. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt NotRequired(this MultiSelectionPrompt obj) where T : notnull { return Required(obj, false); } /// /// Requires a choice to be selected. /// /// The prompt result type. /// The prompt. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt Required(this MultiSelectionPrompt obj) where T : notnull { return Required(obj, true); } /// /// Sets a value indicating whether or not at least one choice must be selected. /// /// The prompt result type. /// The prompt. /// Whether or not at least one choice must be selected. /// The same instance so that multiple calls can be chained. public static MultiSelectionPrompt Required(this MultiSelectionPrompt obj, bool required) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.Required = required; 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 MultiSelectionPrompt UseConverter(this MultiSelectionPrompt obj, Func? displaySelector) where T : notnull { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } obj.Converter = displaySelector; return obj; } } }