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/modification.rs | 70 ++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 22 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index d4c49d6..730583d 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,5 +1,7 @@ use std::{io::Write, collections::HashMap}; +use indicatif::{ProgressBar, ProgressStyle}; + use crate::{ config::Cfg, db::{ @@ -12,6 +14,8 @@ use crate::{ List, }; +const PROGRESS_CHARS: &str = "#>-"; + #[derive(Debug, Clone)] pub struct AddMod { pub id: IDSelector, @@ -41,28 +45,45 @@ pub async fn mod_add( list: List, direct_download: bool, ) -> MLE<()> { - println!("Add mods to {}", list.id); - println!(" └Add mods:"); + let spinner_style = ProgressStyle::with_template("{spinner:.green}{msg}").unwrap(); + let bar_style = ProgressStyle::with_template("{spinner:.green}{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS); + + // println!("Add mods to {}", list.id); + // println!(" └Add mods:"); let mut mod_ids: Vec<(String, bool)> = Vec::new(); let mut ver_ids: Vec<(String, bool)> = Vec::new(); + let p = ProgressBar::new(mods.len().try_into().unwrap()); + p.set_style(spinner_style.clone()); + p.set_message("Sort ids"); + //"Sort" project ids from version ids to be able to handle them differently but in a batch for m in mods { + p.inc(1); match m.id { IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), } } - + + p.finish_with_message("Sort ids done"); + + let info_p = ProgressBar::new(2); + info_p.set_message("Get infos"); + info_p.set_style(bar_style.clone()); let mut projectinfo: Vec = Vec::new(); if !mod_ids.is_empty() { - projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) + projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?); + info_p.inc(1); }; if !ver_ids.is_empty() { - projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) + projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?); + info_p.inc(1); }; + info_p.finish_with_message("Get infos done"); + if projectinfo.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); }; @@ -70,18 +91,19 @@ pub async fn mod_add( let mut downloadstack: Vec = Vec::new(); //Adding each mod to the lists and downloadstack - if projectinfo.len() == 1 { - println!(" └Insert mod in list {} and save infos", list.id); - } else { - println!(" └Insert mods in list {} and save infos", list.id); - } + let add_p = ProgressBar::new(projectinfo.len().try_into().unwrap()); + add_p.set_style(bar_style); for project in projectinfo { + + add_p.set_message(format!("Add {}", project.title)); + let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { project.current_version.clone().unwrap().id }; + match userlist_insert( config.clone(), &list.id, @@ -124,8 +146,13 @@ pub async fn mod_add( if project.current_version.is_some() { downloadstack.push(project.current_version.unwrap()) }; + + // add_p.println(format!("Added {}", project.title)); + add_p.inc(1); } + add_p.finish_with_message("Added all mods"); + //Download all the added mods if direct_download { download_versions(list.clone(), config.clone(), downloadstack).await?; @@ -140,7 +167,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> let mut ids = vec![]; - println!("{:?}", mod_ids); + // println!("{:?}", mod_ids); for id in mod_ids { setmap.insert(id.0.to_string(), id.1); @@ -156,8 +183,8 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> _ => panic!("PANIC"), }; for project in m_projects { - println!("\t└{}", project.title); - println!("\t └Get versions"); + // println!("\t└{}", project.title); + // println!("\t └Get versions"); let available_versions = versions( &config.apis.modrinth, String::from(&project.id), @@ -170,7 +197,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> let file: String; if !available_versions.is_empty() { let current_id = extract_current_version(available_versions.clone())?; - println!("\t └Current version: {}", current_id); + // println!("\t └Current version: {}", current_id); current_version = Some( available_versions @@ -197,7 +224,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> available_versions_vec.push(ver.id); } - println!("{:?}", setmap); + // println!("{:?}", setmap); projectinfo.push(ProjectInfo { mod_id: String::from(&project.id), @@ -206,10 +233,10 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> current_version, applicable_versions: available_versions_vec, download_link: file, - set_version: setmap.get(&project.slug).unwrap().clone(), + set_version: *setmap.get(&project.slug).unwrap(), }) } else { - println!("\t └There's currently no mod version for your specified target"); + // println!("\t └There's currently no mod version for your specified target"); current_version = None; file = String::from("NONE"); available_versions_vec.push(String::from("NONE")); @@ -220,7 +247,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> current_version, applicable_versions: available_versions_vec, download_link: file, - set_version: setmap.get(&project.id).unwrap().clone(), + set_version: *setmap.get(&project.id).unwrap(), }) } } @@ -252,7 +279,7 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE) -> MLE) -> MLE MLE<()> { let mod_id = mods_get_id(&config.data, id)?; - println!("Remove mod {} from {}", mods_get_info(config.clone(), &mod_id)?.title, list.id); + println!("Remove mod {} from {}", mods_get_info(&config, &mod_id)?.title, list.id); let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; print!(" └Remove from list"); @@ -300,7 +327,6 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { return Err(err); }; - () }, }; println!(" ✓"); -- cgit v1.2.3