@@ -198,7 +198,7 @@ pub(crate) struct DownloadTracker {
198198 /// the message "retrying download" for at least a second.
199199 /// Without it, the progress bar would reappear immediately, not allowing the user to
200200 /// correctly see the message, before the progress bar starts again.
201- file_progress_bars : Mutex < HashMap < String , ( ProgressBar , Option < Instant > ) > > ,
201+ file_progress_bars : Mutex < HashMap < String , DownloadStatus > > ,
202202}
203203
204204impl DownloadTracker {
@@ -218,42 +218,90 @@ impl DownloadTracker {
218218
219219 /// Creates a new ProgressBar for the given component.
220220 pub ( crate ) fn create_progress_bar ( & self , component : String , url : String ) {
221- let pb = ProgressBar :: hidden ( ) ;
222- pb. set_style (
223- ProgressStyle :: with_template (
224- "{msg:>12.bold} [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA: {eta})" ,
225- )
226- . unwrap ( )
227- . progress_chars ( "## " ) ,
228- ) ;
229- pb. set_message ( component) ;
230- self . multi_progress_bars . add ( pb. clone ( ) ) ;
231- self . file_progress_bars
232- . lock ( )
233- . unwrap ( )
234- . insert ( url, ( pb, None ) ) ;
221+ let status = DownloadStatus :: new ( component) ;
222+ self . multi_progress_bars . add ( status. progress . clone ( ) ) ;
223+ self . file_progress_bars . lock ( ) . unwrap ( ) . insert ( url, status) ;
235224 }
236225
237226 /// Sets the length for a new ProgressBar and gives it a style.
238227 pub ( crate ) fn content_length_received ( & self , content_len : u64 , url : & str ) {
239- if let Some ( ( pb, _) ) = self . file_progress_bars . lock ( ) . unwrap ( ) . get ( url) {
240- pb. reset ( ) ;
241- pb. set_length ( content_len) ;
228+ if let Some ( status) = self . file_progress_bars . lock ( ) . unwrap ( ) . get ( url) {
229+ status. received_length ( content_len) ;
242230 }
243231 }
244232
245233 /// Notifies self that data of size `len` has been received.
246234 pub ( crate ) fn data_received ( & self , len : usize , url : & str ) {
247235 let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
248- let Some ( ( pb , retry_time ) ) = map. get_mut ( url) else {
249- return ;
236+ if let Some ( status ) = map. get_mut ( url) {
237+ status . received_data ( len ) ;
250238 } ;
251- pb. inc ( len as u64 ) ;
252- if !retry_time. is_some_and ( |instant| instant. elapsed ( ) > Duration :: from_secs ( 1 ) ) {
239+ }
240+
241+ /// Notifies self that the download has finished.
242+ pub ( crate ) fn download_finished ( & self , url : & str ) {
243+ let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
244+ if let Some ( status) = map. get ( url) {
245+ status. finished ( )
246+ } ;
247+ }
248+
249+ /// Notifies self that the download has failed.
250+ pub ( crate ) fn download_failed ( & self , url : & str ) {
251+ let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
252+ if let Some ( status) = map. get ( url) {
253+ status. failed ( ) ;
254+ } ;
255+ }
256+
257+ /// Notifies self that the download is being retried.
258+ pub ( crate ) fn retrying_download ( & self , url : & str ) {
259+ let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
260+ if let Some ( status) = map. get_mut ( url) {
261+ status. retrying ( ) ;
262+ } ;
263+ }
264+ }
265+
266+ struct DownloadStatus {
267+ progress : ProgressBar ,
268+ retry_time : Option < Instant > ,
269+ }
270+
271+ impl DownloadStatus {
272+ fn new ( component : String ) -> Self {
273+ let progress = ProgressBar :: hidden ( ) ;
274+ progress. set_style (
275+ ProgressStyle :: with_template (
276+ "{msg:>12.bold} [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA: {eta})" ,
277+ )
278+ . unwrap ( )
279+ . progress_chars ( "## " ) ,
280+ ) ;
281+ progress. set_message ( component) ;
282+
283+ Self {
284+ progress,
285+ retry_time : None ,
286+ }
287+ }
288+
289+ fn received_length ( & self , len : u64 ) {
290+ self . progress . reset ( ) ;
291+ self . progress . set_length ( len) ;
292+ }
293+
294+ fn received_data ( & mut self , len : usize ) {
295+ self . progress . inc ( len as u64 ) ;
296+ if !self
297+ . retry_time
298+ . is_some_and ( |instant| instant. elapsed ( ) > Duration :: from_secs ( 1 ) )
299+ {
253300 return ;
254301 }
255- * retry_time = None ;
256- pb. set_style (
302+
303+ self . retry_time = None ;
304+ self . progress . set_style (
257305 ProgressStyle :: with_template (
258306 "{msg:>12.bold} [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA: {eta})" ,
259307 )
@@ -262,40 +310,26 @@ impl DownloadTracker {
262310 ) ;
263311 }
264312
265- /// Notifies self that the download has finished.
266- pub ( crate ) fn download_finished ( & self , url : & str ) {
267- let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
268- let Some ( ( pb, _) ) = map. get ( url) else {
269- return ;
270- } ;
271- pb. set_style (
313+ fn finished ( & self ) {
314+ self . progress . set_style (
272315 ProgressStyle :: with_template ( "{msg:>12.bold} downloaded {total_bytes} in {elapsed}" )
273316 . unwrap ( ) ,
274317 ) ;
275- pb . finish ( ) ;
318+ self . progress . finish ( ) ;
276319 }
277320
278- /// Notifies self that the download has failed.
279- pub ( crate ) fn download_failed ( & self , url : & str ) {
280- let map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
281- let Some ( ( pb, _) ) = map. get ( url) else {
282- return ;
283- } ;
284- pb. set_style (
321+ fn failed ( & self ) {
322+ self . progress . set_style (
285323 ProgressStyle :: with_template ( "{msg:>12.bold} download failed after {elapsed}" )
286324 . unwrap ( ) ,
287325 ) ;
288- pb . finish ( ) ;
326+ self . progress . finish ( ) ;
289327 }
290328
291- /// Notifies self that the download is being retried.
292- pub ( crate ) fn retrying_download ( & self , url : & str ) {
293- let mut map = self . file_progress_bars . lock ( ) . unwrap ( ) ;
294- let Some ( ( pb, retry_time) ) = map. get_mut ( url) else {
295- return ;
296- } ;
297- * retry_time = Some ( Instant :: now ( ) ) ;
298- pb. set_style ( ProgressStyle :: with_template ( "{msg:>12.bold} retrying download" ) . unwrap ( ) ) ;
329+ fn retrying ( & mut self ) {
330+ self . retry_time = Some ( Instant :: now ( ) ) ;
331+ self . progress
332+ . set_style ( ProgressStyle :: with_template ( "{msg:>12.bold} retrying download" ) . unwrap ( ) ) ;
299333 }
300334}
301335
0 commit comments