mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
parent
28e9c14de4
commit
102e2dc38d
@ -19,10 +19,10 @@ namespace Charts
|
|||||||
|
|
||||||
// Render a breakdown chart
|
// Render a breakdown chart
|
||||||
AnsiConsole.WriteLine();
|
AnsiConsole.WriteLine();
|
||||||
Render("Languages", new BreakdownChart()
|
Render("Languages used", new BreakdownChart()
|
||||||
.FullSize()
|
.FullSize()
|
||||||
.Width(60)
|
.Width(60)
|
||||||
.TagValueFormat("{0}%")
|
.ShowPercentage()
|
||||||
.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)
|
||||||
|
@ -42,14 +42,14 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
[Expectation("TagFormat")]
|
[Expectation("TagFormat")]
|
||||||
public async Task Should_Render_Correctly_With_Specific_Tag_Formatter()
|
public async Task Should_Render_Correctly_With_Specific_Value_Formatter()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
var chart = Fixture.GetChart()
|
var chart = Fixture.GetChart()
|
||||||
.Width(60)
|
.Width(60)
|
||||||
.Culture("sv-SE")
|
.Culture("sv-SE")
|
||||||
.TagValueFormat("{0}%");
|
.UseValueFormatter((v, c) => string.Format(c, "{0}%", v));
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(chart);
|
console.Render(chart);
|
||||||
|
@ -141,16 +141,53 @@ namespace Spectre.Console
|
|||||||
/// Tags will be shown.
|
/// Tags will be shown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="chart">The breakdown chart.</param>
|
/// <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>
|
/// <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)
|
if (chart is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(chart));
|
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;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,9 +31,9 @@ namespace Spectre.Console
|
|||||||
public bool ShowTagValues { get; set; } = true;
|
public bool ShowTagValues { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the tag value format.
|
/// Gets or sets the tag value formatter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? TagValueFormat { get; set; }
|
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not the
|
/// Gets or sets a value indicating whether or not the
|
||||||
@ -91,7 +91,7 @@ namespace Spectre.Console
|
|||||||
Width = width,
|
Width = width,
|
||||||
Culture = Culture,
|
Culture = Culture,
|
||||||
ShowTagValues = ShowTagValues,
|
ShowTagValues = ShowTagValues,
|
||||||
TagValueFormat = TagValueFormat,
|
ValueFormatter = ValueFormatter,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ namespace Spectre.Console
|
|||||||
public int? Width { get; set; }
|
public int? Width { get; set; }
|
||||||
public CultureInfo? Culture { get; set; }
|
public CultureInfo? Culture { get; set; }
|
||||||
public bool ShowTagValues { get; set; } = true;
|
public bool ShowTagValues { get; set; } = true;
|
||||||
public string? TagValueFormat { get; set; }
|
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
|
||||||
|
|
||||||
public BreakdownTags(List<IBreakdownChartItem> data)
|
public BreakdownTags(List<IBreakdownChartItem> data)
|
||||||
{
|
{
|
||||||
@ -56,16 +56,21 @@ namespace Spectre.Console
|
|||||||
|
|
||||||
private string FormatValue(IBreakdownChartItem item, CultureInfo culture)
|
private string FormatValue(IBreakdownChartItem item, CultureInfo culture)
|
||||||
{
|
{
|
||||||
var formatter = TagValueFormat ?? "{0}";
|
var formatter = ValueFormatter ?? DefaultFormatter;
|
||||||
|
|
||||||
if (ShowTagValues)
|
if (ShowTagValues)
|
||||||
{
|
{
|
||||||
return string.Format(culture, "{0} [grey]{1}[/]",
|
return string.Format(culture, "{0} [grey]{1}[/]",
|
||||||
item.Label.EscapeMarkup(),
|
item.Label.EscapeMarkup(),
|
||||||
string.Format(culture, formatter, item.Value));
|
formatter(item.Value, culture));
|
||||||
}
|
}
|
||||||
|
|
||||||
return item.Label.EscapeMarkup();
|
return item.Label.EscapeMarkup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string DefaultFormatter(double value, CultureInfo culture)
|
||||||
|
{
|
||||||
|
return value.ToString(culture);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,11 @@ namespace Spectre.Console
|
|||||||
bars = Math.Max(0, bars);
|
bars = Math.Max(0, bars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bars < 0)
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
yield return new Segment(new string(token, bars), style);
|
yield return new Segment(new string(token, bars), style);
|
||||||
|
|
||||||
if (ShowValue)
|
if (ShowValue)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user