diff --git a/examples/Console/Canvas/Canvas.csproj b/examples/Console/Canvas/Canvas.csproj index 39df2fb..182d9e5 100644 --- a/examples/Console/Canvas/Canvas.csproj +++ b/examples/Console/Canvas/Canvas.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net5.0 Canvas Demonstrates how to render pixels and images. Widgets diff --git a/examples/Console/Charts/Program.cs b/examples/Console/Charts/Program.cs index b4fdb4b..c44554b 100644 --- a/examples/Console/Charts/Program.cs +++ b/examples/Console/Charts/Program.cs @@ -22,7 +22,7 @@ namespace Charts Render("Languages", new BreakdownChart() .FullSize() .Width(60) - .ShowAsPercentages() + .TagValueFormat("{0}%") .AddItem("SCSS", 37, Color.Red) .AddItem("HTML", 28.3, Color.Blue) .AddItem("C#", 22.6, Color.Green) diff --git a/examples/Console/Prompt/Prompt.csproj b/examples/Console/Prompt/Prompt.csproj index 9c861c6..c0d2d42 100644 --- a/examples/Console/Prompt/Prompt.csproj +++ b/examples/Console/Prompt/Prompt.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net5.0 9 Prompt Demonstrates how to get input from a user. diff --git a/src/Spectre.Console.ImageSharp/Spectre.Console.ImageSharp.csproj b/src/Spectre.Console.ImageSharp/Spectre.Console.ImageSharp.csproj index 5e2e05f..5590b44 100644 --- a/src/Spectre.Console.ImageSharp/Spectre.Console.ImageSharp.csproj +++ b/src/Spectre.Console.ImageSharp/Spectre.Console.ImageSharp.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + net5.0;netstandard2.0 enable true A library that extends Spectre.Console with ImageSharp superpowers. diff --git a/src/Spectre.Console.Tests/Expectations/Widgets/BreakdownChart/ShowAsPercentages.Output.verified.txt b/src/Spectre.Console.Tests/Expectations/Widgets/BreakdownChart/TagFormat.Output.verified.txt similarity index 59% rename from src/Spectre.Console.Tests/Expectations/Widgets/BreakdownChart/ShowAsPercentages.Output.verified.txt rename to src/Spectre.Console.Tests/Expectations/Widgets/BreakdownChart/TagFormat.Output.verified.txt index 4e59f8a..30cc1d9 100644 --- a/src/Spectre.Console.Tests/Expectations/Widgets/BreakdownChart/ShowAsPercentages.Output.verified.txt +++ b/src/Spectre.Console.Tests/Expectations/Widgets/BreakdownChart/TagFormat.Output.verified.txt @@ -1,3 +1,3 @@ ████████████████████████████████████████████████████████████ -■ SCSS 37% ■ HTML 28.3% ■ C# 22.6% ■ JavaScript 6% -■ Ruby 6% ■ Shell 0.1% +■ SCSS 37% ■ HTML 28,3% ■ C# 22,6% ■ JavaScript 6% +■ Ruby 6% ■ Shell 0,1% diff --git a/src/Spectre.Console.Tests/Unit/BreakdownChartTests.cs b/src/Spectre.Console.Tests/Unit/BreakdownChartTests.cs index 491e304..160b1af 100644 --- a/src/Spectre.Console.Tests/Unit/BreakdownChartTests.cs +++ b/src/Spectre.Console.Tests/Unit/BreakdownChartTests.cs @@ -16,15 +16,10 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart(); // When - console.Render(new BreakdownChart() - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); @@ -36,38 +31,28 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart().Width(60); // When - console.Render(new BreakdownChart() - .Width(60) - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); } [Fact] - [Expectation("ShowAsPercentages")] - public async Task Should_Render_Correctly_With_Specific_Width() + [Expectation("TagFormat")] + public async Task Should_Render_Correctly_With_Specific_Tag_Formatter() { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart() + .Width(60) + .Culture("sv-SE") + .TagValueFormat("{0}%"); // When - console.Render(new BreakdownChart() - .Width(60) - .ShowAsPercentages() - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); @@ -79,17 +64,10 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart().Width(60).HideTags(); // When - console.Render(new BreakdownChart() - .Width(60) - .HideTags() - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); @@ -101,17 +79,10 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart().Width(60).HideTagValues(); // When - console.Render(new BreakdownChart() - .Width(60) - .HideTagValues() - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); @@ -123,17 +94,10 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart().Width(60).Culture("sv-SE"); // When - console.Render(new BreakdownChart() - .Width(60) - .Culture("sv-SE") - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); @@ -145,17 +109,10 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeConsole(width: 80); + var chart = Fixture.GetChart().Width(60).FullSize(); // When - console.Render(new BreakdownChart() - .Width(60) - .FullSize() - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); @@ -167,20 +124,27 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new FakeAnsiConsole(ColorSystem.EightBit, width: 80); + var chart = Fixture.GetChart().Width(60).FullSize(); // When - console.Render(new BreakdownChart() - .Width(60) - .FullSize() - .AddItem("SCSS", 37, Color.Red) - .AddItem("HTML", 28.3, Color.Blue) - .AddItem("C#", 22.6, Color.Green) - .AddItem("JavaScript", 6, Color.Yellow) - .AddItem("Ruby", 6, Color.LightGreen) - .AddItem("Shell", 0.1, Color.Aqua)); + console.Render(chart); // Then await Verifier.Verify(console.Output); } + + public static class Fixture + { + public static BreakdownChart GetChart() + { + return new BreakdownChart() + .AddItem("SCSS", 37, Color.Red) + .AddItem("HTML", 28.3, Color.Blue) + .AddItem("C#", 22.6, Color.Green) + .AddItem("JavaScript", 6, Color.Yellow) + .AddItem("Ruby", 6, Color.LightGreen) + .AddItem("Shell", 0.1, Color.Aqua); + } + } } } diff --git a/src/Spectre.Console/Extensions/BreakdownChartExtensions.cs b/src/Spectre.Console/Extensions/BreakdownChartExtensions.cs index 8d4cceb..5c7a2e1 100644 --- a/src/Spectre.Console/Extensions/BreakdownChartExtensions.cs +++ b/src/Spectre.Console/Extensions/BreakdownChartExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; namespace Spectre.Console { @@ -137,18 +138,19 @@ namespace Spectre.Console } /// - /// All values will be shown as percentages. + /// Tags will be shown. /// /// The breakdown chart. + /// The tag value format. /// The same instance so that multiple calls can be chained. - public static BreakdownChart ShowAsPercentages(this BreakdownChart chart) + public static BreakdownChart TagValueFormat(this BreakdownChart chart, string? format) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } - chart.ShowAsPercentages = true; + chart.TagValueFormat = format; return chart; } diff --git a/src/Spectre.Console/Widgets/Charts/BreakdownChart.cs b/src/Spectre.Console/Widgets/Charts/BreakdownChart.cs index 249ccae..74f972f 100644 --- a/src/Spectre.Console/Widgets/Charts/BreakdownChart.cs +++ b/src/Spectre.Console/Widgets/Charts/BreakdownChart.cs @@ -20,12 +20,6 @@ namespace Spectre.Console /// public int? Width { get; set; } - /// - /// Gets or sets a value indicating whether or not - /// to show values as percentages or not. - /// - public bool ShowAsPercentages { get; set; } - /// /// Gets or sets a value indicating whether or not to show tags. /// @@ -36,6 +30,11 @@ namespace Spectre.Console /// public bool ShowTagValues { get; set; } = true; + /// + /// Gets or sets the tag value format. + /// + public string? TagValueFormat { get; set; } + /// /// Gets or sets a value indicating whether or not the /// chart and tags should be rendered in compact mode. @@ -91,8 +90,8 @@ namespace Spectre.Console { Width = width, Culture = Culture, - ShowPercentages = ShowAsPercentages, ShowTagValues = ShowTagValues, + TagValueFormat = TagValueFormat, }); } diff --git a/src/Spectre.Console/Widgets/Charts/BreakdownTags.cs b/src/Spectre.Console/Widgets/Charts/BreakdownTags.cs index 71e8d92..cec47f9 100644 --- a/src/Spectre.Console/Widgets/Charts/BreakdownTags.cs +++ b/src/Spectre.Console/Widgets/Charts/BreakdownTags.cs @@ -11,8 +11,8 @@ namespace Spectre.Console public int? Width { get; set; } public CultureInfo? Culture { get; set; } - public bool ShowPercentages { get; set; } public bool ShowTagValues { get; set; } = true; + public string? TagValueFormat { get; set; } public BreakdownTags(List data) { @@ -56,11 +56,13 @@ namespace Spectre.Console private string FormatValue(IBreakdownChartItem item, CultureInfo culture) { + var formatter = TagValueFormat ?? "{0}"; + if (ShowTagValues) { - return string.Format(culture, "{0} [grey]{1}{2}[/]", - item.Label.EscapeMarkup(), item.Value, - ShowPercentages ? "%" : string.Empty); + return string.Format(culture, "{0} [grey]{1}[/]", + item.Label.EscapeMarkup(), + string.Format(culture, formatter, item.Value)); } return item.Label.EscapeMarkup();