From 529d52534c300aec4a6e3e9e08f9762a401f7086 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 25 May 2023 11:16:16 +0200 Subject: added more progress --- src/commands/update.rs | 79 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 22 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index d3a282b..2de13f3 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,3 +1,5 @@ +use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; + use crate::{ config::Cfg, db::{ @@ -10,6 +12,8 @@ use crate::{ List, }; +const PROGRESS_CHARS: &str = "#>-"; + pub async fn update( config: Cfg, liststack: Vec, @@ -17,20 +21,39 @@ 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())); + let bar_style = ProgressStyle::with_template("{spinner:.green}{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS); + let spinner_style = ProgressStyle::with_template("{spinner:.green}{msg}").unwrap(); + update_p.set_style(bar_style.clone()); + update_p.set_message("Update"); + for current_list in liststack { - println!("Update mods in {}", current_list.id); + + // println!("Update mods in {}", current_list.id); let mods = userlist_get_all_ids(config.clone(), ¤t_list.id)?; + let list_p = mp.insert_before(&update_p, ProgressBar::new(mods.len().try_into().unwrap())); + list_p.set_style(bar_style.clone()); + list_p.set_message(format!("Update {}", current_list.id)); + let mut current_versions: Vec<(String, String)> = vec![]; let mut updatestack: Vec = vec![]; for id in mods { - let info = mods_get_info(config.clone(), &id)?; - println!(" ├{}", info.title); + let mod_p = mp.insert_before(&list_p, ProgressBar::new(1)); + mod_p.set_style(spinner_style.clone()); + + let info = mods_get_info(&config, &id)?; + mod_p.set_message(format!("Update {}", info.title)); + // println!(" ├{}", info.title); if userlist_get_set_version(config.clone(), ¤t_list.id, &id)? { - println!(" │ └Set version, skipping update"); + // println!(" │ └Set version, skipping update"); + list_p.inc(1); continue; } @@ -38,12 +61,15 @@ pub async fn update( let disable_version = userlist_get_current_version(config.clone(), ¤t_list.id, &id)?; + mod_p.inc(1); + updatestack.push( match specific_update( config.clone(), clean, current_list.clone(), String::from(&id), + &mod_p ) .await { @@ -53,19 +79,25 @@ pub async fn update( } Err(e) => { if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { - println!( - " │ └No new version found for the specified minecraft version" - ); + // println!( + // " │ └No new version found for the specified minecraft version" + // ); } else { return Err(e); }; + list_p.inc(1); continue; } }, - ) + ); + list_p.inc(1); } + list_p.finish_with_message(format!("Updated {}", current_list.id)); + if clean { + update_p.set_message("Cleaning"); + update_p.inc(1); clean_list_dir(¤t_list)?; }; @@ -85,12 +117,16 @@ pub async fn update( } } }; + update_p.inc(1); } + update_p.finish_with_message("Updated all lists"); + + Ok(()) } -async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE { +async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progress: &ProgressBar) -> MLE { let applicable_versions = versions(&config.apis.modrinth, String::from(&id), list.clone()).await; @@ -116,10 +152,11 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML let current_str = extract_current_version(applicable_versions.clone())?; if clean { - println!("\t └Add version to downloadstack"); + // println!("\t └Add version to downloadstack"); } else { - println!("\t └Get versions for specified minecraft versions"); - println!("\t └New current version: {}", current_str); + progress.println(format!("Found new version for {}", mods_get_info(&config, &id).unwrap().title)); + // println!("\t └Get versions for specified minecraft versions"); + // println!("\t └New current version: {}", current_str); }; //get new versions @@ -133,16 +170,14 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML }?; current.push(current_ver.clone()); - let link = match current_ver - .files - .into_iter() - .find(|f| f.primary) - .ok_or("!no primary in links") - { - Ok(p) => Ok(p), - Err(e) => Err(MLError::new(ErrorType::Other, e)), - }? - .url; + 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)?; } -- cgit v1.2.3