1- use std:: collections:: BTreeMap ;
21use std:: str:: FromStr ;
32use std:: { net:: SocketAddr , path:: PathBuf , time:: Duration } ;
43
@@ -11,7 +10,8 @@ use console::style;
1110use futures:: { Stream , StreamExt } ;
1211use human_time:: ToHumanTimeString ;
1312use indicatif:: {
14- HumanBytes , HumanDuration , ProgressBar , ProgressDrawTarget , ProgressState , ProgressStyle ,
13+ HumanBytes , HumanDuration , MultiProgress , ProgressBar , ProgressDrawTarget , ProgressState ,
14+ ProgressStyle ,
1515} ;
1616use iroh:: client:: quic:: Iroh ;
1717use iroh:: dial:: Ticket ;
@@ -727,15 +727,24 @@ fn fmt_latency(latency: Option<Duration>) -> String {
727727 }
728728}
729729
730- const PROGRESS_STYLE : & str =
731- "{msg}\n {spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})" ;
730+ fn make_overall_progress ( ) -> ProgressBar {
731+ let pb = ProgressBar :: hidden ( ) ;
732+ pb. enable_steady_tick ( std:: time:: Duration :: from_millis ( 100 ) ) ;
733+ pb. set_style (
734+ ProgressStyle :: with_template (
735+ "{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len}" ,
736+ )
737+ . unwrap ( )
738+ . progress_chars ( "#>-" ) ,
739+ ) ;
740+ pb
741+ }
732742
733- fn make_download_pb ( ) -> ProgressBar {
743+ fn make_individual_progress ( ) -> ProgressBar {
734744 let pb = ProgressBar :: hidden ( ) ;
735- pb. set_draw_target ( ProgressDrawTarget :: stderr ( ) ) ;
736- pb. enable_steady_tick ( std:: time:: Duration :: from_millis ( 50 ) ) ;
745+ pb. enable_steady_tick ( std:: time:: Duration :: from_millis ( 100 ) ) ;
737746 pb. set_style (
738- ProgressStyle :: with_template ( PROGRESS_STYLE )
747+ ProgressStyle :: with_template ( "{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})" )
739748 . unwrap ( )
740749 . with_key (
741750 "eta" ,
@@ -750,75 +759,59 @@ fn make_download_pb() -> ProgressBar {
750759
751760pub async fn show_download_progress (
752761 hash : Hash ,
753- mut stream : impl Stream < Item = Result < GetProgress > > + Unpin ,
762+ mut stream : impl Stream < Item = Result < DownloadProgress > > + Unpin ,
754763) -> Result < ( ) > {
755764 eprintln ! ( "Fetching: {}" , hash) ;
756- let pb = make_download_pb ( ) ;
757- pb. set_message ( format ! ( "{} Connecting ..." , style( "[1/3]" ) . bold( ) . dim( ) ) ) ;
758- let mut sizes = BTreeMap :: new ( ) ;
759- let mut downloading = false ;
765+ let mp = MultiProgress :: new ( ) ;
766+ mp. set_draw_target ( ProgressDrawTarget :: stderr ( ) ) ;
767+ let op = mp. add ( make_overall_progress ( ) ) ;
768+ let ip = mp. add ( make_individual_progress ( ) ) ;
769+ op. set_message ( format ! ( "{} Connecting ...\n " , style( "[1/3]" ) . bold( ) . dim( ) ) ) ;
770+ let mut seq = false ;
760771 while let Some ( x) = stream. next ( ) . await {
761772 match x? {
762- GetProgress :: Connected => {
763- pb . set_message ( format ! ( "{} Requesting ..." , style( "[2/3]" ) . bold( ) . dim( ) ) ) ;
773+ DownloadProgress :: Connected => {
774+ op . set_message ( format ! ( "{} Requesting ...\n " , style( "[2/3]" ) . bold( ) . dim( ) ) ) ;
764775 }
765- GetProgress :: FoundCollection {
766- total_blobs_size,
767- num_blobs,
768- ..
769- } => {
770- let count = num_blobs. unwrap_or_default ( ) ;
771- let missing_bytes = total_blobs_size. unwrap_or_default ( ) ;
772- pb. set_message ( format ! (
773- "{} Downloading {} file(s) with total transfer size {}" ,
776+ DownloadProgress :: FoundHashSeq { children, .. } => {
777+ op. set_message ( format ! (
778+ "{} Downloading {} blob(s)\n " ,
774779 style( "[3/3]" ) . bold( ) . dim( ) ,
775- count,
776- HumanBytes ( missing_bytes) ,
780+ children + 1 ,
777781 ) ) ;
778- pb . set_length ( missing_bytes ) ;
779- pb . reset ( ) ;
780- downloading = true ;
782+ op . set_length ( children + 1 ) ;
783+ op . reset ( ) ;
784+ seq = true ;
781785 }
782- GetProgress :: Found { id, size, .. } => {
783- if !downloading {
784- pb. set_message ( format ! (
785- "{} Downloading blob with size {}" ,
786- style( "[3/3]" ) . bold( ) . dim( ) ,
787- size,
788- ) ) ;
789- pb. set_length ( size) ;
790- pb. reset ( ) ;
786+ DownloadProgress :: Found { size, child, .. } => {
787+ if seq {
788+ op. set_position ( child) ;
789+ } else {
790+ op. finish_and_clear ( ) ;
791791 }
792- sizes. insert ( id, ( size, 0 ) ) ;
792+ ip. set_length ( size) ;
793+ ip. reset ( ) ;
793794 }
794- GetProgress :: Progress { id, offset } => {
795- if let Some ( ( _, current) ) = sizes. get_mut ( & id) {
796- * current = offset;
797- let total = sizes. values ( ) . map ( |( _, current) | current) . sum :: < u64 > ( ) ;
798- pb. set_position ( total) ;
799- }
795+ DownloadProgress :: Progress { offset, .. } => {
796+ ip. set_position ( offset) ;
800797 }
801- GetProgress :: Done { id } => {
802- if let Some ( ( size, current) ) = sizes. get_mut ( & id) {
803- * current = * size;
804- let total = sizes. values ( ) . map ( |( _, current) | current) . sum :: < u64 > ( ) ;
805- pb. set_position ( total) ;
806- }
798+ DownloadProgress :: Done { .. } => {
799+ ip. finish_and_clear ( ) ;
807800 }
808- GetProgress :: NetworkDone {
801+ DownloadProgress :: NetworkDone {
809802 bytes_read,
810803 elapsed,
811804 ..
812805 } => {
813- pb . finish_and_clear ( ) ;
806+ op . finish_and_clear ( ) ;
814807 eprintln ! (
815808 "Transferred {} in {}, {}/s" ,
816809 HumanBytes ( bytes_read) ,
817810 HumanDuration ( elapsed) ,
818811 HumanBytes ( ( bytes_read as f64 / elapsed. as_secs_f64( ) ) as u64 )
819812 ) ;
820813 }
821- GetProgress :: AllDone => {
814+ DownloadProgress :: AllDone => {
822815 break ;
823816 }
824817 _ => { }
0 commit comments