Skip to content

Commit

Permalink
support sub progressbars to any depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Apr 11, 2016
1 parent 3cabfa3 commit 2005ede
Show file tree
Hide file tree
Showing 26 changed files with 897 additions and 201 deletions.
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" ^
"C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe" ^
build\Build.proj /p:NuspecFile=build\ShellProgressBar.nuspec;BUILD_NUMBER=%1 /t:NugetPackage
2 changes: 1 addition & 1 deletion build/Build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

AssemblyVersion="$(Version)"
AssemblyFileVersion="$(Version)"/>
<MSBuild Projects="$(SolutionFile)" Properties="Configuration=$(Configuration)" />
<MSBuild Projects="$(SolutionFile)" Properties="Configuration=$(Configuration)" ToolsVersion="14.0" />
</Target>

<Target Name="NuGetPackage" DependsOnTargets="Compile">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class DeeplyNestedProgressBarTreeExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var random = new Random();

var numberOfSteps = 7;

var overProgressOptions = new ProgressBarOptions
{
BackgroundColor = ConsoleColor.DarkGray,
};

using (var pbar = new ProgressBar(numberOfSteps, "overal progress", overProgressOptions))
{
var stepBarOptions = new ProgressBarOptions
{
ForeGroundColor = ConsoleColor.Cyan,
ForeGroundColorDone = ConsoleColor.DarkGreen,
ProgressCharacter = '─',
BackgroundColor = ConsoleColor.DarkGray,
CollapseWhenFinished = false,

} ;
Parallel.For(0, numberOfSteps, (i) =>
{
var workBarOptions = new ProgressBarOptions
{
ForeGroundColor = ConsoleColor.Yellow,
ProgressCharacter = '─',
BackgroundColor = ConsoleColor.DarkGray,
};
var childSteps = random.Next(1, 5);
using (var childProgress = pbar.Spawn(childSteps, $"step {i} progress", stepBarOptions))
Parallel.For(0, childSteps, (ci) =>
{
var childTicks = random.Next(50, 250);
using (var innerChildProgress = childProgress.Spawn(childTicks, $"step {i}::{ci} progress", workBarOptions))
{
for (var r = 0; r < childTicks; r++)
{
innerChildProgress.Tick();
Program.BusyWait(50);
}
}
childProgress.Tick();
});

pbar.Tick();
});
}
return Task.FromResult(1);
}
}
}
23 changes: 23 additions & 0 deletions src/ShellProgressBar.Example/Examples/DrawsOnlyOnTickExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class DrawsOnlyOnTickExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 5;
var updateOnTicksOnlyOptions = new ProgressBarOptions {DisplayTimeInRealTime = false};
using (var pbar = new ProgressBar(ticks, "only update time on ticks", updateOnTicksOnlyOptions))
{
for (var i = 0; i < ticks; i++)
{
pbar.Tick("only update time on ticks, current: " + i);
Thread.Sleep(1750);
}
}
return Task.FromResult(1);
}
}
}
23 changes: 23 additions & 0 deletions src/ShellProgressBar.Example/Examples/LongRunningExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class LongRunningExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 100;
using (var pbar = new ProgressBar(ticks, "my long running operation", ConsoleColor.Green))
{
for (var i = 0; i < ticks; i++)
{
pbar.Tick("step " + i);
Thread.Sleep(50);
}
}
return Task.FromResult(1);
}
}
}
23 changes: 23 additions & 0 deletions src/ShellProgressBar.Example/Examples/NegativeMaxTicksExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class NegativeMaxTicksExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = -100;
using (var pbar = new ProgressBar(ticks, "my operation with negative ticks", ConsoleColor.Cyan))
{
for (var i = 0; i < ticks; i++)
{
pbar.Tick("step " + i);
Thread.Sleep(50);
}
}
return Task.FromResult(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class NestedProgressBarPerStepProgress : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var outerTicks = 10;
using (var pbar = new ProgressBar(outerTicks, "outer progress", ConsoleColor.Cyan))
{
for (var i = 0; i < outerTicks; i++)
{
InnerProgressBars(pbar);
pbar.Tick();
}
}
return Task.FromResult(1);
}

