Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Report progression with ease by using multilple components such as Bars, Spinner
using var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
Expand Down Expand Up @@ -42,10 +44,6 @@ var descriptor = BarDescriptor.Default;
### Define the reporter to use the component
```csharp
var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
.UsingComponentDescriptor(descriptor)
.Build(Worker.AllItems);
Expand Down
2 changes: 2 additions & 0 deletions src/Progress.Samples.Bar.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
Expand Down
2 changes: 2 additions & 0 deletions src/Progress.Samples.HearthBeat.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
Expand Down
2 changes: 2 additions & 0 deletions src/Progress.Samples.Pulse.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
Expand Down
2 changes: 2 additions & 0 deletions src/Progress.Samples.Spinner.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
Expand Down
73 changes: 73 additions & 0 deletions src/Progress.UnitTest/TimerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace Progress.UnitTest;

public class TimerTests
{
[Fact]
public void GivenNothingDone_WhenGettingRemainingTime_ThenReturnsMaxValue()
{
// Arrange
var timer = Timer.Start();

// Act
var actual = timer.GetRemainingTime(0);

// Assert
actual.Should().Be(TimeSpan.MaxValue);
}


[Fact]
public void GivenEverythingDone_WhenGettingRemainingTime_ThenReturnsZero()
{
// Arrange
var timer = Timer.Start();

// Act
var actual = timer.GetRemainingTime(100);

// Assert
actual.Should().Be(TimeSpan.Zero);
}

[Fact]
public void GivenNothingDone_WhenGettingEstimatedTimeOfArrival_ThenReturnsMaxValue()
{
// Arrange
var timer = Timer.Start();

// Act
var actual = timer.GetEstimatedTimeOfArrival(0);

// Assert
actual.Should().Be(DateTimeOffset.MaxValue);
}

[Fact]
public void GivenInstance_WhenGettingStartedOn_ThenReturnsMomentOfCreation()
{
// Arrange
var moment = DateTimeOffset.UtcNow;
var timer = new Timer(moment);

// Act
var actual = timer.StartedOn;

// Assert
actual.Should().Be(moment);
}

[Fact]
public async Task GivenSomeWorkDone_WhenGettingElapsedTime_ThenReturnsTime()
{
// Arrange
var timer = Timer.Start();
var delay = TimeSpan.FromSeconds(1);
await Task.Delay(delay);

// Act
var actual = timer.ElapsedTime;

// Assert
actual.Should().BeGreaterThanOrEqualTo(delay);
}
}
7 changes: 3 additions & 4 deletions src/Progress/Components/Bar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal class Bar : Component
private readonly uint _width;
private readonly char[] _bar;

private Percent _percent = default!;
private ulong _availableItems;
private ulong _count;
private int _adjustedPercent;
Expand All @@ -26,7 +25,7 @@ public override Component Next(ulong availableItems, ulong currentCount)
{
_availableItems = availableItems;
_count = currentCount;
_percent = Calculate(availableItems, currentCount);
CurrentPercent = Calculate(availableItems, currentCount);
return this;
}

Expand All @@ -45,15 +44,15 @@ public override string ToString()
if (DisplayPercent)
{
sBuilder.Append(' ');
sBuilder.Append(_percent.ToString());
sBuilder.Append(CurrentPercent.ToString());
}

return sBuilder.ToString();
}

private void Fill()
{
if (_percent.Value > 0)
if (CurrentPercent.Value > 0)
_adjustedPercent = (int)(_count / (decimal)_availableItems * _width);

if (_adjustedPercent <= _bar.Length)
Expand Down
8 changes: 5 additions & 3 deletions src/Progress/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ internal abstract class Component
{
private static readonly CultureInfo Culture = new("en-US");

public Percent CurrentPercent { get; protected set; } = default!;

public abstract Component Next(ulong availableItems, ulong currentCount);

protected static Percent Calculate(ulong availableItems, ulong currentCount) => new(availableItems, currentCount);

internal class Percent
{
private readonly decimal _percent;
private readonly double _percent;

public Percent(ulong items, ulong count)
{
if (count > 0)
_percent = count / (decimal)items;
_percent = count / (double)items;
}

public decimal Value => _percent * 100;
public double Value => _percent * 100;

public override string ToString() => _percent.ToString("P02", Culture);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Progress/Components/HearthBeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ internal class HearthBeat : Component

private uint _leftIndex;
private uint _rightIndex;
private Percent _percent = default!;

public HearthBeat(uint width, char progressSymbol)
{
Expand All @@ -21,7 +20,7 @@ public HearthBeat(uint width, char progressSymbol)

public override Component Next(ulong availableItems, ulong currentCount)
{
_percent = Calculate(availableItems, currentCount);
CurrentPercent = Calculate(availableItems, currentCount);

if (_leftIndex == 0)
{
Expand Down Expand Up @@ -53,7 +52,7 @@ public override string ToString()
if (DisplayPercent)
{
sBuilder.Append(' ');
sBuilder.Append(_percent.ToString());
sBuilder.Append(CurrentPercent.ToString());
}

return sBuilder.ToString();
Expand All @@ -74,7 +73,7 @@ private void Fill()
{
Array.Fill(_bar, ' ');

if (_percent.Value < 100)
if (CurrentPercent.Value < 100)
{
_bar[_leftIndex] = _progressSymbol;
_bar[_rightIndex] = _progressSymbol;
Expand Down
7 changes: 3 additions & 4 deletions src/Progress/Components/Pulse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal class Pulse : Component
private readonly char[] _bar;

private uint _index;
private Percent _percent = default!;

public Pulse(uint width, char progressSymbol)
{
Expand All @@ -21,7 +20,7 @@ public Pulse(uint width, char progressSymbol)

public override Component Next(ulong availableItems, ulong currentCount)
{
_percent = Calculate(availableItems, currentCount);
CurrentPercent = Calculate(availableItems, currentCount);

_index++;

Expand All @@ -46,7 +45,7 @@ public override string ToString()
if (DisplayPercent)
{
sBuilder.Append(' ');
sBuilder.Append(_percent.ToString());
sBuilder.Append(CurrentPercent.ToString());
}

return sBuilder.ToString();
Expand All @@ -56,7 +55,7 @@ private void Fill()
{
Array.Fill(_bar, ' ');

if (_percent.Value < 100)
if (CurrentPercent.Value < 100)
_bar[_index] = _progressSymbol;
}
}
6 changes: 2 additions & 4 deletions src/Progress/Components/Spinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ internal class Spinner : Component
private int _counter;
private char _current;

private Percent _percent = default!;

public bool DisplayPercent { get; init; } = true;

public override Component Next(ulong availableItems, ulong currentCount)
{
_percent = Calculate(availableItems, currentCount);
CurrentPercent = Calculate(availableItems, currentCount);
return this;
}

Expand All @@ -27,7 +25,7 @@ public override string ToString()
if (DisplayPercent)
{
sBuilder.Append(' ');
sBuilder.Append(_percent.ToString());
sBuilder.Append(CurrentPercent.ToString());
}

return sBuilder.ToString();
Expand Down
16 changes: 15 additions & 1 deletion src/Progress/Content/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Report progression with ease by using multilple components such as Bars, Spinner
using var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
Expand Down Expand Up @@ -43,10 +45,12 @@ var descriptor = BarDescriptor.Default;
var reporter = new ReporterBuilder()
.DisplayingStartingTime()
.DisplayingElapsedTime()
.DisplayingTimeOfArrival()
.DisplayingRemainingTime()
.DisplayingItemsSummary()
.DisplayingItemsOverview()
.UsingReportingFrequency(TimeSpan.FromMilliseconds(50))
.UsingComponentDescriptor(descriptor)
.UsingComponentDescriptor(BarDescriptor.Default)
.Build(Worker.AllItems);
```

Expand All @@ -55,6 +59,16 @@ var reporter = new ReporterBuilder()
reporter.Start();
```

### Stop the reporter
```csharp
reporter.Stop();
```

### Resume the reporter
```csharp
reporter.Resume();
```

### Report progress
```csharp
reporter.ReportSuccess()
Expand Down
2 changes: 1 addition & 1 deletion src/Progress/Progress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<PropertyGroup>
<PackageId>Progress</PackageId>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Gerard Castello</Authors>
<Title>Report progress with ease</Title>
Expand Down
Loading