mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
Add an implicit operator to convert from Color to Style (#1160)
This commit is contained in:
parent
3ec0fee795
commit
6acf9b8c63
@ -89,15 +89,15 @@ public sealed class JsonText : JustInTimeRenderable
|
|||||||
|
|
||||||
var context = new JsonBuilderContext(new JsonTextStyles
|
var context = new JsonBuilderContext(new JsonTextStyles
|
||||||
{
|
{
|
||||||
BracesStyle = BracesStyle ?? new Style(Color.Grey),
|
BracesStyle = BracesStyle ?? Color.Grey,
|
||||||
BracketsStyle = BracketsStyle ?? new Style(Color.Grey),
|
BracketsStyle = BracketsStyle ?? Color.Grey,
|
||||||
MemberStyle = MemberStyle ?? new Style(Color.Blue),
|
MemberStyle = MemberStyle ?? Color.Blue,
|
||||||
ColonStyle = ColonStyle ?? new Style(Color.Yellow),
|
ColonStyle = ColonStyle ?? Color.Yellow,
|
||||||
CommaStyle = CommaStyle ?? new Style(Color.Grey),
|
CommaStyle = CommaStyle ?? Color.Grey,
|
||||||
StringStyle = StringStyle ?? new Style(Color.Red),
|
StringStyle = StringStyle ?? Color.Red,
|
||||||
NumberStyle = NumberStyle ?? new Style(Color.Green),
|
NumberStyle = NumberStyle ?? Color.Green,
|
||||||
BooleanStyle = BooleanStyle ?? new Style(Color.Green),
|
BooleanStyle = BooleanStyle ?? Color.Green,
|
||||||
NullStyle = NullStyle ?? new Style(Color.Grey),
|
NullStyle = NullStyle ?? Color.Grey,
|
||||||
});
|
});
|
||||||
|
|
||||||
_syntax.Accept(JsonBuilder.Shared, context);
|
_syntax.Accept(JsonBuilder.Shared, context);
|
||||||
|
@ -11,7 +11,7 @@ public sealed class ElapsedTimeColumn : ProgressColumn
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of the remaining time text.
|
/// Gets or sets the style of the remaining time text.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style Style { get; set; } = new Style(foreground: Color.Blue);
|
public Style Style { get; set; } = Color.Blue;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)
|
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)
|
||||||
|
@ -13,7 +13,7 @@ public sealed class PercentageColumn : ProgressColumn
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style for a completed task.
|
/// Gets or sets the style for a completed task.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Green);
|
public Style CompletedStyle { get; set; } = Color.Green;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)
|
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)
|
||||||
|
@ -13,17 +13,17 @@ public sealed class ProgressBarColumn : ProgressColumn
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of completed portions of the progress bar.
|
/// Gets or sets the style of completed portions of the progress bar.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
|
public Style CompletedStyle { get; set; } = Color.Yellow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of a finished progress bar.
|
/// Gets or sets the style of a finished progress bar.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
|
public Style FinishedStyle { get; set; } = Color.Green;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of remaining portions of the progress bar.
|
/// Gets or sets the style of remaining portions of the progress bar.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
|
public Style RemainingStyle { get; set; } = Color.Grey;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of an indeterminate progress bar.
|
/// Gets or sets the style of an indeterminate progress bar.
|
||||||
|
@ -11,7 +11,7 @@ public sealed class RemainingTimeColumn : ProgressColumn
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of the remaining time text.
|
/// Gets or sets the style of the remaining time text.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style Style { get; set; } = new Style(foreground: Color.Blue);
|
public Style Style { get; set; } = Color.Blue;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)
|
public override IRenderable Render(RenderOptions options, ProgressTask task, TimeSpan deltaTime)
|
||||||
|
@ -74,7 +74,7 @@ public sealed class SpinnerColumn : ProgressColumn
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the style of the spinner.
|
/// Gets or sets the style of the spinner.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style? Style { get; set; } = new Style(foreground: Color.Yellow);
|
public Style? Style { get; set; } = Color.Yellow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.
|
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.
|
||||||
|
@ -15,7 +15,7 @@ public sealed class Status
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the spinner style.
|
/// Gets or sets the spinner style.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style? SpinnerStyle { get; set; } = new Style(foreground: Color.Yellow);
|
public Style? SpinnerStyle { get; set; } = Color.Yellow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not status
|
/// Gets or sets a value indicating whether or not status
|
||||||
|
@ -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)
|
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
|
||||||
{
|
{
|
||||||
var list = new List<IRenderable>();
|
var list = new List<IRenderable>();
|
||||||
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
|
var highlightStyle = HighlightStyle ?? Color.Blue;
|
||||||
|
|
||||||
if (Title != null)
|
if (Title != null)
|
||||||
{
|
{
|
||||||
|
@ -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)
|
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex, IEnumerable<(int Index, ListPromptItem<T> Node)> items)
|
||||||
{
|
{
|
||||||
var list = new List<IRenderable>();
|
var list = new List<IRenderable>();
|
||||||
var disabledStyle = DisabledStyle ?? new Style(foreground: Color.Grey);
|
var disabledStyle = DisabledStyle ?? Color.Grey;
|
||||||
var highlightStyle = HighlightStyle ?? new Style(foreground: Color.Blue);
|
var highlightStyle = HighlightStyle ?? Color.Blue;
|
||||||
|
|
||||||
if (Title != null)
|
if (Title != null)
|
||||||
{
|
{
|
||||||
|
@ -132,6 +132,15 @@ public sealed class Style : IEquatable<Style>
|
|||||||
return Parse(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>
|
/// <summary>
|
||||||
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
|
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -153,7 +153,7 @@ public sealed class Calendar : JustInTimeRenderable, IHasCulture, IHasTableBorde
|
|||||||
_useSafeBorder = true;
|
_useSafeBorder = true;
|
||||||
_borderStyle = null;
|
_borderStyle = null;
|
||||||
_culture = CultureInfo.InvariantCulture;
|
_culture = CultureInfo.InvariantCulture;
|
||||||
_highlightStyle = new Style(foreground: Color.Blue);
|
_highlightStyle = Color.Blue;
|
||||||
_showHeader = true;
|
_showHeader = true;
|
||||||
_calendarEvents = new ListWithCallback<CalendarEvent>(() => MarkAsDirty());
|
_calendarEvents = new ListWithCallback<CalendarEvent>(() => MarkAsDirty());
|
||||||
}
|
}
|
||||||
|
@ -13,27 +13,27 @@ public sealed class ExceptionStyle
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the exception color.
|
/// Gets or sets the exception color.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style Exception { get; set; } = new Style(Color.White);
|
public Style Exception { get; set; } = Color.White;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the method color.
|
/// Gets or sets the method color.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style Method { get; set; } = new Style(Color.Yellow);
|
public Style Method { get; set; } = Color.Yellow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the parameter type color.
|
/// Gets or sets the parameter type color.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style ParameterType { get; set; } = new Style(Color.Blue);
|
public Style ParameterType { get; set; } = Color.Blue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the parameter name color.
|
/// Gets or sets the parameter name color.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style ParameterName { get; set; } = new Style(Color.Silver);
|
public Style ParameterName { get; set; } = Color.Silver;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the parenthesis color.
|
/// Gets or sets the parenthesis color.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style Parenthesis { get; set; } = new Style(Color.Silver);
|
public Style Parenthesis { get; set; } = Color.Silver;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the path color.
|
/// Gets or sets the path color.
|
||||||
@ -43,15 +43,15 @@ public sealed class ExceptionStyle
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the line number color.
|
/// Gets or sets the line number color.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style LineNumber { get; set; } = new Style(Color.Blue);
|
public Style LineNumber { get; set; } = Color.Blue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the color for dimmed text such as "at" or "in".
|
/// Gets or sets the color for dimmed text such as "at" or "in".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style Dimmed { get; set; } = new Style(Color.Grey);
|
public Style Dimmed { get; set; } = Color.Grey;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the color for non emphasized items.
|
/// Gets or sets the color for non emphasized items.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Style NonEmphasized { get; set; } = new Style(Color.Silver);
|
public Style NonEmphasized { get; set; } = Color.Silver;
|
||||||
}
|
}
|
@ -16,9 +16,9 @@ internal sealed class ProgressBar : Renderable, IHasCulture
|
|||||||
public bool IsIndeterminate { get; set; }
|
public bool IsIndeterminate { get; set; }
|
||||||
public CultureInfo? Culture { get; set; }
|
public CultureInfo? Culture { get; set; }
|
||||||
|
|
||||||
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
|
public Style CompletedStyle { get; set; } = Color.Yellow;
|
||||||
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
|
public Style FinishedStyle { get; set; } = Color.Green;
|
||||||
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
|
public Style RemainingStyle { get; set; } = Color.Grey;
|
||||||
public Style IndeterminateStyle { get; set; } = DefaultPulseStyle;
|
public Style IndeterminateStyle { get; set; } = DefaultPulseStyle;
|
||||||
|
|
||||||
internal static Style DefaultPulseStyle { get; } = new Style(foreground: Color.DodgerBlue1, background: Color.Grey23);
|
internal static Style DefaultPulseStyle { get; } = new Style(foreground: Color.DodgerBlue1, background: Color.Grey23);
|
||||||
|
@ -2,8 +2,8 @@ namespace Spectre.Console;
|
|||||||
|
|
||||||
internal static class TableRenderer
|
internal static class TableRenderer
|
||||||
{
|
{
|
||||||
private static readonly Style _defaultHeadingStyle = new Style(Color.Silver);
|
private static readonly Style _defaultHeadingStyle = Color.Silver;
|
||||||
private static readonly Style _defaultCaptionStyle = new Style(Color.Grey);
|
private static readonly Style _defaultCaptionStyle = Color.Grey;
|
||||||
|
|
||||||
public static List<Segment> Render(TableRendererContext context, List<int> columnWidths)
|
public static List<Segment> Render(TableRendererContext context, List<int> columnWidths)
|
||||||
{
|
{
|
||||||
|
@ -337,7 +337,7 @@ public sealed class TextPromptTests
|
|||||||
var prompt = new TextPrompt<string>("Enter Value:")
|
var prompt = new TextPrompt<string>("Enter Value:")
|
||||||
.ShowDefaultValue()
|
.ShowDefaultValue()
|
||||||
.DefaultValue("default")
|
.DefaultValue("default")
|
||||||
.DefaultValueStyle(new Style(foreground: Color.Red));
|
.DefaultValueStyle(Color.Red);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Prompt(prompt);
|
console.Prompt(prompt);
|
||||||
@ -384,7 +384,7 @@ public sealed class TextPromptTests
|
|||||||
.ShowChoices()
|
.ShowChoices()
|
||||||
.AddChoice("Choice 1")
|
.AddChoice("Choice 1")
|
||||||
.AddChoice("Choice 2")
|
.AddChoice("Choice 2")
|
||||||
.ChoicesStyle(new Style(foreground: Color.Red));
|
.ChoicesStyle(Color.Red);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Prompt(prompt);
|
console.Prompt(prompt);
|
||||||
|
@ -2,6 +2,19 @@ namespace Spectre.Console.Tests.Unit;
|
|||||||
|
|
||||||
public sealed class StyleTests
|
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]
|
[Fact]
|
||||||
public void Should_Combine_Two_Styles_As_Expected()
|
public void Should_Combine_Two_Styles_As_Expected()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user