Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/progressbar valueformatter #1414

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Spectre.Console/Extensions/BarChartExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@
return chart;
}

public static BarChart UseValueFormatter(this BarChart chart, Func<double, CultureInfo, string>? func)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

Missing XML comment for publicly visible type or member 'BarChartExtensions.UseValueFormatter(BarChart, Func<double, CultureInfo, string>?)'

Check failure on line 119 in src/Spectre.Console/Extensions/BarChartExtensions.cs

View workflow job for this annotation

GitHub Actions / Build (macOS)

jsheely marked this conversation as resolved.
Show resolved Hide resolved
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}

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>
jsheely marked this conversation as resolved.
Show resolved Hide resolved
public static BarChart UseValueFormatter(this BarChart 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>
/// Sets the width of the bar chart.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Spectre.Console/Widgets/Charts/BarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public sealed class BarChart : Renderable, IHasCulture
/// <remarks>Defaults to null, which corresponds to largest value in chart.</remarks>
public double? MaxValue { get; set; }

/// <summary>
/// Gets or sets the function used to format the values of the bar chart.
/// </summary>
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="BarChart"/> class.
/// </summary>
Expand Down Expand Up @@ -90,6 +95,7 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
AsciiBar = '█',
ShowValue = ShowValues,
Culture = Culture,
ValueFormatter = ValueFormatter,
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/Spectre.Console/Widgets/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal sealed class ProgressBar : Renderable, IHasCulture
public bool ShowValue { get; set; }
public bool IsIndeterminate { get; set; }
public CultureInfo? Culture { get; set; }
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }

public Style CompletedStyle { get; set; } = Color.Yellow;
public Style FinishedStyle { get; set; } = Color.Green;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
var barCount = Math.Max(0, (int)(width * (completedBarCount / MaxValue)));

// Show value?
var value = completedBarCount.ToString(Culture ?? CultureInfo.InvariantCulture);
var value = ValueFormatter != null ? ValueFormatter(completedBarCount, Culture ?? CultureInfo.InvariantCulture) : completedBarCount.ToString(Culture ?? CultureInfo.InvariantCulture);
if (ShowValue)
{
barCount = barCount - value.Length - 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9,000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9000
51 changes: 51 additions & 0 deletions test/Spectre.Console.Tests/Unit/Widgets/ProgressBarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Spectre.Console.Tests.Unit;

[UsesVerify]
[ExpectationPath("Widgets/ProgressBar")]
public class ProgressBarTests
{
[Fact]
[Expectation("Render")]
public async Task Should_Render_Correctly()
{
// Given
var console = new TestConsole();

var bar = new ProgressBar()
{
Width = 60,
Value = 9000,
MaxValue = 9000,
ShowValue = true
};

// When
console.Write(bar);

// Then
jsheely marked this conversation as resolved.
Show resolved Hide resolved
await Verifier.Verify(console.Output);
}

[Fact]
[Expectation("Formatted")]
public async Task Should_Render_ValueFormatted()
{
// Given
var console = new TestConsole();

var bar = new ProgressBar()
{
Width = 60,
Value = 9000,
MaxValue = 9000,
ShowValue = true,
ValueFormatter = (value, _) => value.ToString("N0"),
};

// When
console.Write(bar);

// Then
jsheely marked this conversation as resolved.
Show resolved Hide resolved
await Verifier.Verify(console.Output);
}
}
Loading