Skip to content

Commit

Permalink
Add precision argument and default to 0 for integer values
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Nov 2, 2023
1 parent bfed993 commit 691d576
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/Hangfire.Console/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public static void ResetTextColor(this PerformContext context)
/// <param name="context">Context</param>
/// <param name="value">Initial value</param>
/// <param name="color">Progress bar color</param>
public static IProgressBar WriteProgressBar(this PerformContext context, int value = 0, ConsoleTextColor? color = null)
=> ConsoleContext.FromPerformContext(context)?.WriteProgressBar(null, value, color) ?? new NoOpProgressBar();
/// <param name="digits">The number of fractional digits or decimal places to use for the progress bar</param>
public static IProgressBar WriteProgressBar(this PerformContext context, int value = 0, ConsoleTextColor? color = null, int digits = 0)
=> ConsoleContext.FromPerformContext(context)?.WriteProgressBar(null, value, color, digits) ?? new NoOpProgressBar();

/// <summary>
/// Adds an updateable named progress bar to console.
Expand All @@ -64,8 +65,9 @@ public static IProgressBar WriteProgressBar(this PerformContext context, int val
/// <param name="name">Name</param>
/// <param name="value">Initial value</param>
/// <param name="color">Progress bar color</param>
public static IProgressBar WriteProgressBar(this PerformContext context, string name, double value = 0, ConsoleTextColor? color = null)
=> ConsoleContext.FromPerformContext(context)?.WriteProgressBar(name, value, color) ?? new NoOpProgressBar();
/// <param name="digits">The number of fractional digits or decimal places to use for the progress bar</param>
public static IProgressBar WriteProgressBar(this PerformContext context, string name, double value = 0, ConsoleTextColor? color = null, int digits = 0)
=> ConsoleContext.FromPerformContext(context)?.WriteProgressBar(name, value, color, digits) ?? new NoOpProgressBar();

/// <summary>
/// Adds a string to console.
Expand Down
12 changes: 8 additions & 4 deletions src/Hangfire.Console/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="context">Perform context</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(0, color), count);
/// <param name="digits">The number of fractional digits or decimal places to use for the progress bar</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, ConsoleTextColor? color = null, int count = -1, int digits = 0) => WithProgress(enumerable, context.WriteProgressBar(0, color, digits), count);

/// <summary>
/// Returns ab <see cref="IEnumerable" /> reporting enumeration progress.
Expand All @@ -75,7 +76,8 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="context">Perform context</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(0, color), count);
/// <param name="digits">The number of fractional digits or decimal places to use for the progress bar</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, ConsoleTextColor? color = null, int count = -1, int digits = 0) => WithProgress(enumerable, context.WriteProgressBar(0, color, digits), count);

/// <summary>
/// Returns an <see cref="IEnumerable{T}" /> reporting enumeration progress.
Expand All @@ -86,7 +88,8 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="name">Progress bar name</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, string name, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color), count);
/// <param name="digits">The number of fractional digits or decimal places to use for the progress bar</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, string name, ConsoleTextColor? color = null, int count = -1, int digits = 0) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color, digits), count);

/// <summary>
/// Returns ab <see cref="IEnumerable" /> reporting enumeration progress.
Expand All @@ -96,5 +99,6 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="name">Progress bar name</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, string name, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color), count);
/// <param name="digits">The number of fractional digits or decimal places to use for the progress bar</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, string name, ConsoleTextColor? color = null, int count = -1, int digits = 0) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color, digits), count);
}
7 changes: 5 additions & 2 deletions src/Hangfire.Console/Progress/DefaultProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ internal class DefaultProgressBar : IProgressBar

private readonly string _progressBarId;

private readonly int _digits;

private string? _color;

private string? _name;

private double _value;

internal DefaultProgressBar(ConsoleContext context, string progressBarId, string? name, string? color)
internal DefaultProgressBar(ConsoleContext context, string progressBarId, string? name, string? color, int digits)
{
if (string.IsNullOrEmpty(progressBarId))
{
Expand All @@ -29,6 +31,7 @@ internal DefaultProgressBar(ConsoleContext context, string progressBarId, string

_context = context ?? throw new ArgumentNullException(nameof(context));
_progressBarId = progressBarId;
_digits = digits;
_name = string.IsNullOrEmpty(name) ? null : name;
_color = string.IsNullOrEmpty(color) ? null : color;
_value = -1;
Expand All @@ -41,7 +44,7 @@ public void SetValue(int value)

public void SetValue(double value)
{
value = Math.Round(value, 1);
value = Math.Round(value, _digits);

if (value < 0 || value > 100)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Hangfire.Console/Server/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ public void WriteLine(string? value, ConsoleTextColor? color)
AddLine(new ConsoleLine { Message = value ?? "", TextColor = color ?? TextColor });
}

public IProgressBar WriteProgressBar(string? name, double value, ConsoleTextColor? color)
public IProgressBar WriteProgressBar(string? name, double value, ConsoleTextColor? color) => WriteProgressBar(name, value, color, precision: 0);

public IProgressBar WriteProgressBar(string? name, double value, ConsoleTextColor? color, int precision)
{
var progressBarId = Interlocked.Increment(ref _nextProgressBarId);

var progressBar = new DefaultProgressBar(this, progressBarId.ToString(CultureInfo.InvariantCulture), name, color);
var progressBar = new DefaultProgressBar(this, progressBarId.ToString(CultureInfo.InvariantCulture), name, color, precision);

// set initial value
progressBar.SetValue(value);
Expand Down

0 comments on commit 691d576

Please sign in to comment.