Skip to content

Commit

Permalink
Add an implicit operator to convert from Color to Style (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced authored May 10, 2023
1 parent 3ec0fee commit 6acf9b8
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 73 deletions.
18 changes: 9 additions & 9 deletions src/Spectre.Console.Json/JsonText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ protected override IRenderable Build()

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Live/Progress/Columns/SpinnerColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public string? PendingText
/// <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.
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Live/Status/Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Prompts/MultiSelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItem
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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItem
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)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Spectre.Console/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ public static implicit operator Style(string 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>
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Widgets/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Calendar(int year, int month, int day)
_useSafeBorder = true;
_borderStyle = null;
_culture = CultureInfo.InvariantCulture;
_highlightStyle = new Style(foreground: Color.Blue);
_highlightStyle = Color.Blue;
_showHeader = true;
_calendarEvents = new ListWithCallback<CalendarEvent>(() => MarkAsDirty());
}
Expand Down
16 changes: 8 additions & 8 deletions src/Spectre.Console/Widgets/Exceptions/ExceptionStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
6 changes: 3 additions & 3 deletions src/Spectre.Console/Widgets/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Spectre.Console/Widgets/Table/TableRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
76 changes: 38 additions & 38 deletions test/Spectre.Console.Tests/Unit/Prompts/TextPromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,42 +247,42 @@ public Task Should_Choose_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_

// 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]
Expand Down Expand Up @@ -337,7 +337,7 @@ public Task Uses_specified_default_value_style()
var prompt = new TextPrompt<string>("Enter Value:")
.ShowDefaultValue()
.DefaultValue("default")
.DefaultValueStyle(new Style(foreground: Color.Red));
.DefaultValueStyle(Color.Red);

// When
console.Prompt(prompt);
Expand Down Expand Up @@ -384,7 +384,7 @@ public Task Uses_the_specified_choices_style()
.ShowChoices()
.AddChoice("Choice 1")
.AddChoice("Choice 2")
.ChoicesStyle(new Style(foreground: Color.Red));
.ChoicesStyle(Color.Red);

// When
console.Prompt(prompt);
Expand Down
15 changes: 14 additions & 1 deletion test/Spectre.Console.Tests/Unit/StyleTests.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand Down

0 comments on commit 6acf9b8

Please sign in to comment.