Add an implicit operator to convert from Color to Style (#1160)

This commit is contained in:
Cédric Luthi 2023-05-10 15:20:12 +02:00 committed by GitHub
parent 3ec0fee795
commit 6acf9b8c63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 95 additions and 73 deletions

View File

@ -89,15 +89,15 @@ public sealed class JsonText : JustInTimeRenderable
var context = new JsonBuilderContext(new JsonTextStyles
{
BracesStyle = BracesStyle ?? new Style(Color.Grey),
BracketsStyle = BracketsStyle ?? new Style(Color.Grey),
MemberStyle = MemberStyle ?? new Style(Color.Blue),
ColonStyle = ColonStyle ?? new Style(Color.Yellow),
CommaStyle = CommaStyle ?? new Style(Color.Grey),
StringStyle = StringStyle ?? new Style(Color.Red),
NumberStyle = NumberStyle ?? new Style(Color.Green),
BooleanStyle = BooleanStyle ?? new Style(Color.Green),
NullStyle = NullStyle ?? new Style(Color.Grey),
BracesStyle = BracesStyle ?? Color.Grey,
BracketsStyle = BracketsStyle ?? Color.Grey,
MemberStyle = MemberStyle ?? Color.Blue,
ColonStyle = ColonStyle ?? Color.Yellow,
CommaStyle = CommaStyle ?? Color.Grey,
StringStyle = StringStyle ?? Color.Red,
NumberStyle = NumberStyle ?? Color.Green,
BooleanStyle = BooleanStyle ?? Color.Green,
NullStyle = NullStyle ?? Color.Grey,
});
_syntax.Accept(JsonBuilder.Shared, context);

View File

@ -11,7 +11,7 @@ public sealed class ElapsedTimeColumn : ProgressColumn
/// <summary>
/// Gets or sets the style of the remaining time text.
/// </summary>
public Style Style { get; set; } = new Style(foreground: Color.Blue);
public Style Style { get; set; } = Color.Blue;
/// <inheritdoc/>
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)

View File

@ -13,7 +13,7 @@ public sealed class PercentageColumn : ProgressColumn
/// <summary>
/// Gets or sets the style for a completed task.
/// </summary>
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Green);
public Style CompletedStyle { get; set; } = Color.Green;
/// <inheritdoc/>
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)

View File

@ -13,17 +13,17 @@ public sealed class ProgressBarColumn : ProgressColumn
/// <summary>
/// Gets or sets the style of completed portions of the progress bar.
/// </summary>
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
public Style CompletedStyle { get; set; } = Color.Yellow;
/// <summary>
/// Gets or sets the style of a finished progress bar.
/// </summary>
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
public Style FinishedStyle { get; set; } = Color.Green;
/// <summary>
/// Gets or sets the style of remaining portions of the progress bar.
/// </summary>
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
public Style RemainingStyle { get; set; } = Color.Grey;
/// <summary>
/// Gets or sets the style of an indeterminate progress bar.

View File

@ -11,7 +11,7 @@ public sealed class RemainingTimeColumn : ProgressColumn
/// <summary>
/// Gets or sets the style of the remaining time text.
/// </summary>
public Style Style { get; set; } = new Style(foreground: Color.Blue);
public Style Style { get; set; } = Color.Blue;
/// <inheritdoc/>
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)

View File

@ -74,7 +74,7 @@ public sealed class SpinnerColumn : ProgressColumn
/// <summary>
/// Gets or sets the style of the spinner.
/// </summary>
public Style? Style { get; set; } = new Style(foreground: Color.Yellow);
public Style? Style { get; set; } = Color.Yellow;
/// <summary>
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.

View File

@ -15,7 +15,7 @@ public sealed class Status
/// <summary>
/// Gets or sets the spinner style.
/// </summary>
public Style? SpinnerStyle { get; set; } = new Style(foreground: Color.Yellow);
public Style? SpinnerStyle { get; set; } = Color.Yellow;
/// <summary>
/// Gets or sets a value indicating whether or not status

View File

