|
|
@ -1,113 +1,62 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
|
|
|
using System.Reactive.Subjects;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Common;
|
|
|
|
namespace Common;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// An ASCII progress bar
|
|
|
|
/// An ASCII progress bar
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
public sealed class ProgressBar //: IProgress<double>
|
|
|
|
/// Full credit to: https://gist.github.com/DanielSWolf/0ab6a96899cc5377bf54
|
|
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
|
|
[SuppressMessage("Reliability", "CA2002:Do not lock on objects with weak identity",
|
|
|
|
|
|
|
|
Justification = "Code is from third party")]
|
|
|
|
|
|
|
|
public sealed class ProgressBar : IDisposable, IProgress<double>
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private const int BlockCount = 10;
|
|
|
|
|
|
|
|
private readonly TimeSpan _animationInterval = TimeSpan.FromSeconds(1.0 / 8);
|
|
|
|
private readonly TimeSpan _animationInterval = TimeSpan.FromSeconds(1.0 / 8);
|
|
|
|
private const string Animation = @"|/-\";
|
|
|
|
private const string Animation = @"|/-\";
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Timer _timer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private double _currentProgress;
|
|
|
|
|
|
|
|
private string _currentText = string.Empty;
|
|
|
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
private int _animationIndex;
|
|
|
|
private int _animationIndex;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Subject<float> _reportProgress = new();
|
|
|
|
|
|
|
|
public IObserver<float> ReportProgress => _reportProgress;
|
|
|
|
|
|
|
|
public string Description { get; set; } = "";
|
|
|
|
|
|
|
|
|
|
|
|
public ProgressBar()
|
|
|
|
public ProgressBar()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_timer = new Timer(TimerHandler);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A progress bar is only for temporary display in a console window.
|
|
|
|
// A progress bar is only for temporary display in a console window.
|
|
|
|
// If the console output is redirected to a file, draw nothing.
|
|
|
|
// If the console output is redirected to a file, draw nothing.
|
|
|
|
// Otherwise, we'll end up with a lot of garbage in the target file.
|
|
|
|
// Otherwise, we'll end up with a lot of garbage in the target file.
|
|
|
|
if (!Console.IsOutputRedirected)
|
|
|
|
if (!Console.IsOutputRedirected)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ResetTimer();
|
|
|
|
_reportProgress.Sample(_animationInterval)
|
|
|
|
|
|
|
|
.Select(CalculateText)
|
|
|
|
|
|
|
|
.StartWith(string.Empty)
|
|
|
|
|
|
|
|
.Buffer(2, 1) // sliding window: take previous and current
|
|
|
|
|
|
|
|
.Subscribe(x => UpdateText(x[0].Length, x[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Report(double value)
|
|
|
|
private string CalculateText(float progress)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Make sure value is in [0..1] range
|
|
|
|
|
|
|
|
value = Math.Max(0, Math.Min(1, value));
|
|
|
|
|
|
|
|
Interlocked.Exchange(ref _currentProgress, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void TimerHandler(object? state)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock (_timer)
|
|
|
|
const int blockCount = 10;
|
|
|
|
{
|
|
|
|
var progressBlockCount = (int) (progress * blockCount);
|
|
|
|
if (_disposed)
|
|
|
|
var percent = (int) (progress * 100);
|
|
|
|
{
|
|
|
|
var progressBlocks = new string('#', progressBlockCount);
|
|
|
|
return;
|
|
|
|
var progressBlocksUnfilled = new string('-', blockCount - progressBlockCount);
|
|
|
|
}
|
|
|
|
var currentAnimationFrame = Animation[_animationIndex++ % Animation.Length];
|
|
|
|
|
|
|
|
return $"[{progressBlocks}{progressBlocksUnfilled}] {percent,3}% {currentAnimationFrame} {Description}";
|
|
|
|
var progressBlockCount = (int) (_currentProgress * BlockCount);
|
|
|
|
|
|
|
|
var percent = (int) (_currentProgress * 100);
|
|
|
|
|
|
|
|
var progressBlocks = new string('#', progressBlockCount);
|
|
|
|
|
|
|
|
var progressBlocksUnfilled = new string('-', BlockCount - progressBlockCount);
|
|
|
|
|
|
|
|
var currentAnimationFrame = Animation[_animationIndex++ % Animation.Length];
|
|
|
|
|
|
|
|
var text = $"[{progressBlocks}{progressBlocksUnfilled}] {percent,3}% {currentAnimationFrame}";
|
|
|
|
|
|
|
|
UpdateText(text);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ResetTimer();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateText(string text)
|
|
|
|
private static void UpdateText(int previousTextLength, string text)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Get length of common portion
|
|
|
|
|
|
|
|
var commonPrefixLength = 0;
|
|
|
|
|
|
|
|
var commonLength = Math.Min(_currentText.Length, text.Length);
|
|
|
|
|
|
|
|
while (commonPrefixLength < commonLength && text[commonPrefixLength] == _currentText[commonPrefixLength])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
commonPrefixLength++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Backtrack to the first differing character
|
|
|
|
|
|
|
|
var outputBuilder = new StringBuilder();
|
|
|
|
var outputBuilder = new StringBuilder();
|
|
|
|
outputBuilder.Append('\b', _currentText.Length - commonPrefixLength);
|
|
|
|
outputBuilder.Append('\r');
|
|
|
|
|
|
|
|
outputBuilder.Append(text);
|
|
|
|
// Output new suffix
|
|
|
|
|
|
|
|
outputBuilder.Append(text.AsSpan(commonPrefixLength));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the new text is shorter than the old one: delete overlapping characters
|
|
|
|
// If the previous string was longer, "erase" the old characters with spaces.
|
|
|
|
var overlapCount = _currentText.Length - text.Length;
|
|
|
|
var lengthDifference = previousTextLength - text.Length;
|
|
|
|
if (overlapCount > 0)
|
|
|
|
if (lengthDifference > 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
outputBuilder.Append(' ', overlapCount);
|
|
|
|
outputBuilder.Append(' ', lengthDifference);
|
|
|
|
outputBuilder.Append('\b', overlapCount);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.Write(outputBuilder);
|
|
|
|
Console.Write(outputBuilder);
|
|
|
|
_currentText = text;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ResetTimer()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_timer.Change(_animationInterval, TimeSpan.FromMilliseconds(-1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lock (_timer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
UpdateText(string.Empty);
|
|
|
|
|
|
|
|
_timer.Dispose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|