mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-01 01:25:27 +08:00 
			
		
		
		
	Add support for selection prompt highlighting
This commit is contained in:
		| @@ -35,6 +35,11 @@ namespace Spectre.Console | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         public int PageSize { get; set; } = 10; |         public int PageSize { get; set; } = 10; | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets or sets the highlight style of the selected choice. | ||||||
|  |         /// </summary> | ||||||
|  |         public Style? HighlightStyle { get; set; } | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets or sets a value indicating whether or not |         /// Gets or sets a value indicating whether or not | ||||||
|         /// at least one selection is required. |         /// at least one selection is required. | ||||||
| @@ -68,7 +73,7 @@ namespace Spectre.Console | |||||||
|  |  | ||||||
|             var converter = Converter ?? TypeConverterHelper.ConvertToString; |             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)) |             using (new RenderHookScope(console, list)) | ||||||
|             { |             { | ||||||
|                 console.Cursor.Hide(); |                 console.Cursor.Hide(); | ||||||
|   | |||||||
| @@ -103,6 +103,24 @@ namespace Spectre.Console | |||||||
|             return obj; |             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> |         /// <summary> | ||||||
|         /// Requires no choice to be selected. |         /// Requires no choice to be selected. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|   | |||||||
| @@ -17,12 +17,12 @@ namespace Spectre.Console | |||||||
|  |  | ||||||
|         public RenderableMultiSelectionList( |         public RenderableMultiSelectionList( | ||||||
|             IAnsiConsole console, string? title, int pageSize, |             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) |             : base(console, pageSize, choices, converter) | ||||||
|         { |         { | ||||||
|             _console = console ?? throw new ArgumentNullException(nameof(console)); |             _console = console ?? throw new ArgumentNullException(nameof(console)); | ||||||
|             _title = title; |             _title = title; | ||||||
|             _highlightStyle = new Style(foreground: Color.Blue); |             _highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue); | ||||||
|  |  | ||||||
|             Selections = new HashSet<int>(); |             Selections = new HashSet<int>(); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -12,12 +12,12 @@ namespace Spectre.Console | |||||||
|         private readonly string? _title; |         private readonly string? _title; | ||||||
|         private readonly Style _highlightStyle; |         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) |             : base(console, requestedPageSize, choices, converter) | ||||||
|         { |         { | ||||||
|             _console = console ?? throw new ArgumentNullException(nameof(console)); |             _console = console ?? throw new ArgumentNullException(nameof(console)); | ||||||
|             _title = title; |             _title = title; | ||||||
|             _highlightStyle = new Style(foreground: Color.Blue); |             _highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         protected override int CalculatePageSize(int requestedPageSize) |         protected override int CalculatePageSize(int requestedPageSize) | ||||||
|   | |||||||
| @@ -34,6 +34,11 @@ namespace Spectre.Console | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         public int PageSize { get; set; } = 10; |         public int PageSize { get; set; } = 10; | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets or sets the highlight style of the selected choice. | ||||||
|  |         /// </summary> | ||||||
|  |         public Style? HighlightStyle { get; set; } | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class. |         /// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class. | ||||||
|         /// </summary> |         /// </summary> | ||||||
| @@ -61,7 +66,7 @@ namespace Spectre.Console | |||||||
|  |  | ||||||
|             var converter = Converter ?? TypeConverterHelper.ConvertToString; |             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)) |             using (new RenderHookScope(console, list)) | ||||||
|             { |             { | ||||||
|                 console.Cursor.Hide(); |                 console.Cursor.Hide(); | ||||||
|   | |||||||
| @@ -103,6 +103,24 @@ namespace Spectre.Console | |||||||
|             return obj; |             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> |         /// <summary> | ||||||
|         /// Sets the function to create a display string for a given choice. |         /// Sets the function to create a display string for a given choice. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Nick
					Nick