@@ -16,35 +16,42 @@ internal class FileDownloader : IDisposable
1616 private readonly SemaphoreSlim _throttler = new ( 4 , 4 ) ;
1717 private readonly IEnumerable < DownloadedFile > _files ;
1818 private readonly HttpClient _httpClient ;
19- private readonly ProgressContext _progressContext ;
19+ private readonly ProgressContext ? _progressContext ;
2020 private readonly ConsoleHelper _console ;
2121 private readonly StringTrimmer _descriptionTrimmer ;
2222 private readonly CancellationToken _cancellationToken ;
2323
2424 private bool _used ;
2525
26- public static Task < bool > DownloadAsync ( IEnumerable < DownloadedFile > files , HttpClient httpClient , ConsoleHelper console )
26+ public static Task < bool > DownloadAsync ( IEnumerable < DownloadedFile > files , HttpClient httpClient , ConsoleHelper console , bool showProgress )
2727 {
2828 var cancellationToken = ConsoleHelper . CancellationToken ;
2929
30- return Task . Run (
31- ( ) => AnsiConsole . Progress ( )
32- . Columns (
33- new TaskDescriptionColumn ( ) ,
34- new ProgressBarColumn ( ) ,
35- new DownloadedColumn ( ) ,
36- new TransferSpeedColumn ( ) ,
37- new RemainingTimeColumn ( ) ,
38- new ElapsedTimeColumn ( ) ,
39- new SpinnerColumn ( ) )
40- . StartAsync ( ctx => new FileDownloader ( files , httpClient , ctx , console , cancellationToken ) . DownloadAsync ( ) ) ,
41- cancellationToken ) ;
30+ if ( showProgress )
31+ {
32+ return Task . Run (
33+ ( ) => AnsiConsole . Progress ( )
34+ . Columns (
35+ new TaskDescriptionColumn ( ) ,
36+ new ProgressBarColumn ( ) ,
37+ new DownloadedColumn ( ) ,
38+ new TransferSpeedColumn ( ) ,
39+ new RemainingTimeColumn ( ) ,
40+ new ElapsedTimeColumn ( ) ,
41+ new SpinnerColumn ( ) )
42+ . StartAsync ( ctx => new FileDownloader ( files , httpClient , ctx , console , cancellationToken ) . DownloadAsync ( ) ) ,
43+ cancellationToken ) ;
44+ }
45+ else
46+ {
47+ return new FileDownloader ( files , httpClient , null , console , cancellationToken ) . DownloadAsync ( ) ;
48+ }
4249 }
4350
4451 private FileDownloader (
4552 IEnumerable < DownloadedFile > files ,
4653 HttpClient httpClient ,
47- ProgressContext progressContext ,
54+ ProgressContext ? progressContext ,
4855 ConsoleHelper console ,
4956 CancellationToken cancellationToken )
5057 {
@@ -57,7 +64,7 @@ private FileDownloader(
5764 }
5865
5966 private async Task < ( bool Result , DownloadedFile File , Exception ? Exception ) > DownloadFileAsync (
60- ProgressTask progress ,
67+ ProgressTask ? progress ,
6168 DownloadedFile file )
6269 {
6370 var attempt = 0 ;
@@ -70,13 +77,13 @@ private FileDownloader(
7077 {
7178 if ( attempt > 0 )
7279 {
73- ( ( IProgress < double > ) progress ) . Report ( 0 ) ;
80+ ( ( IProgress < double > ? ) progress ) ? . Report ( 0 ) ;
7481
7582 var delay = Math . Pow ( 2 , attempt ) ;
7683
7784 for ( var i = delay ; i > 0 ; i -- )
7885 {
79- progress . Description ( this . _descriptionTrimmer . Trim ( $ "{ file . Description } failed, retrying in { i } seconds" ) ) ;
86+ progress ? . Description ( this . _descriptionTrimmer . Trim ( $ "{ file . Description } failed, retrying in { i } seconds" ) ) ;
8087
8188 await Task . Delay ( TimeSpan . FromSeconds ( 1 ) , this . _cancellationToken ) ;
8289 }
@@ -86,7 +93,7 @@ private FileDownloader(
8693
8794 if ( attempt == 0 )
8895 {
89- progress . StartTask ( ) ;
96+ progress ? . StartTask ( ) ;
9097 }
9198
9299 var response = await this . _httpClient . GetAsync ( file . SourceUrl , HttpCompletionOption . ResponseHeadersRead , this . _cancellationToken ) ;
@@ -110,7 +117,7 @@ private FileDownloader(
110117 while ( ( bytesRead = await httpStream . ReadAsync ( buffer , 0 , buffer . Length , this . _cancellationToken ) ) != 0 )
111118 {
112119 await fileStream . WriteAsync ( buffer , 0 , bytesRead , this . _cancellationToken ) ;
113- progress . Increment ( bytesRead ) ;
120+ progress ? . Increment ( bytesRead ) ;
114121 }
115122
116123 return ( true , file , null ) ;
@@ -124,7 +131,7 @@ private FileDownloader(
124131 continue ;
125132 }
126133
127- progress . Description ( this . _descriptionTrimmer . Trim ( $ "{ file . Description } failed: { e . Message } " ) ) ;
134+ progress ? . Description ( this . _descriptionTrimmer . Trim ( $ "{ file . Description } failed: { e . Message } " ) ) ;
128135
129136 return ( false , file , e ) ;
130137 }
@@ -153,7 +160,7 @@ private async Task<bool> DownloadAsync()
153160
154161 foreach ( var file in this . _files )
155162 {
156- var progress = this . _progressContext . AddTask ( this . _descriptionTrimmer . Trim ( file . Description ) , false , file . Length ) ;
163+ var progress = this . _progressContext ? . AddTask ( this . _descriptionTrimmer . Trim ( file . Description ) , false , file . Length ) ;
157164 var task = this . DownloadFileAsync ( progress , file ) ;
158165
159166 pendingDownloads . Add ( task ) ;
0 commit comments