From 2aaae9239cdfeccd0cd0a7352159241476112c2f Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Mon, 31 Aug 2026 00:08:48 +0500 Subject: [PATCH] refactor: make a with_progressbar function --- src/main.rs | 30 +++++++++++++----------------- src/utils.rs | 24 +++++++++++++++++++++++- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5f7d7ee..4390fef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ mod models; 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::{ Parser, Subcommand, builder::styling::{self}, }; use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme}; use futures_util::StreamExt; -use indicatif::{ProgressBar, ProgressStyle}; use sha1::{Digest, Sha1}; use std::error::Error; use tokio::io::AsyncWriteExt; @@ -79,25 +78,22 @@ async fn main() -> Result<(), Box> { .json() .await?; - println!( + let start_msg = format!( "downloading server.jar for {} {}", loaders[loader_index], release_version_names[version_index] ); - let pb = ProgressBar::new(version_detail.downloads.server.size); - pb.set_style(ProgressStyle::default_bar().template( - "{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})", - )?); + with_progressbar(&start_msg, "download finished", version_detail.downloads.server.size, |pb| async move { + let server_bytes = reqwest::get(version_detail.downloads.server.url).await?; + 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?; - let mut file = tokio::fs::File::create("server.jar").await?; - let mut stream = server_bytes.bytes_stream(); - - while let Some(chunk) = stream.next().await { - let chunk = chunk?; - file.write_all(&chunk).await?; - pb.inc(chunk.len() as u64); - } - pb.finish_with_message("download finished"); + while let Some(chunk) = stream.next().await { + let chunk = chunk?; + file.write_all(&chunk).await?; + pb.inc(chunk.len() as u64); + } + Ok(()) + }).await?; with_spinner("verifying sha1 hash", "sha1 hash verified", || async { let data = tokio::fs::read("server.jar").await?; diff --git a/src/utils.rs b/src/utils.rs index 9e5ba69..b0bc56e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ use std::{error::Error, future::Future}; -use indicatif::ProgressBar; +use indicatif::{ProgressBar, ProgressStyle}; pub async fn with_spinner( start_msg: &str, @@ -21,3 +21,25 @@ where result } + +pub async fn with_progressbar( + start_msg: &str, + finish_msg: &str, + len: u64, + f: F, +) -> Result> +where + F: FnOnce(ProgressBar) -> Fut, + Fut: Future>> +{ + 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 +}