mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
Adds ValueFormatter to ProgressBar
This commit is contained in:
parent
71631b248a
commit
703d653ec5
@ -132,3 +132,13 @@ AnsiConsole.Write(new BreakdownChart()
|
|||||||
.AddItem(new Fruit("Mango", 3, Color.Orange4))
|
.AddItem(new Fruit("Mango", 3, Color.Orange4))
|
||||||
.AddItems(items));
|
.AddItems(items));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Add value formatter to chart numbers
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
var chart = new BreakdownChart();
|
||||||
|
chart.UseValueFormater(value => value.ToString("N0"));
|
||||||
|
|
||||||
|
// This can be simplified as extension methods are chainable.
|
||||||
|
var chart = new BreakdownChart().UseValueFormatter(v => v.ToString("N0"));
|
||||||
|
```
|
||||||
|
@ -116,6 +116,43 @@ public static class BarChartExtensions
|
|||||||
return chart;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the value formatter for the bar chart using culture info.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="chart">The bar chart.</param>
|
||||||
|
/// <param name="func">The value formatter function with culture info.</param>
|
||||||
|
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||||
|
public static BarChart UseValueFormatter(this BarChart chart, Func<double, CultureInfo, string>? func)
|
||||||
|
{
|
||||||
|
if (chart is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(chart));
|
||||||
|
}
|
||||||
|
|
||||||
|
chart.ValueFormatter = func;
|
||||||
|
return chart;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the value formatter for the bar chart.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="chart">The bar 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 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>
|
/// <summary>
|
||||||
/// Sets the width of the bar chart.
|
/// Sets the width of the bar chart.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -43,6 +43,11 @@ public sealed class BarChart : Renderable, IHasCulture
|
|||||||
/// <remarks>Defaults to null, which corresponds to largest value in chart.</remarks>
|
/// <remarks>Defaults to null, which corresponds to largest value in chart.</remarks>
|
||||||
public double? MaxValue { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="BarChart"/> class.
|
/// Initializes a new instance of the <see cref="BarChart"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -90,6 +95,7 @@ public sealed class BarChart : Renderable, IHasCulture
|
|||||||
AsciiBar = '█',
|
AsciiBar = '█',
|
||||||
ShowValue = ShowValues,
|
ShowValue = ShowValues,
|
||||||
Culture = Culture,
|
Culture = Culture,
|
||||||
|
ValueFormatter = ValueFormatter,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ internal sealed class ProgressBar : Renderable, IHasCulture
|
|||||||
public bool ShowValue { get; set; }
|
public bool ShowValue { get; set; }
|
||||||
public bool IsIndeterminate { get; set; }
|
public bool IsIndeterminate { get; set; }
|
||||||
public CultureInfo? Culture { get; set; }
|
public CultureInfo? Culture { get; set; }
|
||||||
|
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }
|
||||||
|
|
||||||
public Style CompletedStyle { get; set; } = Color.Yellow;
|
public Style CompletedStyle { get; set; } = Color.Yellow;
|
||||||
public Style FinishedStyle { get; set; } = Color.Green;
|
public Style FinishedStyle { get; set; } = Color.Green;
|
||||||
@ -50,7 +51,7 @@ internal sealed class ProgressBar : Renderable, IHasCulture
|
|||||||
var barCount = Math.Max(0, (int)(width * (completedBarCount / MaxValue)));
|
var barCount = Math.Max(0, (int)(width * (completedBarCount / MaxValue)));
|
||||||
|
|
||||||
// Show value?
|
// 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)
|
if (ShowValue)
|
||||||
{
|
{
|
||||||
barCount = barCount - value.Length - 1;
|
barCount = barCount - value.Length - 1;
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9,000
|
@ -0,0 +1 @@
|
|||||||
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9000
|
51
test/Spectre.Console.Tests/Unit/Widgets/ProgressBarTests.cs
Normal file
51
test/Spectre.Console.Tests/Unit/Widgets/ProgressBarTests.cs
Normal file
@ -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
|
||||||
|
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
|
||||||
|
await Verifier.Verify(console.Output);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user