-
-
Save genedna/0e3a6fa49bb0fcecdb216fcc0fa3d87d to your computer and use it in GitHub Desktop.
indicatif progress bars with hyper 0.10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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