Skip to content

Commit

Permalink
Add formatting support for breakdown chart values
Browse files Browse the repository at this point in the history
Closes #252
  • Loading branch information
patriksvensson committed Feb 5, 2021
1 parent b64e016 commit 705cf74
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 91 deletions.
2 changes: 1 addition & 1 deletion examples/Console/Canvas/Canvas.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<ExampleTitle>Canvas</ExampleTitle>
<ExampleDescription>Demonstrates how to render pixels and images.</ExampleDescription>
<ExampleGroup>Widgets</ExampleGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/Console/Charts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void Main()
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)
Expand Down
2 changes: 1 addition & 1 deletion examples/Console/Prompt/Prompt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9</LangVersion>
<ExampleTitle>Prompt</ExampleTitle>
<ExampleDescription>Demonstrates how to get input from a user.</ExampleDescription>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<Description>A library that extends Spectre.Console with ImageSharp superpowers.</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -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%
106 changes: 35 additions & 71 deletions src/Spectre.Console.Tests/Unit/BreakdownChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ public async Task Should_Render_Correctly()
{
// 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);
Expand All @@ -36,38 +31,28 @@ public async Task Should_Render_With_Specific_Width()
{
// 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);
Expand All @@ -79,17 +64,10 @@ public async Task Should_Render_Correctly_Without_Tags()
{
// 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);
Expand All @@ -101,17 +79,10 @@ public async Task Should_Render_Correctly_Without_Tag_Values()
{
// 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);
Expand All @@ -123,17 +94,10 @@ public async Task Should_Render_Correctly_With_Specific_Culture()
{
// 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);
Expand All @@ -145,17 +109,10 @@ public async Task Should_Render_FullSize_Mode_Correctly()
{
// 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);
Expand All @@ -167,20 +124,27 @@ public async Task Should_Render_Correct_Ansi()
{
// 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);
}
}
}
}
8 changes: 5 additions & 3 deletions src/Spectre.Console/Extensions/BreakdownChartExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Spectre.Console
{
Expand Down Expand Up @@ -137,18 +138,19 @@ public static BreakdownChart Width(this BreakdownChart chart, int? width)
}

/// <summary>
/// All values will be shown as percentages.
/// Tags will be shown.
/// </summary>
/// <param name="chart">The breakdown chart.</param>
/// <param name="format">The tag value format.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
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;
}

Expand Down
13 changes: 6 additions & 7 deletions src/Spectre.Console/Widgets/Charts/BreakdownChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ public sealed class BreakdownChart : Renderable, IHasCulture
/// </summary>
public int? Width { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not
/// to show values as percentages or not.
/// </summary>
public bool ShowAsPercentages { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not to show tags.
/// </summary>
Expand All @@ -36,6 +30,11 @@ public sealed class BreakdownChart : Renderable, IHasCulture
/// </summary>
public bool ShowTagValues { get; set; } = true;

/// <summary>
/// Gets or sets the tag value format.
/// </summary>
public string? TagValueFormat { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not the
/// chart and tags should be rendered in compact mode.
Expand Down Expand Up @@ -91,8 +90,8 @@ protected override IEnumerable<Segment> Render(RenderContext context, int maxWid
{
Width = width,
Culture = Culture,
ShowPercentages = ShowAsPercentages,
ShowTagValues = ShowTagValues,
TagValueFormat = TagValueFormat,
});
}

Expand Down
10 changes: 6 additions & 4 deletions src/Spectre.Console/Widgets/Charts/BreakdownTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal sealed class BreakdownTags : Renderable

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<IBreakdownChartItem> data)
{
Expand Down Expand Up @@ -56,11 +56,13 @@ private string GetTag(IBreakdownChartItem item, CultureInfo culture)

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();
Expand Down

0 comments on commit 705cf74

Please sign in to comment.