Skip to content

Commit

Permalink
feat: add progress for DHT update
Browse files Browse the repository at this point in the history
  • Loading branch information
rklaehn committed Oct 24, 2022
1 parent 67ee33d commit 8d33ddb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
29 changes: 23 additions & 6 deletions iroh/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -162,7 +163,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;
Expand All @@ -184,7 +185,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)
);

Expand All @@ -197,21 +198,37 @@ 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);
}
}
}
}
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);
tokio::time::sleep(Duration::from_secs(1)).await;
for cid in cids {
api.provide(cid).await?;
pb.inc(1);
}
pb.finish_and_clear();
println!("/ipfs/{}", root);
api.provide(root).await?;

Ok(())
}
5 changes: 3 additions & 2 deletions iroh/tests/cmd/add_directory.trycmd
Original file line number Diff line number Diff line change
@@ -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

```
5 changes: 3 additions & 2 deletions iroh/tests/cmd/add_file.trycmd
Original file line number Diff line number Diff line change
@@ -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

```

0 comments on commit 8d33ddb

Please sign in to comment.