feat(): add progress bar

This commit is contained in:
2024-12-23 13:07:01 +01:00
parent a6d0d34ad1
commit 9abb2f0a65
3 changed files with 74 additions and 3 deletions
Generated
+61
View File
@@ -178,6 +178,19 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "console"
version = "0.15.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea3c6ecd8059b57859df5c69830340ed3c41d30e3da0c1cbed90a96ac853041b"
dependencies = [
"encode_unicode",
"libc",
"once_cell",
"unicode-width",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.4" version = "0.9.4"
@@ -205,6 +218,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "encode_unicode"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.35" version = "0.8.35"
@@ -664,6 +683,19 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "indicatif"
version = "0.17.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf675b85ed934d3c67b5c5469701eec7db22689d0a2139d856e0925fa28b281"
dependencies = [
"console",
"number_prefix",
"portable-atomic",
"unicode-width",
"web-time",
]
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.10.1" version = "2.10.1"
@@ -775,6 +807,12 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "number_prefix"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.7" version = "0.36.7"
@@ -869,6 +907,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"futures", "futures",
"indicatif",
"reqwest", "reqwest",
"serde", "serde",
"tokio", "tokio",
@@ -893,6 +932,12 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "portable-atomic"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.92" version = "1.0.92"
@@ -1364,6 +1409,12 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "unicode-width"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.9.0" version = "0.9.0"
@@ -1510,6 +1561,16 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "web-time"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]] [[package]]
name = "windows-registry" name = "windows-registry"
version = "0.2.0" version = "0.2.0"
+1
View File
@@ -7,6 +7,7 @@ authors = ["David Schütte"]
[dependencies] [dependencies]
clap = { version = "4.5.23", features = ["derive", "std"] } clap = { version = "4.5.23", features = ["derive", "std"] }
futures = "0.3.31" futures = "0.3.31"
indicatif = "0.17.9"
reqwest = { version = "0.12.9", features = ["json", "stream"] } reqwest = { version = "0.12.9", features = ["json", "stream"] }
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
tokio = { version = "1.42.0", default-features = false, features = ["full"] } tokio = { version = "1.42.0", default-features = false, features = ["full"] }
+12 -3
View File
@@ -1,4 +1,5 @@
use futures::TryFutureExt; use futures::TryFutureExt;
use indicatif::ProgressBar;
use reqwest::{Client, Url}; use reqwest::{Client, Url};
use futures::stream::TryStreamExt; use futures::stream::TryStreamExt;
@@ -94,6 +95,7 @@ async fn download_file(
query_url: Url, query_url: Url,
cli: Arc<Cli>, cli: Arc<Cli>,
sem: Arc<Semaphore>, sem: Arc<Semaphore>,
bar: Arc<ProgressBar>,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
let _permit = sem.clone().acquire_owned().await; let _permit = sem.clone().acquire_owned().await;
let mut file = file; let mut file = file;
@@ -115,13 +117,14 @@ async fn download_file(
if let Ok(fd) = fd { if let Ok(fd) = fd {
if let Ok(metadata) = fd.metadata().await { if let Ok(metadata) = fd.metadata().await {
println!("Skipping {}", file.title); // println!("Skipping {}", file.title);
if metadata.len() == selected_download.file_size { if metadata.len() == selected_download.file_size {
bar.inc(1);
return Ok(()); return Ok(());
} }
} }
} }
println!("Downloading {}", file.title); // println!("Downloading {}", file.title);
let mut fd = File::create(&filepath).await?; let mut fd = File::create(&filepath).await?;
let mut reader = StreamReader::new( let mut reader = StreamReader::new(
resp.bytes_stream() resp.bytes_stream()
@@ -129,6 +132,8 @@ async fn download_file(
); );
copy(&mut reader, &mut fd).await?; copy(&mut reader, &mut fd).await?;
bar.inc(1);
Ok(()) Ok(())
} }
@@ -229,7 +234,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let downloads = all_files; let downloads = all_files;
let len = downloads.len(); let len = downloads.len();
println!("Total files: {}", len); let bar = Arc::new(ProgressBar::new(len as u64));
let mut handles = Vec::with_capacity(len); let mut handles = Vec::with_capacity(len);
let sem = Arc::new(Semaphore::new(10)); let sem = Arc::new(Semaphore::new(10));
@@ -238,6 +243,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let client = client.clone(); let client = client.clone();
let cli = cli.clone(); let cli = cli.clone();
let sem = sem.clone(); let sem = sem.clone();
let bar = bar.clone();
let job = tokio::spawn(download_file( let job = tokio::spawn(download_file(
file, file,
@@ -246,6 +252,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
query_url.clone(), query_url.clone(),
cli, cli,
sem, sem,
bar,
)); ));
handles.push(job); handles.push(job);
} }
@@ -254,5 +261,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
job.await; job.await;
} }
bar.finish();
Ok(()) Ok(())
} }