Skip to content

Commit

Permalink
Option to add child as indeterminate progress bar (#98)
Browse files Browse the repository at this point in the history
* Option to add indeterminate child progress bar

* add missing dispose
  • Loading branch information
diegosps authored Jun 7, 2022
1 parent f25b0ba commit 3f819bc
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
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

0 comments on commit 3f819bc

Please sign in to comment.