Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@genedna
Forked from clux/indicatif-hyper.rs
Created September 18, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genedna/0e3a6fa49bb0fcecdb216fcc0fa3d87d to your computer and use it in GitHub Desktop.
Save genedna/0e3a6fa49bb0fcecdb216fcc0fa3d87d to your computer and use it in GitHub Desktop.
indicatif progress bars with hyper 0.10
pub fn http_download_to_path(url: &str, save: &PathBuf) -> Result<()> {
let client = Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap()));
let mut res = client.get(url).send()?;
if res.status != hyper::Ok {
return Err(Error::SomeError)));
}
let use_progress = true;
if use_progress {
use indicatif::{ProgressBar, ProgressStyle};
let total_size = res.headers.get::<hyper::header::ContentLength>().unwrap().clone().0;
let mut downloaded = 0;
let mut buffer = [0; 1000000];
let mut f = File::create(save)?;
let pb = ProgressBar::new(total_size);
pb.set_style(ProgressStyle::default_bar()
.template("[{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.progress_chars("#>-"));
while downloaded < total_size {
let read = res.read(&mut buffer)?;
f.write(&mut buffer[0..read])?;
downloaded += read as u64;
pb.set_position(downloaded);
}
f.flush()?;
} else {
let mut buffer: Vec<u8> = Vec::new();
res.read_to_end(&mut buffer)?;
let mut f = File::create(save)?;
f.write_all(&buffer)?;
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment