refactor: make a with_progressbar function

This commit is contained in:
RafayAhmad7548 2026-08-31 00:08:48 +05:00
parent 0a12b87a2a
commit 2aaae9239c
Signed by: RafayAhmad
SSH key fingerprint: SHA256:WURX8viobA1uawb4dWM3LqYrY+XPcZcXhAXAlrYdhtE
2 changed files with 36 additions and 18 deletions

View file

@ -1,14 +1,13 @@
mod models; mod models;
mod utils; mod utils;
use crate::{models::vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, utils::with_spinner}; use crate::{models::vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, utils::{with_progressbar, with_spinner}};
use clap::{ use clap::{
Parser, Subcommand, Parser, Subcommand,
builder::styling::{self}, builder::styling::{self},
}; };
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme}; use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
use futures_util::StreamExt; use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
use std::error::Error; use std::error::Error;
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
@ -79,25 +78,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
.json() .json()
.await?; .await?;
println!( let start_msg = format!(
"downloading server.jar for {} {}", "downloading server.jar for {} {}",
loaders[loader_index], release_version_names[version_index] loaders[loader_index], release_version_names[version_index]
); );
let pb = ProgressBar::new(version_detail.downloads.server.size); with_progressbar(&start_msg, "download finished", version_detail.downloads.server.size, |pb| async move {
pb.set_style(ProgressStyle::default_bar().template( let server_bytes = reqwest::get(version_detail.downloads.server.url).await?;
"{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})", let mut file = tokio::fs::File::create("server.jar").await?;
)?); let mut stream = server_bytes.bytes_stream();
let server_bytes = reqwest::get(version_detail.downloads.server.url).await?; while let Some(chunk) = stream.next().await {
let mut file = tokio::fs::File::create("server.jar").await?; let chunk = chunk?;
let mut stream = server_bytes.bytes_stream(); file.write_all(&chunk).await?;
pb.inc(chunk.len() as u64);
while let Some(chunk) = stream.next().await { }
let chunk = chunk?; Ok(())
file.write_all(&chunk).await?; }).await?;
pb.inc(chunk.len() as u64);
}
pb.finish_with_message("download finished");
with_spinner("verifying sha1 hash", "sha1 hash verified", || async { with_spinner("verifying sha1 hash", "sha1 hash verified", || async {
let data = tokio::fs::read("server.jar").await?; let data = tokio::fs::read("server.jar").await?;

View file

@ -1,5 +1,5 @@
use std::{error::Error, future::Future}; use std::{error::Error, future::Future};
use indicatif::ProgressBar; use indicatif::{ProgressBar, ProgressStyle};
pub async fn with_spinner<F, Fut, T>( pub async fn with_spinner<F, Fut, T>(
start_msg: &str, start_msg: &str,
@ -21,3 +21,25 @@ where
result result
} }
pub async fn with_progressbar<F, Fut, T>(
start_msg: &str,
finish_msg: &str,
len: u64,
f: F,
) -> Result<T, Box<dyn Error>>
where
F: FnOnce(ProgressBar) -> Fut,
Fut: Future<Output = Result<T, Box<dyn Error>>>
{
let pb = ProgressBar::new(len);
pb.set_style(ProgressStyle::default_bar().template(
"{msg}\n{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})",
)?);
pb.set_message(start_msg.to_string());
let result = f(pb.clone()).await;
pb.finish_with_message(finish_msg.to_string());
result
}