From 9abcfaa635a0b096e6cec0299749276ca13dfe8a Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Mon, 24 Oct 2022 22:03:20 +0300 Subject: [PATCH] feat: add progress for DHT update --- iroh/src/run.rs | 27 +++++++++++++++++++++------ iroh/tests/cmd/add_directory.trycmd | 5 +++-- iroh/tests/cmd/add_file.trycmd | 5 +++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/iroh/src/run.rs b/iroh/src/run.rs index d7daa3a5e7..f553947908 100644 --- a/iroh/src/run.rs +++ b/iroh/src/run.rs @@ -162,7 +162,7 @@ async fn add(api: &impl Api, path: &Path, no_wrap: bool, recursive: bool) -> Res path.display() ); } - println!("{} Calculating size...", style("[1/2]").bold().dim()); + println!("{} Calculating size...", style("[1/3]").bold().dim()); let pb = ProgressBar::new_spinner(); let mut total_size: u64 = 0; @@ -184,7 +184,7 @@ async fn add(api: &impl Api, path: &Path, no_wrap: bool, recursive: bool) -> Res println!( "{} Importing content {}...", - style("[2/2]").bold().dim(), + style("[2/3]").bold().dim(), human::format_bytes(total_size) ); @@ -197,11 +197,11 @@ async fn add(api: &impl Api, path: &Path, no_wrap: bool, recursive: bool) -> Res pb.inc(0); let mut progress = api.add_stream(path, !no_wrap).await?; - let mut root = None; + let mut cids = Vec::new(); while let Some(add_event) = progress.next().await { match add_event? { AddEvent::ProgressDelta { cid, size } => { - root = Some(cid); + cids.push(cid); if let Some(size) = size { pb.inc(size); } @@ -209,9 +209,24 @@ async fn add(api: &impl Api, path: &Path, no_wrap: bool, recursive: bool) -> Res } } pb.finish_and_clear(); - let root = root.context("File processing failed")?; + + let pb = ProgressBar::new(cids.len().try_into().unwrap()); + let root = *cids.last().context("File processing failed")?; + // remove everything but the root + cids.splice(0..cids.len() - 1, []); + println!( + "{} Providing {} records to DHT ...", + style("[3/3]").bold().dim(), + cids.len(), + ); + pb.set_style(ProgressStyle::with_template("[{elapsed_precise}] {wide_bar} {pos}/{len} ({per_sec}) {msg}").unwrap()); + pb.inc(0); + for cid in cids { + api.provide(cid).await?; + pb.inc(1); + } + pb.finish_and_clear(); println!("/ipfs/{}", root); - api.provide(root).await?; Ok(()) } diff --git a/iroh/tests/cmd/add_directory.trycmd b/iroh/tests/cmd/add_directory.trycmd index 086daf0339..7112310549 100644 --- a/iroh/tests/cmd/add_directory.trycmd +++ b/iroh/tests/cmd/add_directory.trycmd @@ -1,7 +1,8 @@ ``` $ iroh add -r mydir -[1/2] Calculating size... -[2/2] Importing content 5 B... +[1/3] Calculating size... +[2/3] Importing content 5 B... +[3/3] Providing 1 records to DHT ... /ipfs/QmYbcW4tXLXHWw753boCK8Y7uxLu5abXjyYizhLznq9PUR ``` \ No newline at end of file diff --git a/iroh/tests/cmd/add_file.trycmd b/iroh/tests/cmd/add_file.trycmd index 6e0e8c3fa6..1d5fe3c4b4 100644 --- a/iroh/tests/cmd/add_file.trycmd +++ b/iroh/tests/cmd/add_file.trycmd @@ -1,7 +1,8 @@ ``` $ iroh add file.txt -[1/2] Calculating size... -[2/2] Importing content 20 B... +[1/3] Calculating size... +[2/3] Importing content 20 B... +[3/3] Providing 1 records to DHT ... /ipfs/QmYbcW4tXLXHWw753boCK8Y7uxLu5abXjyYizhLznq9PUR ``` \ No newline at end of file