-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to add child as indeterminate progress bar (#98)
* Option to add indeterminate child progress bar * add missing dispose
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/ShellProgressBar.Example/Examples/IndeterminateChildrenNoCollapse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters