From 89e61a0da2cf5b7469bd4ddfb75c2ddd8c82248e Mon Sep 17 00:00:00 2001 From: TOwInOK <60252419+TOwInOK@users.noreply.github.com> Date: Thu, 2 May 2024 17:14:55 +0700 Subject: [PATCH] now using prefix in pb. --- settings.toml | 3 ++- src/lock/core.rs | 2 +- src/models/cores/paper.rs | 2 +- src/models/cores/purpur.rs | 4 ++-- src/models/cores/vanilla.rs | 7 +++++-- src/settings/core.rs | 26 ++++++++++++-------------- src/settings/extensions/plugins.rs | 22 +++++++++------------- src/tr/download.rs | 2 +- 8 files changed, 33 insertions(+), 35 deletions(-) diff --git a/settings.toml b/settings.toml index 9a636a7..f855c18 100644 --- a/settings.toml +++ b/settings.toml @@ -1,8 +1,9 @@ [core] Provider = "paper" Version = "1.20.6" +ForceUpdate = true [plugins.simple-voice-chat] -force_update = false +force_update = true [plugins.chunky] force_update = false diff --git a/src/lock/core.rs b/src/lock/core.rs index af6375c..01c9201 100644 --- a/src/lock/core.rs +++ b/src/lock/core.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::settings::core::{Core, Provider}; +use crate::settings::core::Provider; #[derive(Deserialize, Serialize, Debug, Default, PartialEq, Clone)] pub struct CoreMeta { diff --git a/src/models/cores/paper.rs b/src/models/cores/paper.rs index 068f4de..0334d2d 100644 --- a/src/models/cores/paper.rs +++ b/src/models/cores/paper.rs @@ -1,4 +1,4 @@ -use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle}; +use indicatif::ProgressBar; use serde::{Deserialize, Serialize}; use crate::{ diff --git a/src/models/cores/purpur.rs b/src/models/cores/purpur.rs index a36c69e..9c375da 100644 --- a/src/models/cores/purpur.rs +++ b/src/models/cores/purpur.rs @@ -1,4 +1,4 @@ -use indicatif::{MultiProgress, ProgressBar}; +use indicatif::ProgressBar; use serde::{Deserialize, Serialize}; use crate::{ @@ -43,7 +43,7 @@ impl ModelCore for Purpur { type Version = String; //find build and push link - async fn get_link(core: &Core, pb: &ProgressBar) -> Result<(String, ChooseHash, String)> { + async fn get_link(core: &Core, _pb: &ProgressBar) -> Result<(String, ChooseHash, String)> { let build = core.build(); let version = core.version(); let version = find_version(version).await?; diff --git a/src/models/cores/vanilla.rs b/src/models/cores/vanilla.rs index 2533608..897a08e 100644 --- a/src/models/cores/vanilla.rs +++ b/src/models/cores/vanilla.rs @@ -4,7 +4,7 @@ use crate::{ settings::core::Core, tr::{hash::ChooseHash, model::core::ModelCore}, }; -use indicatif::{MultiProgress, ProgressBar}; +use indicatif::ProgressBar; use serde::Deserialize; use serde::Serialize; @@ -69,7 +69,10 @@ impl ModelCore for Vanilla { type Link = OuterLink; type Version = VersionID; /// Making request to mojang api and find the link to download minecraft.jar - async fn get_link(core: &Core, pb: &ProgressBar) -> Result<(OuterLink, ChooseHash, VersionID)> { + async fn get_link( + core: &Core, + _pb: &ProgressBar, + ) -> Result<(OuterLink, ChooseHash, VersionID)> { let version = core.version(); // debug!("Start find fn with version: {:#?}", version); let link = find_version(version).await?; diff --git a/src/settings/core.rs b/src/settings/core.rs index 985ad62..b3d6f14 100644 --- a/src/settings/core.rs +++ b/src/settings/core.rs @@ -1,8 +1,7 @@ use std::sync::Arc; -use console::Term; -use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle}; -use log::{debug, info}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use log::debug; use serde::{Deserialize, Serialize}; use tokio::sync::Mutex; @@ -97,17 +96,15 @@ impl Core { ) -> Result<()> { let mpb = mpb.lock().await; - let name = self.provider.as_str().to_string(); - // PB style, init - let name_closure = move |_: &ProgressState, f: &mut dyn std::fmt::Write| { - f.write_str(&name).unwrap(); - }; let pb = mpb.add(ProgressBar::new_spinner()); pb.set_style( - ProgressStyle::with_template("Package:: {name:.blue} >>> {msg:.blue}") - .unwrap() - .with_key("name", name_closure), + ProgressStyle::with_template( + "Package:: {prefix:.blue} >>>{spinner:.green} {msg:.blue} > eta: {eta:.blue}", + ) + .unwrap(), ); + pb.set_prefix(self.provider.as_str()); + // Check meta let (link, hash, build) = self.get_link(&pb).await?; let mut lock = lock.lock().await; @@ -119,12 +116,13 @@ impl Core { return Ok(()); } } - pb.set_message("Downloading..."); let file = Core::get_file(link, hash, &pb).await?; - pb.set_message("Start download"); + pb.set_message("Saving file"); Core::save_bytes(file, self.provider().as_str()).await?; *lock.core_mut() = self.clone().to_meta(build); - lock.save().await + let res = lock.save().await; + pb.finish_with_message("Done"); + res } async fn get_link(&self, pb: &ProgressBar) -> Result<(String, ChooseHash, String)> { match self.provider { diff --git a/src/settings/extensions/plugins.rs b/src/settings/extensions/plugins.rs index 237705f..1409edb 100644 --- a/src/settings/extensions/plugins.rs +++ b/src/settings/extensions/plugins.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use futures_util::future::join_all; -use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use serde::{Deserialize, Serialize}; use tokio::sync::Mutex; @@ -13,7 +13,7 @@ use crate::tr::{download::Download, save::Save}; use super::plugin::Plugin; -const PATH: &'static str = "./plugins/"; +const PATH: &str = "./plugins/"; #[derive(Deserialize, Serialize, Debug, Default, PartialEq)] pub struct Plugins(HashMap); @@ -42,20 +42,16 @@ impl Plugins { for (name, plugin) in self.0.clone() { // Get link let (link, hash, build) = plugin.get_link(&name, game_version).await?; - - // Init name for PB - let cloned_name = name.clone(); - let name_closure = move |_: &ProgressState, f: &mut dyn std::fmt::Write| { - f.write_str(&cloned_name.clone()).unwrap(); - }; // PB init let pb = mpb.lock().await.add(ProgressBar::new_spinner()); // PB style pb.set_style( - ProgressStyle::with_template("Package:: {name:.blue} >>> {msg:.blue}") - .unwrap() - .with_key("name", name_closure), + ProgressStyle::with_template( + "Package:: {prefix:.blue} >>>{spinner:.green} {msg:.blue} > eta: {eta:.blue}", + ) + .unwrap(), ); + pb.set_prefix(name.clone()); // Check meta if let Some(plugin_meta) = lock.lock().await.plugins().get(&name) { let local_build = plugin_meta.build(); @@ -87,9 +83,9 @@ impl Plugins { lock.plugins_mut().insert(name.to_string(), { ExtensionMeta::new(build, format!("{}{}.jar", PATH, name)) }); - let res = lock.save().await?; + lock.save().await?; pb.finish_with_message("Done"); - Ok(res) + Ok(()) })); } join_all(handler_list).await; diff --git a/src/tr/download.rs b/src/tr/download.rs index 7a2b135..62da056 100644 --- a/src/tr/download.rs +++ b/src/tr/download.rs @@ -1,7 +1,7 @@ use crate::errors::error::Result; use bytes::{Bytes, BytesMut}; use futures_util::StreamExt; -use indicatif::ProgressBar; +use indicatif::{ProgressBar, ProgressStyle}; use super::hash::ChooseHash; pub trait Download {