mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-20 05:48:14 +08:00
Adds ValueFormatter to ProgressBar
This commit is contained in:
@ -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>
|
||||
@ -90,6 +95,7 @@ public sealed class BarChart : Renderable, IHasCulture
|
||||
AsciiBar = '█',
|
||||
ShowValue = ShowValues,
|
||||
Culture = Culture,
|
||||
ValueFormatter = ValueFormatter,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
@ -50,7 +51,7 @@ internal sealed class ProgressBar : Renderable, IHasCulture
|
||||
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;
|
||||
|
Reference in New Issue
Block a user