Commit 0c3769fd authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub
Browse files

Merge pull request #11 from ids1024/pbr

Use pbr crate for progress bar
parents 9b212cd5 5f7ad954
......@@ -19,6 +19,7 @@ serde_derive = "1.0"
tar = { git = "https://github.com/redox-os/tar-rs" }
toml = "0.4"
version-compare = "0.0.4"
pbr = { version = "1.0", git = "https://github.com/ids1024/pb", branch = "redox" }
[dependencies.hyper]
version = "0.10"
......
......@@ -10,6 +10,8 @@ use hyper_rustls::TlsClient;
use hyper::error::Error as HyperError;
use hyper::header::ContentLength;
use pbr::{ProgressBar, Units};
pub fn download(remote_path: &str, local_path: &str) -> io::Result<()> {
let mut stderr = stderr();
......@@ -29,45 +31,17 @@ pub fn download(remote_path: &str, local_path: &str) -> io::Result<()> {
let mut count = 0;
let length = response.headers.get::<ContentLength>().map_or(0, |h| h.0 as usize);
let mut status = [b' '; 50];
let mut file = File::create(&local_path)?;
let mut pb = ProgressBar::new(length as u64);
pb.set_units(Units::Bytes);
loop {
let (percent, cols) = if count >= length {
(100, status.len())
} else {
((100 * count) / length, (status.len() * count) / length)
};
let _ = write!(stderr, "\r* {:>3}% [", percent);
for i in 0..cols {
status[i] = b'=';
}
if cols < status.len() {
status[cols] = b'>';
}
let _ = stderr.write(&status);
let (size, suffix) = if count >= 10 * 1000 * 1000 * 1000 {
(count / (1000 * 1000 * 1000), "GB")
} else if count >= 10 * 1000 * 1000 {
(count / (1000 * 1000), "MB")
} else if count >= 10 * 1000 {
(count / 1000, "KB")
} else {
(count, "B")
};
let _ = write!(stderr, "] {:>4} {}", size, suffix);
let mut buf = [0; 8192];
let res = response.read(&mut buf)?;
if res == 0 {
break;
}
count += file.write(&buf[.. res])?;
pb.set(count as u64);
}
let _ = write!(stderr, "\n");
......
......@@ -8,6 +8,7 @@ extern crate octavo;
extern crate serde_derive;
extern crate tar;
extern crate toml;
extern crate pbr;
use libflate::gzip::Encoder;
use octavo::octavo_digest::Digest;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment