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

@ -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)