11use indicatif:: { MultiProgress , ProgressBar , ProgressDrawTarget , ProgressStyle } ;
2- use std:: time :: Duration ;
2+ use std:: collections :: HashMap ;
33
44use crate :: dist:: Notification as In ;
55use crate :: notifications:: Notification ;
@@ -13,8 +13,8 @@ use crate::utils::Notification as Un;
1313pub ( crate ) struct DownloadTracker {
1414 /// MultiProgress bar for the downloads.
1515 multi_progress_bars : MultiProgress ,
16- /// ProgressBar for the current download .
17- progress_bar : ProgressBar ,
16+ /// Mapping of URLs being downloaded to their corresponding progress bars .
17+ file_progress_bars : HashMap < String , ProgressBar > ,
1818}
1919
2020impl DownloadTracker {
@@ -28,22 +28,29 @@ impl DownloadTracker {
2828
2929 Self {
3030 multi_progress_bars,
31- progress_bar : ProgressBar :: hidden ( ) ,
31+ file_progress_bars : HashMap :: new ( ) ,
3232 }
3333 }
3434
3535 pub ( crate ) fn handle_notification ( & mut self , n : & Notification < ' _ > ) -> bool {
3636 match * n {
37- Notification :: Install ( In :: Utils ( Un :: DownloadContentLengthReceived ( content_len) ) ) => {
38- self . content_length_received ( content_len) ;
37+ Notification :: Install ( In :: Utils ( Un :: DownloadContentLengthReceived (
38+ content_len,
39+ url,
40+ ) ) ) => {
41+ self . content_length_received ( content_len, url) ;
3942 true
4043 }
41- Notification :: Install ( In :: Utils ( Un :: DownloadDataReceived ( data) ) ) => {
42- self . data_received ( data. len ( ) ) ;
44+ Notification :: Install ( In :: Utils ( Un :: DownloadDataReceived ( data, url ) ) ) => {
45+ self . data_received ( data. len ( ) , url ) ;
4346 true
4447 }
45- Notification :: Install ( In :: Utils ( Un :: DownloadFinished ) ) => {
46- self . download_finished ( ) ;
48+ Notification :: Install ( In :: Utils ( Un :: DownloadFinished ( url) ) ) => {
49+ self . download_finished ( url) ;
50+ true
51+ }
52+ Notification :: Install ( In :: DownloadingComponent ( component, _, _, url) ) => {
53+ self . create_progress_bar ( component. to_string ( ) , url. to_string ( ) ) ;
4754 true
4855 }
4956 Notification :: Install ( In :: Utils ( Un :: DownloadPushUnit ( _) ) ) => true ,
@@ -53,30 +60,44 @@ impl DownloadTracker {
5360 }
5461 }
5562
56- /// Sets the length for a new ProgressBar and gives it a style .
57- pub ( crate ) fn content_length_received ( & mut self , content_len : u64 ) {
58- self . progress_bar . set_length ( content_len ) ;
59- self . progress_bar . set_style (
63+ /// Creates a new ProgressBar for the given component .
64+ pub ( crate ) fn create_progress_bar ( & mut self , component : String , url : String ) {
65+ let pb = ProgressBar :: hidden ( ) ;
66+ pb . set_style (
6067 ProgressStyle :: with_template (
61- "[{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA: {eta})" ,
68+ "{msg:>12.bold} [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA: {eta})" ,
6269 )
6370 . unwrap ( )
6471 . progress_chars ( "## " ) ,
6572 ) ;
73+ pb. set_message ( component) ;
74+ self . multi_progress_bars . add ( pb. clone ( ) ) ;
75+ self . file_progress_bars . insert ( url, pb) ;
76+ }
77+
78+ /// Sets the length for a new ProgressBar and gives it a style.
79+ pub ( crate ) fn content_length_received ( & mut self , content_len : u64 , url : & str ) {
80+ if let Some ( pb) = self . file_progress_bars . get ( url) {
81+ pb. set_length ( content_len) ;
82+ }
6683 }
6784
6885 /// Notifies self that data of size `len` has been received.
69- pub ( crate ) fn data_received ( & mut self , len : usize ) {
70- if self . progress_bar . is_hidden ( ) && self . progress_bar . elapsed ( ) >= Duration :: from_secs ( 1 ) {
71- self . multi_progress_bars . add ( self . progress_bar . clone ( ) ) ;
86+ pub ( crate ) fn data_received ( & mut self , len : usize , url : & str ) {
87+ if let Some ( pb ) = self . file_progress_bars . get ( url ) {
88+ pb . inc ( len as u64 ) ;
7289 }
73- self . progress_bar . inc ( len as u64 ) ;
7490 }
7591
7692 /// Notifies self that the download has finished.
77- pub ( crate ) fn download_finished ( & mut self ) {
78- self . progress_bar . finish_and_clear ( ) ;
79- self . multi_progress_bars . remove ( & self . progress_bar ) ;
80- self . progress_bar = ProgressBar :: hidden ( ) ;
93+ pub ( crate ) fn download_finished ( & mut self , url : & str ) {
94+ let Some ( pb) = self . file_progress_bars . get ( url) else {
95+ return ;
96+ } ;
97+ pb. set_style (
98+ ProgressStyle :: with_template ( "{msg:>12.bold} downloaded {total_bytes} in {elapsed}" )
99+ . unwrap ( ) ,
100+ ) ;
101+ pb. finish ( ) ;
81102 }
82103}
0 commit comments