Skip to content

Commit

Permalink
Add download data example to illustrate #56
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Feb 23, 2020
1 parent 429b192 commit ad95fe1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/ShellProgressBar.Example/Examples/DownloadPogressExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Linq;
using System.Net;
using System.Threading;

namespace ShellProgressBar.Example.Examples
{
public class DownloadProgressExample : ExampleBase
{
protected override void Start()
{
var files = new string[]
{
"https://github.com/Mpdreamz/shellprogressbar/archive/4.3.0.zip",
"https://github.com/Mpdreamz/shellprogressbar/archive/4.2.0.zip",
"https://github.com/Mpdreamz/shellprogressbar/archive/4.1.0.zip",
"https://github.com/Mpdreamz/shellprogressbar/archive/4.0.0.zip",
};
var childOptions = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ProgressCharacter = '\u2593'
};
using var pbar = new ProgressBar(files.Length, "downloading");
foreach (var (file, i) in files.Select((f, i) => (f, i)))
{
byte[] data = null;
using var child = pbar.Spawn(100, "page: " + i, childOptions);
try
{
using var client = new WebClient();
client.DownloadProgressChanged += (o, args) => child.Tick(args.ProgressPercentage);
client.DownloadDataCompleted += (o, args) => data = args.Result;
client.DownloadDataAsync(new Uri(file));
while (client.IsBusy)
{
Thread.Sleep(1);
}

pbar.Tick();
}
catch (WebException error)
{
pbar.WriteLine(error.Message);
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/ShellProgressBar.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class Program
new IntegrationWithIProgressPercentageExample(),
new MessageBeforeAndAfterExample(),
new DeeplyNestedProgressBarTreeExample(),
new EstimatedDurationExample()
new EstimatedDurationExample(),
new DownloadProgressExample()
};

static void Main(string[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<AssemblyVersion>$(CurrentAssemblyVersion)</AssemblyVersion>
<FileVersion>$(CurrentAssemblyFileVersion)</FileVersion>
<IsPackable>False</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ShellProgressBar\ShellProgressBar.csproj" />
Expand Down
3 changes: 3 additions & 0 deletions src/ShellProgressBar/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ void TopHalf()
{
TopHalf();
Console.SetCursorPosition(0, ++cursorTop);
ProgressBarBottomHalf(percentage, child.StartDate, child.EndTime, child.Message, childIndentation,
child.Options.ProgressBarOnBottom, child.Options.ShowEstimatedDuration,
child.EstimatedDuration);

DrawChildren(child.Children, childIndentation, ref cursorTop);
}
Expand Down

0 comments on commit ad95fe1

Please sign in to comment.