Example #1
0
        private static DownloaderResult Download(Downloader downloader, Uri url, ICredentials credentials)
        {
            var progressState = new ProgresState {
                LastPercent = -1.0,
                LastRemainingTimeString = null,
                LastBytesPerSecondString = null
            };

            var result = downloader.Download(url, new DownloaderOptions {
                Credentials = credentials,
                ReportProgress = p => ReportDownloadProgress(p, progressState)
            });
            SetTaskBarProgress(TaskbarProgressBarState.NoProgress);

            return result;
        }
Example #2
0
        private static void ReportDownloadProgress(DownloaderProgress progress, ProgresState progressState)
        {
            if (progress.BytesTotal <= 0) { // length is unknown
                SetTaskBarProgress(TaskbarProgressBarState.Indeterminate);
                return;
            }

            var indent = ConsoleIndent.Length;
            var percent = 100 * (double)(progress.BytesDownloadedBefore + progress.BytesDownloaded) / progress.BytesTotal;
            if (Math.Abs(percent - progressState.LastPercent) >= 0.1) {
                Console.SetCursorPosition(indent, Console.CursorTop);
                Console.Write("{0,4:F1}% |", percent);
                progressState.LastPercent = percent;

                SetTaskBarProgress(TaskbarProgressBarState.Normal);
                SetTaskBarProgress(percent);
            }

            indent += "99.9% | ".Length;
            var bps = progress.BytesDownloaded/progress.TimeElapsed.TotalSeconds;
            var bpsString = FormatBytesPerSecond(bps);
            if (bpsString != progressState.LastBytesPerSecondString) {
                Console.SetCursorPosition(indent, Console.CursorTop);
                Console.Write(bpsString.PadRight("999 TB/s".Length));
                Console.Write(" |");
                progressState.LastBytesPerSecondString = bpsString;
            }

            indent += "999 TB/s | ".Length;
            var msRemaining = progress.BytesRemaining * (progress.TimeElapsed.TotalMilliseconds / progress.BytesDownloaded);
            var timeRemaining = TimeSpan.FromMilliseconds(msRemaining);
            var timeRemainingString = FormatRemainingTime(timeRemaining);
            if (timeRemainingString != progressState.LastRemainingTimeString) {
                Console.SetCursorPosition(indent, Console.CursorTop);
                Console.Write("Remaining: {0}.".PadRight(40), timeRemainingString);
                progressState.LastRemainingTimeString = timeRemainingString;
                Console.Title = string.Format("{0}: {1}", Regex.Replace(timeRemainingString, @"(\d)\s(\w)\w*", "$1$2"), progress.File.Name);
            }
        }