@ -225,7 +225,7 @@ public sealed class MultiSelectionPrompt<T> : IPrompt<List<T>>, IListPromptStrat
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
{
var list = new List<IRenderable>();
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
var highlightStyle = HighlightStyle ?? Color.Blue;
if (Title != null)
{

View File

@ -137,8 +137,8 @@ public sealed class SelectionPrompt<T> : IPrompt<T>, IListPromptStrategy<T>
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
{
var list = new List<IRenderable>();
var disabledStyle = DisabledStyle ?? new Style(foreground: Color.Grey);
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
var disabledStyle = DisabledStyle ?? Color.Grey;
var highlightStyle = HighlightStyle ?? Color.Blue;
if (Title != null)
{

View File

@ -132,6 +132,15 @@ public sealed class Style : IEquatable<Style>
return Parse(style);
}
/// <summary>
/// Implicitly converts <see cref="Color"/> into a <see cref="Style"/> with a foreground color.
/// </summary>
/// <param name="color">The foreground color.</param>
public static implicit operator Style(Color color)
{
return new Style(foreground: color);
}
/// <summary>
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
/// </summary>

View File

@ -153,7 +153,7 @@ public sealed class Calendar : JustInTimeRenderable, IHasCulture, IHasTableBorde
_useSafeBorder = true;
_borderStyle = null;
_culture = CultureInfo.InvariantCulture;
_highlightStyle = new Style(foreground: Color.Blue);
_highlightStyle = Color.Blue;
_showHeader = true;
_calendarEvents = new ListWithCallback<CalendarEvent>(() => MarkAsDirty());
}

View File

@ -13,27 +13,27 @@ public sealed class ExceptionStyle
/// <summary>
/// Gets or sets the exception color.
/// </summary>
public Style Exception { get; set; } = new Style(Color.White);
public Style Exception { get; set; } = Color.White;
/// <summary>
/// Gets or sets the method color.
/// </summary>
public Style Method { get; set; } = new Style(Color.Yellow);
public Style Method { get; set; } = Color.Yellow;
/// <summary>
/// Gets or sets the parameter type color.
/// </summary>
public Style ParameterType { get; set; } = new Style(Color.Blue);
public Style ParameterType { get; set; } = Color.Blue;
/// <summary>
/// Gets or sets the parameter name color.
/// </summary>
public Style ParameterName { get; set; } = new Style(Color.Silver);
public Style ParameterName { get; set; } = Color.Silver;
/// <summary>
/// Gets or sets the parenthesis color.
/// </summary>
public Style Parenthesis { get; set; } = new Style(Color.Silver);
public Style Parenthesis { get; set; } = Color.Silver;
/// <summary>
/// Gets or sets the path color.
@ -43,15 +43,15 @@ public sealed class ExceptionStyle
/// <summary>
/// Gets or sets the line number color.
/// </summary>
public Style LineNumber { get; set; } = new Style(Color.Blue);
public Style LineNumber { get; set; } = Color.Blue;
/// <summary>
/// Gets or sets the color for dimmed text such as "at" or "in".
/// </summary>
public Style Dimmed { get; set; } = new Style(Color.Grey);
public Style Dimmed { get; set; } = Color.Grey;
/// <summary>
/// Gets or sets the color for non emphasized items.
/// </summary>
public Style NonEmphasized { get; set; } = new Style(Color.Silver);
public Style NonEmphasized { get; set; } = Color.Silver;
}

View File

@ -16,9 +16,9 @@ internal sealed class ProgressBar : Renderable, IHasCulture
public bool IsIndeterminate { get; set; }
public CultureInfo? Culture { get; set; }
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
public Style CompletedStyle { get; set; } = Color.Yellow;
public Style FinishedStyle { get; set; } = Color.Green;
public Style RemainingStyle { get; set; } = Color.Grey;
public Style IndeterminateStyle { get; set; } = DefaultPulseStyle;
internal static Style DefaultPulseStyle { get; } = new Style(foreground: Color.DodgerBlue1, background: Color.Grey23);

View File

@ -2,8 +2,8 @@ namespace Spectre.Console;
internal static class TableRenderer
{
private static readonly Style _defaultHeadingStyle = new Style(Color.Silver);
private static readonly Style _defaultCaptionStyle = new Style(Color.Grey);
private static readonly Style _defaultHeadingStyle = Color.Silver;
private static readonly Style _defaultCaptionStyle = Color.Grey;
public static List<Segment> Render(TableRendererContext context, List<int> columnWidths)
{

View File

@ -247,42 +247,42 @@ public sealed class TextPromptTests
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValueCustomMask")]
public Task Should_Choose_Custom_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret_And_Mask_Is_Custom()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret('-')
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValueNullMask")]
public Task Should_Choose_Empty_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret_And_Mask_Is_Null()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret(null)
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValueCustomMask")]
public Task Should_Choose_Custom_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret_And_Mask_Is_Custom()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret('-')
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValueNullMask")]
public Task Should_Choose_Empty_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret_And_Mask_Is_Null()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret(null)
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
@ -337,7 +337,7 @@ public sealed class TextPromptTests
var prompt = new TextPrompt<string>("Enter Value:")
.ShowDefaultValue()
.DefaultValue("default")
.DefaultValueStyle(new Style(foreground: Color.Red));
.DefaultValueStyle(Color.Red);
// When
console.Prompt(prompt);
@ -384,7 +384,7 @@ public sealed class TextPromptTests
.ShowChoices()
.AddChoice("Choice 1")
.AddChoice("Choice 2")
.ChoicesStyle(new Style(foreground: Color.Red));
.ChoicesStyle(Color.Red);
// When
console.Prompt(prompt);

View File

@ -1,7 +1,20 @@
namespace Spectre.Console.Tests.Unit;
public sealed class StyleTests
{
{
[Fact]
public void Should_Convert_From_Color_As_Expected()
{
// Given
Style style;
// When
style = Color.Red;
// Then
style.Foreground.ShouldBe(Color.Red);
}
[Fact]
public void Should_Combine_Two_Styles_As_Expected()
{