private static void InnerProgressBars(ProgressBar pbar)
{
var innerProgressBars = Enumerable.Range(0, new Random().Next(2, 6))
.Select(s => pbar.Spawn(new Random().Next(2, 5), $"inner bar {s}"))
.ToList();

var maxTicks = innerProgressBars.Max(p => p.MaxTicks);

for (var ii = 0; ii < maxTicks; ii++)
{
foreach (var p in innerProgressBars)
{
InnerInnerProgressBars(p);
p.Tick();
}


Thread.Sleep(4);
}
foreach (var p in innerProgressBars) p.Dispose();
}

private static void InnerInnerProgressBars(ChildProgressBar pbar)
{
var progressBarOption = new ProgressBarOptions { ForeGroundColor = ConsoleColor.Yellow };
var innerProgressBars = Enumerable.Range(0, new Random().Next(1, 3))
.Select(s => pbar.Spawn(new Random().Next(5, 10), $"inner bar {s}", progressBarOption))
.ToList();
if (!innerProgressBars.Any()) return;

var maxTicks = innerProgressBars.Max(p => p.MaxTicks);

for (var ii = 0; ii < maxTicks; ii++)
{
foreach (var p in innerProgressBars)
p.Tick();
}
foreach (var p in innerProgressBars) p.Dispose();
}
}
}
21 changes: 21 additions & 0 deletions src/ShellProgressBar.Example/Examples/NeverCompletesExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class NeverCompletesExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 5;
using (var pbar = new ProgressBar(ticks, "A console progress bar does not complete"))
{
pbar.Tick();
pbar.Tick();
pbar.Tick();
pbar.Tick();
}
return Task.FromResult(1);
}
}
}
17 changes: 17 additions & 0 deletions src/ShellProgressBar.Example/Examples/NeverTicksExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class NeverTicksExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 10;
using (var pbar = new ProgressBar(ticks, "A console progress bar that never ticks"))
{
}
return Task.FromResult(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class ThreadedTicksOverflowExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 200;
using (var pbar = new ProgressBar(ticks/10, "My operation that ticks to often using threads", ConsoleColor.Cyan))
{
var threads = Enumerable.Range(0, ticks).Select(i => new Thread(() => pbar.Tick("threaded tick " + i))).ToList();
foreach (var thread in threads) thread.Start();
foreach (var thread in threads) thread.Join();
}
return Task.FromResult(1);
}
}
}
23 changes: 23 additions & 0 deletions src/ShellProgressBar.Example/Examples/TicksOverflowExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class TicksOverflowExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 10;
using (var pbar = new ProgressBar(ticks, "My operation that ticks to often", ConsoleColor.Cyan))
{
for (var i = 0; i < ticks*10; i++)
{
pbar.Tick("too many steps " + i);
Thread.Sleep(50);
}
}
return Task.FromResult(1);
}
}
}
30 changes: 30 additions & 0 deletions src/ShellProgressBar.Example/Examples/UpdatesMaxTicksExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class UpdatesMaxTicksExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 10;
using (var pbar = new ProgressBar(ticks, "My operation that updates maxTicks", ConsoleColor.Cyan))
{
var sleep = 1000;
for (var i = 0; i < ticks; i++)
{
pbar.Tick("Updating maximum ticks " + i);
if (i == 5)
{
ticks = 120;
pbar.UpdateMaxTicks(ticks);
sleep = 50;
}
Thread.Sleep(sleep);
}
}
return Task.FromResult(1);
}
}
}
23 changes: 23 additions & 0 deletions src/ShellProgressBar.Example/Examples/ZeroMaxTicksExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class ZeroMaxTicksExample : IProgressBarExample
{
public Task Start(CancellationToken token)
{
var ticks = 0;
using (var pbar = new ProgressBar(ticks, "my operation with zero ticks", ConsoleColor.Cyan))
{
for (var i = 0; i < ticks; i++)
{
pbar.Tick("step " + i);
Thread.Sleep(50);
}
}
return Task.FromResult(1);
}
}
}
10 changes: 10 additions & 0 deletions src/ShellProgressBar.Example/IProgressBarExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example
{
public interface IProgressBarExample
{
Task Start(CancellationToken token);
}
}
Loading

0 comments on commit 2005ede

Please sign in to comment.