diff --git a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs index b43b0f6..a1bbf74 100644 --- a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs +++ b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs @@ -23,6 +23,11 @@ namespace Spectre.Console /// public List Choices { get; } + /// + /// Gets the initially selected choices. + /// + public HashSet Selected { get; } + /// /// Gets or sets the converter to get the display string for a choice. By default /// the corresponding is used. @@ -52,6 +57,7 @@ namespace Spectre.Console public MultiSelectionPrompt() { Choices = new List(); + Selected = new HashSet(); } /// @@ -73,7 +79,7 @@ namespace Spectre.Console var converter = Converter ?? TypeConverterHelper.ConvertToString; - var list = new RenderableMultiSelectionList(console, Title, PageSize, Choices, converter, HighlightStyle); + var list = new RenderableMultiSelectionList(console, Title, PageSize, Choices, Selected, converter, HighlightStyle); using (new RenderHookScope(console, list)) { console.Cursor.Hide(); @@ -114,4 +120,4 @@ namespace Spectre.Console .ToList(); } } -} +} \ No newline at end of file diff --git a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs index db37230..4d0609d 100644 --- a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs +++ b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs @@ -44,6 +44,73 @@ namespace Spectre.Console return obj; } + /// + /// Marks an item as selected. + /// + /// The prompt result type. + /// The prompt. + /// The index of the item to select. + /// The same instance so that multiple calls can be chained. + public static MultiSelectionPrompt Select(this MultiSelectionPrompt obj, int index) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (index < 0) + { + throw new ArgumentException("Index must be greater than zero", nameof(index)); + } + + obj.Selected.Add(index); + return obj; + } + + /// + /// Marks multiple items as selected. + /// + /// The prompt result type. + /// The prompt. + /// The indices of the items to select. + /// The same instance so that multiple calls can be chained. + public static MultiSelectionPrompt Select(this MultiSelectionPrompt obj, params int[] indices) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + foreach (var index in indices) + { + Select(obj, index); + } + + return obj; + } + + /// + /// Marks multiple items as selected. + /// + /// The prompt result type. + /// The prompt. + /// The indices of the items to select. + /// The same instance so that multiple calls can be chained. + public static MultiSelectionPrompt Select(this MultiSelectionPrompt obj, IEnumerable indices) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + foreach (var index in indices) + { + Select(obj, index); + } + + return obj; + } + /// /// Adds multiple choices. /// @@ -179,4 +246,4 @@ namespace Spectre.Console return obj; } } -} +} \ No newline at end of file diff --git a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs index 15dbb23..97f184f 100644 --- a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs +++ b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs @@ -17,14 +17,15 @@ namespace Spectre.Console public RenderableMultiSelectionList( IAnsiConsole console, string? title, int pageSize, - List choices, Func? converter, Style? highlightStyle) + List choices, HashSet selections, + Func? converter, Style? highlightStyle) : base(console, pageSize, choices, converter) { _console = console ?? throw new ArgumentNullException(nameof(console)); _title = title; _highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue); - Selections = new HashSet(); + Selections = new HashSet(selections); } public void Select() @@ -52,7 +53,8 @@ namespace Spectre.Console return pageSize; } - protected override IRenderable Build(int pointerIndex, bool scrollable, IEnumerable<(int Original, int Index, string Item)> choices) + protected override IRenderable Build(int pointerIndex, bool scrollable, + IEnumerable<(int Original, int Index, string Item)> choices) { var list = new List(); @@ -98,4 +100,4 @@ namespace Spectre.Console return new Rows(list); } } -} +} \ No newline at end of file