mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 02:25:28 +08:00 
			
		
		
		
	(#684) Enable setting the color of the values in a BreakdownChart
This commit is contained in:
		@@ -23,6 +23,7 @@ public static class Program
 | 
				
			|||||||
            .FullSize()
 | 
					            .FullSize()
 | 
				
			||||||
            .Width(60)
 | 
					            .Width(60)
 | 
				
			||||||
            .ShowPercentage()
 | 
					            .ShowPercentage()
 | 
				
			||||||
 | 
					            .WithValueColor(Color.Orange1)
 | 
				
			||||||
            .AddItem("SCSS", 37, Color.Red)
 | 
					            .AddItem("SCSS", 37, Color.Red)
 | 
				
			||||||
            .AddItem("HTML", 28.3, Color.Blue)
 | 
					            .AddItem("HTML", 28.3, Color.Blue)
 | 
				
			||||||
            .AddItem("C#", 22.6, Color.Green)
 | 
					            .AddItem("C#", 22.6, Color.Green)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -297,4 +297,21 @@ public static class BreakdownChartExtensions
 | 
				
			|||||||
        chart.Compact = compact;
 | 
					        chart.Compact = compact;
 | 
				
			||||||
        return chart;
 | 
					        return chart;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// Sets the <see cref="BreakdownChart.ValueColor"/>.
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    /// <param name="chart">The breakdown chart.</param>
 | 
				
			||||||
 | 
					    /// <param name="color">The <see cref="Color"/> to set.</param>
 | 
				
			||||||
 | 
					    /// <returns>The same instance so that multiple calls can be chained.</returns>
 | 
				
			||||||
 | 
					    public static BreakdownChart WithValueColor(this BreakdownChart chart, Color color)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (chart is null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            throw new ArgumentNullException(nameof(chart));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        chart.ValueColor = color;
 | 
				
			||||||
 | 
					        return chart;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -30,6 +30,11 @@ public sealed class BreakdownChart : Renderable, IHasCulture, IExpandable
 | 
				
			|||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
 | 
					    public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// Gets or sets the Color in which the values will be shown.
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    public Color ValueColor { get; set; } = Color.Grey;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Gets or sets a value indicating whether or not the
 | 
					    /// Gets or sets a value indicating whether or not the
 | 
				
			||||||
    /// chart and tags should be rendered in compact mode.
 | 
					    /// chart and tags should be rendered in compact mode.
 | 
				
			||||||
@@ -94,6 +99,7 @@ public sealed class BreakdownChart : Renderable, IHasCulture, IExpandable
 | 
				
			|||||||
                Culture = Culture,
 | 
					                Culture = Culture,
 | 
				
			||||||
                ShowTagValues = ShowTagValues,
 | 
					                ShowTagValues = ShowTagValues,
 | 
				
			||||||
                ValueFormatter = ValueFormatter,
 | 
					                ValueFormatter = ValueFormatter,
 | 
				
			||||||
 | 
					                ValueColor = ValueColor,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,7 @@ internal sealed class BreakdownTags : Renderable
 | 
				
			|||||||
    public CultureInfo? Culture { get; set; }
 | 
					    public CultureInfo? Culture { get; set; }
 | 
				
			||||||
    public bool ShowTagValues { get; set; } = true;
 | 
					    public bool ShowTagValues { get; set; } = true;
 | 
				
			||||||
    public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
 | 
					    public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
 | 
				
			||||||
 | 
					    public Color ValueColor { get; set; } = Color.Grey;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public BreakdownTags(List<IBreakdownChartItem> data)
 | 
					    public BreakdownTags(List<IBreakdownChartItem> data)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -55,8 +56,9 @@ internal sealed class BreakdownTags : Renderable
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if (ShowTagValues)
 | 
					        if (ShowTagValues)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return string.Format(culture, "{0} [grey]{1}[/]",
 | 
					            return string.Format(culture, "{0} [{1}]{2}[/]",
 | 
				
			||||||
                item.Label.EscapeMarkup(),
 | 
					                item.Label.EscapeMarkup(),
 | 
				
			||||||
 | 
					                ValueColor.ToMarkup(),
 | 
				
			||||||
                formatter(item.Value, culture));
 | 
					                formatter(item.Value, culture));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					[38;5;9m████████████████████████[0m[38;5;12m█████████████████[0m[38;5;2m█████████████[0m[38;5;11m███[0m[38;5;119m███[0m
 | 
				
			||||||
 | 
					[38;5;9m■[0m SCSS [38;5;9m37[0m    [38;5;12m■[0m HTML [38;5;9m28.3[0m     [38;5;2m■[0m C# [38;5;9m22.6[0m    [38;5;11m■[0m JavaScript [38;5;9m6[0m    
 | 
				
			||||||
 | 
					[38;5;119m■[0m Ruby [38;5;9m6[0m     [38;5;14m■[0m Shell [38;5;9m0.1[0m                                    
 | 
				
			||||||
@@ -127,6 +127,21 @@ public sealed class BreakdownChartTests
 | 
				
			|||||||
        await Verifier.Verify(console.Output);
 | 
					        await Verifier.Verify(console.Output);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    [Fact]
 | 
				
			||||||
 | 
					    [Expectation("ValueColor")]
 | 
				
			||||||
 | 
					    public async Task Should_Render_Correct_ValueColor()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // Given
 | 
				
			||||||
 | 
					        var console = new TestConsole().EmitAnsiSequences();
 | 
				
			||||||
 | 
					        var chart = Fixture.GetChart().Width(60).WithValueColor(Color.Red);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // When
 | 
				
			||||||
 | 
					        console.Write(chart);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Then
 | 
				
			||||||
 | 
					        await Verifier.Verify(console.Output);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static class Fixture
 | 
					    public static class Fixture
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public static BreakdownChart GetChart()
 | 
					        public static BreakdownChart GetChart()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user