Skip to content

Commit

Permalink
fixed Sample.Program.cs file to support MacOs console keyRead without…
Browse files Browse the repository at this point in the history
… wait on
  • Loading branch information
bezzad committed Aug 27, 2024
1 parent b87035e commit ffb5fbd
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/Samples/Downloader.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public partial class Program

private static async Task Main()
{
// Recover the standard output stream so that a
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);

try
{
#if NETCOREAPP
DummyHttpServer.HttpServer.Run(3333);
#endif
await Task.Delay(1000);
Console.Clear();
Initial();
Expand All @@ -42,18 +46,17 @@ private static async Task Main()
catch (Exception e)
{
Console.Clear();
Console.Error.WriteLine(e);
await Console.Error.WriteLineAsync(e.Message);
Debugger.Break();
}
finally
{
#if NETCOREAPP
await DummyHttpServer.HttpServer.Stop();
#endif
}

Console.WriteLine("END");
await Console.Out.WriteLineAsync("END");
}

private static void Initial()
{
CancelAllTokenSource = new CancellationTokenSource();
Expand All @@ -75,18 +78,25 @@ private static void Initial()
ProgressBarOnBottom = true
};
}

private static void KeyboardHandler()
{
ConsoleKeyInfo cki;
Console.CancelKeyPress += CancelAll;
Console.CancelKeyPress += (_, _) => CancelAll();

while (true)
{
cki = Console.ReadKey(true);
if (CurrentDownloadConfiguration != null)
while (Console.KeyAvailable)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.C:
if (cki.Modifiers == ConsoleModifiers.Control)
{
CancelAll();
return;
}
break;
case ConsoleKey.P:
CurrentDownloadService?.Pause();
Console.Beep();
Expand All @@ -98,16 +108,19 @@ private static void KeyboardHandler()
CurrentDownloadService?.CancelAsync();
break;
case ConsoleKey.UpArrow:
CurrentDownloadConfiguration.MaximumBytesPerSecond *= 2;
if (CurrentDownloadConfiguration != null)
CurrentDownloadConfiguration.MaximumBytesPerSecond *= 2;
break;
case ConsoleKey.DownArrow:
CurrentDownloadConfiguration.MaximumBytesPerSecond /= 2;
if (CurrentDownloadConfiguration != null)
CurrentDownloadConfiguration.MaximumBytesPerSecond /= 2;
break;
}
}
}
}
private static void CancelAll(object sender, ConsoleCancelEventArgs e)

private static void CancelAll()
{
CancelAllTokenSource.Cancel();
CurrentDownloadService?.CancelAsync();
Expand Down Expand Up @@ -144,18 +157,23 @@ private static async Task<IDownloadService> DownloadFile(DownloadItem downloadIt
{
Logger = FileLogger.Factory(downloadItem.FolderPath);
CurrentDownloadService.AddLogger(Logger);
await CurrentDownloadService.DownloadFileTaskAsync(downloadItem.Url, new DirectoryInfo(downloadItem.FolderPath)).ConfigureAwait(false);
await CurrentDownloadService
.DownloadFileTaskAsync(downloadItem.Url, new DirectoryInfo(downloadItem.FolderPath))
.ConfigureAwait(false);
}
else
{
Logger = FileLogger.Factory(downloadItem.FolderPath, Path.GetFileName(downloadItem.FileName));
CurrentDownloadService.AddLogger(Logger);
await CurrentDownloadService.DownloadFileTaskAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);
await CurrentDownloadService.DownloadFileTaskAsync(downloadItem.Url, downloadItem.FileName)
.ConfigureAwait(false);
}

if (downloadItem.ValidateData)
{
var isValid = await ValidateDataAsync(CurrentDownloadService.Package.FileName, CurrentDownloadService.Package.TotalFileSize).ConfigureAwait(false);
var isValid =
await ValidateDataAsync(CurrentDownloadService.Package.FileName,
CurrentDownloadService.Package.TotalFileSize).ConfigureAwait(false);
if (!isValid)
{
var message = "Downloaded data is invalid: " + CurrentDownloadService.Package.FileName;
Expand All @@ -175,7 +193,8 @@ private static async Task<bool> ValidateDataAsync(string filename, long size)
var next = stream.ReadByte();
if (next != i % 256)
{
Logger?.LogWarning($"Sample.Program.ValidateDataAsync(): Data at index [{i}] of `{filename}` is `{next}`, expectation is `{i % 256}`");
Logger?.LogWarning(
$"Sample.Program.ValidateDataAsync(): Data at index [{i}] of `{filename}` is `{next}`, expectation is `{i % 256}`");
return false;
}
}
Expand All @@ -195,6 +214,7 @@ private static async Task WriteKeyboardGuidLines()
await Console.Out.FlushAsync();
await Task.Yield();
}

private static DownloadService CreateDownloadService(DownloadConfiguration config)
{
var downloadService = new DownloadService(config);
Expand Down Expand Up @@ -242,7 +262,6 @@ private static void OnDownloadFileCompleted(object sender, AsyncCompletedEventAr
else
{
ConsoleProgress.Message += " DONE";
Console.Title = "100%";
}

foreach (var child in ChildConsoleProgresses.Values)
Expand Down

0 comments on commit ffb5fbd

Please sign in to comment.