Rename ProgressReporter to ProgressTicker

This commit is contained in:
Alexey Golub
2019-08-24 13:00:13 +03:00
parent 13b15b98ed
commit e4a502d9d6
4 changed files with 18 additions and 16 deletions

View File

@@ -9,7 +9,7 @@ using NUnit.Framework;
namespace CliFx.Tests.Utilities
{
[TestFixture]
public class ProgressReporterTests
public class ProgressTickerTests
{
[Test]
public void Report_Test()
@@ -20,14 +20,14 @@ namespace CliFx.Tests.Utilities
using (var stdout = new StringWriter(formatProvider))
{
var console = new VirtualConsole(TextReader.Null, false, stdout, false, TextWriter.Null, false);
var reporter = console.CreateProgressReporter();
var ticker = console.CreateProgressTicker();
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
var progressStringValues = progressValues.Select(p => p.ToString("P2", formatProvider)).ToArray();
// Act
foreach (var progress in progressValues)
reporter.Report(progress);
ticker.Report(progress);
// Assert
stdout.ToString().Should().ContainAll(progressStringValues);
@@ -41,13 +41,13 @@ namespace CliFx.Tests.Utilities
using (var stdout = new StringWriter())
{
var console = new VirtualConsole(stdout);
var reporter = console.CreateProgressReporter();
var ticker = console.CreateProgressTicker();
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
// Act
foreach (var progress in progressValues)
reporter.Report(progress);
ticker.Report(progress);
// Assert
stdout.ToString().Should().BeEmpty();

View File

@@ -8,8 +8,8 @@ namespace CliFx.Utilities
public static class Extensions
{
/// <summary>
/// Creates a progress reporter for this console.
/// Creates a <see cref="ProgressTicker"/> bound to this console.
/// </summary>
public static ProgressReporter CreateProgressReporter(this IConsole console) => new ProgressReporter(console);
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
}
}

View File

@@ -4,18 +4,18 @@ using CliFx.Services;
namespace CliFx.Utilities
{
/// <summary>
/// Utility that provides continuous progress reporting to the console.
/// Utility for rendering current progress to the console that erases and rewrites output on every tick.
/// </summary>
public class ProgressReporter : IProgress<double>
public class ProgressTicker : IProgress<double>
{
private readonly IConsole _console;
private string _lastOutput = "";
/// <summary>
/// Initializes an instance of <see cref="ProgressReporter"/>.
/// Initializes an instance of <see cref="ProgressTicker"/>.
/// </summary>
public ProgressReporter(IConsole console)
public ProgressTicker(IConsole console)
{
_console = console;
}
@@ -33,7 +33,7 @@ namespace CliFx.Utilities
}
/// <summary>
/// Reports progress and renders it to console.
/// Erases previous output and renders new progress to the console.
/// If console's stdout is redirected, this method returns without doing anything.
/// </summary>
public void Report(double progress)

View File

@@ -265,15 +265,17 @@ var app = new CliApplicationBuilder()
### Report progress
CliFx comes with a simple utility for rendering progress to the console, `ProgressReporter`. It implements a well-known `IProgress<double>` interface so you can pass it to methods that are aware of this abstraction.
CliFx comes with a simple utility for reporting progress to the console, `ProgressTicker`, which renders progress in-place on every tick.
To avoid polluting output when it's not bound to a console, `ProgressReporter` will simply no-op if stdout is redirected.
It implements a well-known `IProgress<double>` interface so you can pass it to methods that are aware of this abstraction.
To avoid polluting output when it's not bound to a console, `ProgressTicker` will simply no-op if stdout is redirected.
```c#
var progressReporter = console.CreateProgressReporter();
var progressTicker = console.CreateProgressTicker();
for (var i = 0.0; i <= 1; i += 0.01)
progressReporter.Report(i);
progressTicker.Report(i);
```
### Testing