Allow formatting breakdown charts with lambda expr

Relates to #252
This commit is contained in:
Patrik Svensson 2021-02-13 17:02:05 +01:00 committed by Patrik Svensson
parent 28e9c14de4
commit 102e2dc38d
6 changed files with 60 additions and 13 deletions

View File

@ -19,10 +19,10 @@ namespace Charts
// Render a breakdown chart
AnsiConsole.WriteLine();
Render("Languages", new BreakdownChart()
Render("Languages used", new BreakdownChart()
.FullSize()
.Width(60)
.TagValueFormat("{0}%")
.ShowPercentage()
.AddItem("SCSS", 37, Color.Red)
.AddItem("HTML", 28.3, Color.Blue)
.AddItem("C#", 22.6, Color.Green)

View File

@ -42,14 +42,14 @@ namespace Spectre.Console.Tests.Unit
[Fact]
[Expectation("TagFormat")]
public async Task Should_Render_Correctly_With_Specific_Tag_Formatter()
public async Task Should_Render_Correctly_With_Specific_Value_Formatter()
{
// Given
var console = new FakeConsole(width: 80);
var chart = Fixture.GetChart()
.Width(60)
.Culture("sv-SE")
.TagValueFormat("{0}%");
.UseValueFormatter((v, c) => string.Format(c, "{0}%", v));
// When
console.Render(chart);

View File

@ -141,16 +141,53 @@ namespace Spectre.Console
/// Tags will be shown.
/// </summary>
/// <param name="chart">The breakdown chart.</param>
/// <param name="format">The tag value format.</param>
/// <param name="func">The value formatter to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static BreakdownChart TagValueFormat(this BreakdownChart chart, string? format)
public static BreakdownChart UseValueFormatter(this BreakdownChart chart, Func<double, CultureInfo, string>? func)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.TagValueFormat = format;
chart.ValueFormatter = func;
return chart;
}
/// <summary>
/// Tags will be shown.
/// </summary>
/// <param name="chart">The breakdown chart.</param>
/// <param name="func">The value formatter to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static BreakdownChart UseValueFormatter(this BreakdownChart chart, Func<double, string>? func)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ValueFormatter = func != null
? (value, _) => func(value)
: null;
return chart;
}
/// <summary>
/// Tags will be shown.
/// </summary>
/// <param name="chart">The breakdown chart.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static BreakdownChart ShowPercentage(this BreakdownChart chart)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ValueFormatter = (value, culture) => string.Format(culture, "{0}%", value);
return chart;
}

View File

@ -31,9 +31,9 @@ namespace Spectre.Console
public bool ShowTagValues { get; set; } = true;
/// <summary>
/// Gets or sets the tag value format.
/// Gets or sets the tag value formatter.
/// </summary>
public string? TagValueFormat { get; set; }
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not the
@ -91,7 +91,7 @@ namespace Spectre.Console
Width = width,
Culture = Culture,
ShowTagValues = ShowTagValues,
TagValueFormat = TagValueFormat,
ValueFormatter = ValueFormatter,
});
}

View File

@ -12,7 +12,7 @@ namespace Spectre.Console
public int? Width { get; set; }
public CultureInfo? Culture { get; set; }
public bool ShowTagValues { get; set; } = true;
public string? TagValueFormat { get; set; }
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
public BreakdownTags(List<IBreakdownChartItem> data)
{
@ -56,16 +56,21 @@ namespace Spectre.Console
private string FormatValue(IBreakdownChartItem item, CultureInfo culture)
{
var formatter = TagValueFormat ?? "{0}";
var formatter = ValueFormatter ?? DefaultFormatter;
if (ShowTagValues)
{
return string.Format(culture, "{0} [grey]{1}[/]",
item.Label.EscapeMarkup(),
string.Format(culture, formatter, item.Value));
formatter(item.Value, culture));
}
return item.Label.EscapeMarkup();
}
private static string DefaultFormatter(double value, CultureInfo culture)
{
return value.ToString(culture);
}
}
}

View File

@ -44,6 +44,11 @@ namespace Spectre.Console
bars = Math.Max(0, bars);
}
if (bars < 0)
{
yield break;
}
yield return new Segment(new string(token, bars), style);
if (ShowValue)