From 2d7e0a2fbf1c8a4187e2bf3fdcd592631ab273a0 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 26 May 2023 17:40:27 +0200 Subject: added full progress? cargo fmt --- src/commands/update.rs | 95 +++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 35 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index 194bbe5..3aae002 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,4 +1,4 @@ -use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ config::Cfg, @@ -19,11 +19,14 @@ pub async fn update( direct_download: bool, delete_old: bool, ) -> MLE<()> { - let mp = MultiProgress::new(); let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); - update_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); + update_p.set_style( + ProgressStyle::with_template(STYLE_BAR_POS) + .unwrap() + .progress_chars(PROGRESS_CHARS), + ); for current_list in liststack { update_p.set_message(format!("Update {}", current_list.id)); @@ -35,7 +38,11 @@ pub async fn update( let mods = userlist_get_all_ids(config, ¤t_list.id)?; let list_u_p = mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into().unwrap())); - list_u_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); + list_u_p.set_style( + ProgressStyle::with_template(STYLE_BAR_POS) + .unwrap() + .progress_chars(PROGRESS_CHARS), + ); let mut current_versions: Vec<(String, String)> = vec![]; let mut updatestack: Vec = vec![]; @@ -43,7 +50,7 @@ pub async fn update( for id in mods { let info = mods_get_info(config, &id)?; list_u_p.set_message(format!("Update {}", info.title)); - + //Skip check if version is set if userlist_get_set_version(config, ¤t_list.id, &id)? { list_u_p.inc(1); @@ -51,19 +58,10 @@ pub async fn update( } //Getting current installed version for disable or delete - let disable_version = - userlist_get_current_version(config, ¤t_list.id, &id)?; + let disable_version = userlist_get_current_version(config, ¤t_list.id, &id)?; updatestack.push( - match specific_update( - config, - clean, - current_list.clone(), - &id, - &list_u_p - ) - .await - { + match specific_update(config, clean, current_list.clone(), &id, &list_u_p).await { Ok(ver) => { current_versions.push((disable_version, id)); ver @@ -89,17 +87,31 @@ pub async fn update( }; if direct_download && !updatestack.is_empty() { - download_versions(current_list.clone(), config.clone(), updatestack, &mp, &list_p).await?; + download_versions( + current_list.clone(), + config.clone(), + updatestack, + &mp, + &list_p, + ) + .await?; //Disable old versions if !clean { - let d_p = mp.insert_before(&list_p, ProgressBar::new(current_versions.len().try_into().unwrap())); - d_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); + let d_p = mp.insert_before( + &list_p, + ProgressBar::new(current_versions.len().try_into().unwrap()), + ); + d_p.set_style( + ProgressStyle::with_template(STYLE_BAR_POS) + .unwrap() + .progress_chars(PROGRESS_CHARS), + ); for ver in current_versions { if delete_old { d_p.set_message(format!("Delete version {}", ver.0)); d_p.inc(1); - delete_version(current_list.clone(), ver.0)?; + delete_version(¤t_list, ver.0)?; } else if ver.0 != "NONE" { d_p.set_message(format!("Disable version {}", ver.0)); d_p.inc(1); @@ -125,9 +137,14 @@ pub async fn update( Ok(()) } -async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progress: &ProgressBar) -> MLE { - let applicable_versions = - versions(&config.apis.modrinth, String::from(id), list.clone()).await; +async fn specific_update( + config: &Cfg, + clean: bool, + list: List, + id: &str, + progress: &ProgressBar, +) -> MLE { + let applicable_versions = versions(&config.apis.modrinth, String::from(id), list.clone()).await; let mut versions: Vec = vec![]; @@ -142,15 +159,16 @@ async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progre let mut current: Vec = vec![]; if clean || (versions.join("|") - != userlist_get_applicable_versions( - config, - String::from(&list.id), - String::from(id), - )?) + != userlist_get_applicable_versions(config, String::from(&list.id), String::from(id))?) { let current_str = extract_current_version(applicable_versions.clone())?; - if !clean { progress.println(format!("Found new version for {}", mods_get_info(config, id).unwrap().title)); } + if !clean { + progress.println(format!( + "Found new version for {}", + mods_get_info(config, id).unwrap().title + )); + } //get new versions let current_ver = match applicable_versions @@ -166,12 +184,19 @@ async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progre let files = ¤t_ver.files; let link = match files.clone().into_iter().find(|f| f.primary) { - Some(f) => f, - None => { files[0].clone() } - } - .url; - - userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id.to_string())?; + Some(f) => f, + None => files[0].clone(), + } + .url; + + userlist_change_versions( + config, + list.id, + current_str, + versions.join("|"), + link, + id.to_string(), + )?; } if current.is_empty() { -- cgit v1.2.3