From 63bae278a9a271d9c4a03a22e796f9b9117306ca Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 15 Jan 2021 14:23:09 +0000 Subject: [PATCH] Add support for selection prompt highlighting --- .../Widgets/Prompt/MultiSelectionPrompt.cs | 7 ++++++- .../Prompt/MultiSelectionPromptExtensions.cs | 18 ++++++++++++++++++ .../Rendering/RenderableMultiSelectionList.cs | 4 ++-- .../Rendering/RenderableSelectionList.cs | 4 ++-- .../Widgets/Prompt/SelectionPrompt.cs | 7 ++++++- .../Prompt/SelectionPromptExtensions.cs | 18 ++++++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs index a7c960d..b43b0f6 100644 --- a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs +++ b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs @@ -35,6 +35,11 @@ namespace Spectre.Console /// public int PageSize { get; set; } = 10; + /// + /// Gets or sets the highlight style of the selected choice. + /// + public Style? HighlightStyle { get; set; } + /// /// Gets or sets a value indicating whether or not /// at least one selection is required. @@ -68,7 +73,7 @@ namespace Spectre.Console var converter = Converter ?? TypeConverterHelper.ConvertToString; - var list = new RenderableMultiSelectionList(console, Title, PageSize, Choices, converter); + var list = new RenderableMultiSelectionList(console, Title, PageSize, Choices, converter, HighlightStyle); using (new RenderHookScope(console, list)) { console.Cursor.Hide(); diff --git a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs index 410172d..db37230 100644 --- a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs +++ b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs @@ -103,6 +103,24 @@ namespace Spectre.Console 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) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + obj.HighlightStyle = highlightStyle; + return obj; + } + /// /// Requires no choice to be selected. /// diff --git a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs index 1b1e9b3..15dbb23 100644 --- a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs +++ b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs @@ -17,12 +17,12 @@ namespace Spectre.Console public RenderableMultiSelectionList( IAnsiConsole console, string? title, int pageSize, - List choices, Func? converter) + List choices, Func? converter, Style? highlightStyle) : base(console, pageSize, choices, converter) { _console = console ?? throw new ArgumentNullException(nameof(console)); _title = title; - _highlightStyle = new Style(foreground: Color.Blue); + _highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue); Selections = new HashSet(); } diff --git a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs index 508ab9d..b9696f9 100644 --- a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs +++ b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs @@ -12,12 +12,12 @@ namespace Spectre.Console private readonly string? _title; private readonly Style _highlightStyle; - public RenderableSelectionList(IAnsiConsole console, string? title, int requestedPageSize, List choices, Func? converter) + public RenderableSelectionList(IAnsiConsole console, string? title, int requestedPageSize, List choices, Func? converter, Style? highlightStyle) : base(console, requestedPageSize, choices, converter) { _console = console ?? throw new ArgumentNullException(nameof(console)); _title = title; - _highlightStyle = new Style(foreground: Color.Blue); + _highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue); } protected override int CalculatePageSize(int requestedPageSize) diff --git a/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs b/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs index 1feaaee..3cb6b9c 100644 --- a/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs +++ b/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs @@ -34,6 +34,11 @@ namespace Spectre.Console /// public int PageSize { get; set; } = 10; + /// + /// Gets or sets the highlight style of the selected choice. + /// + public Style? HighlightStyle { get; set; } + /// /// Initializes a new instance of the class. /// @@ -61,7 +66,7 @@ namespace Spectre.Console var converter = Converter ?? TypeConverterHelper.ConvertToString; - var list = new RenderableSelectionList(console, Title, PageSize, Choices, converter); + var list = new RenderableSelectionList(console, Title, PageSize, Choices, converter, HighlightStyle); using (new RenderHookScope(console, list)) { console.Cursor.Hide(); diff --git a/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs b/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs index 3590226..cbbc8f4 100644 --- a/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs +++ b/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs @@ -103,6 +103,24 @@ namespace Spectre.Console 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 SelectionPrompt HighlightStyle(this SelectionPrompt obj, Style highlightStyle) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + obj.HighlightStyle = highlightStyle; + return obj; + } + /// /// Sets the function to create a display string for a given choice. ///