Add support for selection prompt highlighting

This commit is contained in:
Nick 2021-01-15 14:23:09 +00:00 committed by GitHub
parent 1a747696a8
commit 63bae278a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 6 deletions

View File

@ -35,6 +35,11 @@ namespace Spectre.Console
/// </summary>
public int PageSize { get; set; } = 10;
/// <summary>
/// Gets or sets the highlight style of the selected choice.
/// </summary>
public Style? HighlightStyle { get; set; }
/// <summary>
/// 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<T>(console, Title, PageSize, Choices, converter);
var list = new RenderableMultiSelectionList<T>(console, Title, PageSize, Choices, converter, HighlightStyle);
using (new RenderHookScope(console, list))
{
console.Cursor.Hide();

View File

@ -103,6 +103,24 @@ namespace Spectre.Console
return obj;
}
/// <summary>
/// Sets the highlight style of the selected choice.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="highlightStyle">The highlight style of the selected choice.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> HighlightStyle<T>(this MultiSelectionPrompt<T> obj, Style highlightStyle)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.HighlightStyle = highlightStyle;
return obj;
}
/// <summary>
/// Requires no choice to be selected.
/// </summary>

View File

@ -17,12 +17,12 @@ namespace Spectre.Console
public RenderableMultiSelectionList(
IAnsiConsole console, string? title, int pageSize,
List<T> choices, Func<T, string>? converter)
List<T> choices, Func<T, string>? 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<int>();
}

View File

@ -12,12 +12,12 @@ namespace Spectre.Console
private readonly string? _title;
private readonly Style _highlightStyle;
public RenderableSelectionList(IAnsiConsole console, string? title, int requestedPageSize, List<T> choices, Func<T, string>? converter)
public RenderableSelectionList(IAnsiConsole console, string? title, int requestedPageSize, List<T> choices, Func<T, string>? 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)

View File

@ -34,6 +34,11 @@ namespace Spectre.Console
/// </summary>
public int PageSize { get; set; } = 10;
/// <summary>
/// Gets or sets the highlight style of the selected choice.
/// </summary>
public Style? HighlightStyle { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class.
/// </summary>
@ -61,7 +66,7 @@ namespace Spectre.Console
var converter = Converter ?? TypeConverterHelper.ConvertToString;
var list = new RenderableSelectionList<T>(console, Title, PageSize, Choices, converter);
var list = new RenderableSelectionList<T>(console, Title, PageSize, Choices, converter, HighlightStyle);
using (new RenderHookScope(console, list))
{
console.Cursor.Hide();

View File

@ -103,6 +103,24 @@ namespace Spectre.Console
return obj;
}
/// <summary>
/// Sets the highlight style of the selected choice.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="highlightStyle">The highlight style of the selected choice.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static SelectionPrompt<T> HighlightStyle<T>(this SelectionPrompt<T> obj, Style highlightStyle)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.HighlightStyle = highlightStyle;
return obj;
}
/// <summary>
/// Sets the function to create a display string for a given choice.
/// </summary>