Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to add child as indeterminate progress bar #98

Merged
merged 2 commits into from
Jun 7, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Threading;

namespace ShellProgressBar.Example.Examples
{
public class IndeterminateChildrenNoCollapseExample : ExampleBase
{
protected override void Start()
{
const int totalChildren = 10;
Random random = new Random();
var options = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
BackgroundColor = ConsoleColor.DarkGray,
ProgressCharacter = '─'
};
var childOptions = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Green,
BackgroundColor = ConsoleColor.DarkGray,
ProgressCharacter = '─',
CollapseWhenFinished = false
};
using (var pbar = new ProgressBar(totalChildren, "main progressbar", options))
{
for (int i = 1; i <= totalChildren; i++)
{
pbar.Message = $"Start {i} of {totalChildren}: main progressbar";
using (var child = pbar.SpawnIndeterminate("child action " + i, childOptions))
{
Thread.Sleep(1000 * random.Next(5, 15));
child.Finished();
}
pbar.Tick();
}
}
}
}
}
1 change: 1 addition & 0 deletions src/ShellProgressBar.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Program
new NeverTicksExample(),
new EstimatedDurationExample(),
new IndeterminateProgressExample(),
new IndeterminateChildrenNoCollapseExample(),
};

private static readonly IList<IProgressBarExample> Examples = new List<IProgressBarExample>
Expand Down
61 changes: 61 additions & 0 deletions src/ShellProgressBar/IndeterminateChildProgressBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace ShellProgressBar
{
public class IndeterminateChildProgressBar : ChildProgressBar
{
private const int MaxTicksForIndeterminate = 20;
private Timer _timer;
internal IndeterminateChildProgressBar(
string message,
Action scheduleDraw,
Action<string> writeLine,
Action<string> writeError,
ProgressBarOptions options = null,
Action<ProgressBarHeight> growth = null
)
: base(MaxTicksForIndeterminate, message, scheduleDraw, writeLine, writeError, options, growth)
{
if (options == null)
{
options = new ProgressBarOptions();
}

options.DisableBottomPercentage = true;
_timer = new Timer((s) => OnTimerTick(), null, 500, 500);
}

private long _seenTicks = 0;

protected void OnTimerTick()
{
Interlocked.Increment(ref _seenTicks);
if (_seenTicks == MaxTicksForIndeterminate - 1)
{
this.Tick(0);
Interlocked.Exchange(ref _seenTicks, 0);
}
else
{
this.Tick();
}
DisplayProgress();
}

public void Finished()
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_timer.Dispose();
Tick(MaxTicksForIndeterminate);
}

public void Dispose()
{
if (_timer != null) _timer.Dispose();
foreach (var c in this.Children) c.Dispose();
OnDone();
}
}
}
14 changes: 14 additions & 0 deletions src/ShellProgressBar/ProgressBarBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public ChildProgressBar Spawn(int maxTicks, string message, ProgressBarOptions o
return pbar;
}

public IndeterminateChildProgressBar SpawnIndeterminate(string message, ProgressBarOptions options = null)
{
// if this bar collapses all child progressbar will collapse
if (options?.CollapseWhenFinished == false && this.Options.CollapseWhenFinished)
options.CollapseWhenFinished = true;

var pbar = new IndeterminateChildProgressBar(
message, DisplayProgress, WriteLine, WriteErrorLine, options ?? this.Options, d => this.Grow(d)
);
this.Children.Add(pbar);
DisplayProgress();
return pbar;
}

public abstract void WriteLine(string message);
public abstract void WriteErrorLine(string message);

Expand Down