Add download progress bar
This commit is contained in:
parent
2f396b5e12
commit
9a91ac5510
|
@ -223,9 +223,20 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.17"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88d1c26957f23603395cd326b0ffe64124b818f4449552f960d815cfba83a53d"
|
||||
checksum = "629316e42fe7c2a0b9a65b47d159ceaa5453ab14e8f0a3c5eedbb8cd55b4a445"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a89f17b21645bc4ed773c69af9c9a0effd4a3f1a3876eadd453469f8854e7fdd"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
|
@ -235,21 +246,22 @@ checksum = "36ea153c13024fe480590b3e3d4cad89a0cfacecc24577b68f86c6ced9c2bc11"
|
|||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.17"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d3d00f4eddb73e498a54394f228cd55853bdf059259e8e7bc6e69d408892e99"
|
||||
checksum = "dabf1872aaab32c886832f2276d2f5399887e2bd613698a02359e4ea83f8de12"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.17"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481"
|
||||
checksum = "41d22213122356472061ac0f1ab2cee28d2bac8491410fd68c2af53d1cedb83e"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"futures-core",
|
||||
"futures-macro",
|
||||
"futures-task",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -314,6 +326,8 @@ dependencies = [
|
|||
"confy",
|
||||
"console",
|
||||
"dialoguer",
|
||||
"futures-util",
|
||||
"indicatif",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -413,6 +427,18 @@ dependencies = [
|
|||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.16.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d207dc617c7a380ab07ff572a6e52fa202a2a8f355860ac9c38e23f8196be1b"
|
||||
dependencies = [
|
||||
"console",
|
||||
"lazy_static",
|
||||
"number_prefix",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.12"
|
||||
|
@ -550,6 +576,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "number_prefix"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.8.0"
|
||||
|
|
|
@ -9,7 +9,9 @@ anyhow = "1.0"
|
|||
confy = "0.4"
|
||||
console = "0.15.0"
|
||||
dialoguer = "0.9.0"
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
futures-util = "0.3.18"
|
||||
indicatif = "0.16.2"
|
||||
reqwest = { version = "0.11", features = ["json", "stream"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
structopt = "0.3"
|
||||
|
|
43
src/main.rs
43
src/main.rs
|
@ -1,6 +1,9 @@
|
|||
use console::style;
|
||||
use futures_util::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::min;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
|
@ -277,10 +280,10 @@ async fn fetch_mod_version(config: &Config, version_id: &String) -> anyhow::Resu
|
|||
}
|
||||
|
||||
async fn download_version_file(_config: &Config, file: &ModVersionFile) -> anyhow::Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
let response = client.get(&file.url).send().await?;
|
||||
// TODO replace all uses of .unwrap() with proper error codes
|
||||
let filename = &file.filename;
|
||||
|
||||
// TODO make confirmation skippable with flag argument
|
||||
use dialoguer::Confirm;
|
||||
let prompt = format!("Download to {}?", filename);
|
||||
let confirm = Confirm::new()
|
||||
|
@ -292,13 +295,39 @@ async fn download_version_file(_config: &Config, file: &ModVersionFile) -> anyho
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
// TODO stream from socket to cache with response.bytes_stream()
|
||||
// TODO check hashes while streaming
|
||||
|
||||
let mut file = std::fs::File::create(&file.filename)?;
|
||||
let mut content = std::io::Cursor::new(response.bytes().await?);
|
||||
std::io::copy(&mut content, &mut file)?;
|
||||
println!("done downloading.");
|
||||
let client = reqwest::Client::new();
|
||||
let url = &file.url;
|
||||
let response = client.get(url).send().await?;
|
||||
let total_size = response.content_length().unwrap();
|
||||
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
let pb = ProgressBar::new(total_size);
|
||||
pb.set_style(ProgressStyle::default_bar().template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})").progress_chars("#>-"));
|
||||
|
||||
// TODO find a way to into -> impl Into<Cow<'static, str>>
|
||||
// let message = format!("Downloading {}", url);
|
||||
let message = "Downloading file";
|
||||
pb.set_message(message);
|
||||
|
||||
let filename = &file.filename;
|
||||
let mut file = std::fs::File::create(filename)?;
|
||||
let mut downloaded: u64 = 0;
|
||||
let mut stream = response.bytes_stream();
|
||||
|
||||
while let Some(item) = stream.next().await {
|
||||
let chunk = &item.unwrap();
|
||||
file.write(&chunk)?;
|
||||
let new = min(downloaded + (chunk.len() as u64), total_size);
|
||||
downloaded = new;
|
||||
pb.set_position(new);
|
||||
}
|
||||
|
||||
// TODO find a way to into -> impl Into<Cow<'static, str>>
|
||||
// let message = format!("Downloaded {} to {}", url, filename);
|
||||
let message = "Download complete";
|
||||
pb.finish_with_message(message);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue