mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00
Add support for selection prompt highlighting
This commit is contained in:
parent
1a747696a8
commit
63bae278a9
@ -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();
|
||||
|
@ -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>
|
||||
|
@ -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>();
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user