Skip to content

Commit

Permalink
feat: Implement download file progress bar (#6018)
Browse files Browse the repository at this point in the history
Fixes #4617 

This PR introduces a download progress bar using [indicatif](https://github.com/console-rs/indicatif). I used the default download style they showcase in one of their [examples](https://github.com/console-rs/indicatif/blob/main/examples/download.rs) because I think it fits pretty well to the existing color scheme here. But let me know if you prefer something to be restyled. The gif below shows what it looks like (with middle cut out for the sake of brevity):

![progress-bar](https://user-images.githubusercontent.com/7879134/148504443-1205ee4c-ccec-435b-868b-ebe6a0e6c9b6.gif)
  • Loading branch information
itegulov authored Jan 17, 2022
1 parent 7312b5d commit b133248
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nearcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ near-rust-allocator-proxy = { version = "0.3.0", optional = true }
lazy-static-include = "3"
tempfile = "3"
anyhow = "1.0.51"
indicatif = "0.15.0"

near-crypto = { path = "../core/crypto" }
near-primitives = { path = "../core/primitives" }
Expand Down
19 changes: 19 additions & 0 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::time::Duration;

use anyhow::{anyhow, bail, Context};
use hyper::body::HttpBody;
use indicatif::{ProgressBar, ProgressStyle};
use near_primitives::time::Clock;
use num_rational::Rational;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -1165,12 +1166,30 @@ async fn download_file_impl(
let https_connector = hyper_tls::HttpsConnector::new();
let client = hyper::Client::builder().build::<_, hyper::Body>(https_connector);
let mut resp = client.get(uri).await.map_err(FileDownloadError::HttpError)?;
let bar = if let Some(file_size) = resp.size_hint().upper() {
let bar = ProgressBar::new(file_size);
bar.set_style(
ProgressStyle::default_bar().template(
"{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} [{bytes_per_sec}] ({eta})"
).progress_chars("#>-")
);
bar
} else {
let bar = ProgressBar::new_spinner();
bar.set_style(
ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] {bytes} [{bytes_per_sec}]"),
);
bar
};
while let Some(next_chunk_result) = resp.data().await {
let next_chunk = next_chunk_result.map_err(FileDownloadError::HttpError)?;
file.write_all(next_chunk.as_ref())
.await
.map_err(|e| FileDownloadError::WriteError(path.to_path_buf(), e))?;
bar.inc(next_chunk.len() as u64);
}
bar.finish();
Ok(())
}

Expand Down

0 comments on commit b133248

Please sign in to comment.