From d8554e30029bf43dccce72e982784cd01857b0c4 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 25 May 2023 22:48:54 +0200 Subject: added mod add progress --- src/commands/download.rs | 31 ++++++++++++++++++---- src/commands/io.rs | 14 +++++----- src/commands/list.rs | 8 +++--- src/commands/modification.rs | 62 +++++++++++++++++++++----------------------- src/commands/update.rs | 23 +++++++--------- 5 files changed, 75 insertions(+), 63 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index e9a96b5..7aa0156 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,6 +1,7 @@ -use indicatif::MultiProgress; +use indicatif::{MultiProgress, ProgressStyle, ProgressBar}; +use crate::{STYLE_BAR_POS, PROGRESS_CHARS}; use crate::{config::Cfg, List}; use crate::{ db::userlist_get_all_current_versions_with_mods, @@ -14,9 +15,12 @@ use crate::{ pub async fn download(config: &Cfg, liststack: Vec, clean: bool, delete_old: bool) -> MLE<()> { let mp = MultiProgress::new(); + let download_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); + download_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); for current_list in liststack { - println!("Downloading current versions of mods in {}", current_list.id); + download_p.set_message(format!("Download in {}", current_list.id)); + let downloaded_versions = get_downloaded_versions(current_list.clone())?; let current_version_ids = match userlist_get_all_current_versions_with_mods( config, @@ -59,24 +63,41 @@ pub async fn download(config: &Cfg, liststack: Vec, clean: bool, delete_ol config.clone(), get_raw_versions(&config.apis.modrinth, to_download).await, &mp, - None + &download_p, ) .await?; } else { - println!("There are no new versions to download"); + download_p.println(format!("There are no new versions to download for {}", current_list.id)); } if !to_disable.is_empty() { + let d_p = mp.insert_before(&download_p, ProgressBar::new(to_disable.len().try_into().unwrap())); + d_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); for ver in to_disable { if delete_old { - // println!("Deleting version {} for mod {}", ver.1, ver.0); + d_p.set_message(format!("Delete version {}", ver.1)); + d_p.inc(1); delete_version(current_list.clone(), ver.1)?; } else { + d_p.set_message(format!("Disable version {}", ver.1)); + d_p.inc(1); disable_version(config, current_list.clone(), ver.1, ver.0)?; }; } + + let del_msg = if delete_old { + "Deleted all old versions" + } else { + "Disabled all old versions" + }; + + d_p.finish_with_message(del_msg); } + + download_p.inc(1); } + download_p.finish_with_message("Downloaded all lists"); + Ok(()) } diff --git a/src/commands/io.rs b/src/commands/io.rs index 43e642a..45e363e 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -39,18 +39,18 @@ struct ExportList { } impl ExportList { - pub fn from(config: &Cfg, list_id: String, download: bool) -> MLE { - let list = lists_get(config, String::from(&list_id))?; + pub fn from(config: &Cfg, list_id: &str, download: bool) -> MLE { + let list = lists_get(config, list_id)?; let mut dl_folder = None; if download { dl_folder = Some(list.download_folder) }; - let mods = userlist_get_all_ids(config, &list_id)?; + let mods = userlist_get_all_ids(config, list_id)?; let mut versions = vec![]; for m in mods { - versions.push(ExportVersion::from(config, &list_id, &m)?) + versions.push(ExportVersion::from(config, list_id, &m)?) } Ok(Self { @@ -68,11 +68,11 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { if list.is_none() { list_ids = lists_get_all_ids(config)?; } else { - list_ids.push(lists_get(config, list.unwrap())?.id); + list_ids.push(lists_get(config, &list.unwrap())?.id); } let mut lists: Vec = vec![]; for list_id in list_ids { - lists.push(ExportList::from(config, list_id, true)?); + lists.push(ExportList::from(config, &list_id, true)?); } let toml = toml::to_string(&Export { lists })?; @@ -85,7 +85,7 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { Ok(()) } -pub async fn import(config: &Cfg, file_str: String, direct_download: bool) -> MLE<()> { +pub async fn import(config: &Cfg, file_str: &str, direct_download: bool) -> MLE<()> { let mut file = File::open(file_str)?; let mut content = String::new(); file.read_to_string(&mut content)?; diff --git a/src/commands/list.rs b/src/commands/list.rs index 95f9927..52f14f2 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -18,7 +18,7 @@ pub struct List { pub fn get_current_list(config: &Cfg) -> MLE { let id = config_get_current_list(config)?; - lists_get(config, id) + lists_get(config, &id) } pub fn list_add( @@ -52,7 +52,7 @@ pub fn list_remove(config: &Cfg, id: String) -> MLE<()> { /// * `args` - All args, to extract the new version pub async fn list_version( config: &Cfg, - id: String, + id: &str, mc_version: String, download: bool, delete: bool, @@ -62,7 +62,7 @@ pub async fn list_version( id, mc_version ); - lists_version(config, &id, &mc_version)?; + lists_version(config, id, &mc_version)?; println!( "\nCheck for updates for new minecraft version in list {}", @@ -75,7 +75,7 @@ pub async fn list_version( pub fn list_list(config: &Cfg) -> MLE<()> { let lists = lists_get_all_ids(config)?; for list in lists { - let l = lists_get(config, list)?; + let l = lists_get(config, &list)?; println!("{}: | {} | {}", l.id, l.mc_version, l.modloader) } Ok(()) diff --git a/src/commands/modification.rs b/src/commands/modification.rs index d369c4b..8abf913 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -11,7 +11,7 @@ use crate::{ error::{ErrorType, MLError, MLE}, files::{delete_version, download_versions}, modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, - List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_SPINNER, + List, PROGRESS_CHARS, STYLE_BAR_POS, }; #[derive(Debug)] @@ -43,57 +43,49 @@ pub async fn mod_add( list: List, direct_download: bool, ) -> MLE<()> { + let mp = MultiProgress::new(); - //TODO MultiProgress - - let spinner_style = ProgressStyle::with_template(STYLE_SPINNER).unwrap(); - let bar_style = ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS); 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"); + + let add_p = mp.add(ProgressBar::new(mods.len().try_into().unwrap())); + add_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); + add_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); + add_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)), } } + + add_p.set_message("Get infos"); - 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, mod_ids, list.clone()).await?); - info_p.inc(1); }; if !ver_ids.is_empty() { projectinfo.append(&mut get_ver_info(config, 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?")); }; + add_p.set_message("Add mods to database"); + let mut downloadstack: Vec = Vec::new(); //Adding each mod to the lists and downloadstack - let add_p = ProgressBar::new(projectinfo.len().try_into().unwrap()); - add_p.set_style(bar_style); + let project_p = mp.insert_before(&add_p, ProgressBar::new(projectinfo.len().try_into().unwrap())); + project_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); for project in projectinfo { - add_p.set_message(format!("Add {}", project.title)); + project_p.set_message(format!("Add {}", project.title)); let current_version_id = if project.current_version.is_none() { String::from("NONE") @@ -144,18 +136,19 @@ pub async fn mod_add( downloadstack.push(project.current_version.unwrap()) }; - // add_p.println(format!("Added {}", project.title)); - add_p.inc(1); + project_p.inc(1); } - add_p.finish_with_message("Added all mods"); + project_p.finish_with_message("Added all mods to the database"); //Download all the added mods if direct_download { - let mp = MultiProgress::new(); - download_versions(list.clone(), config.clone(), downloadstack, &mp, None).await?; + add_p.set_message("Download mods"); + download_versions(list.clone(), config.clone(), downloadstack, &mp, &add_p).await?; }; + add_p.finish_with_message("Added all mods"); + Ok(()) } @@ -277,14 +270,17 @@ async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE f, + None => { files[0].clone() } + } .url; + projectinfo.push(ProjectInfo { mod_id: String::from(&project.id), slug: project.slug, diff --git a/src/commands/update.rs b/src/commands/update.rs index bde6896..194bbe5 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -9,7 +9,7 @@ use crate::{ error::{ErrorType, MLError, MLE}, files::{clean_list_dir, delete_version, disable_version, download_versions}, modrinth::{extract_current_version, versions, Version}, - List, PROGRESS_CHARS, STYLE_BAR_POS, + List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; pub async fn update( @@ -23,23 +23,21 @@ pub async fn update( let mp = MultiProgress::new(); let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); - let bar_style = ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS); - update_p.set_style(bar_style.clone()); - update_p.set_message("Update"); + 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)); + let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); - list_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); - list_p.set_message(format!("Update {}", current_list.id)); + list_p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + list_p.set_message("Update mods"); 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(bar_style.clone()); - list_u_p.set_message(format!("Update {}", current_list.id)); + 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![]; for id in mods { @@ -84,7 +82,6 @@ pub async fn update( } list_u_p.finish_with_message(format!("Updated mods in {}", current_list.id)); - list_p.inc(1); if clean { list_p.set_message("Cleaning"); @@ -92,12 +89,12 @@ pub async fn update( }; if direct_download && !updatestack.is_empty() { - download_versions(current_list.clone(), config.clone(), updatestack, &mp, Some(&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(bar_style.clone()); + 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)); @@ -119,14 +116,12 @@ pub async fn update( d_p.finish_with_message(del_msg); } }; - list_p.inc(1); list_p.finish_with_message(format!("Updated {}", current_list.id)); update_p.inc(1); } update_p.finish_with_message("Updated all lists"); - Ok(()) } -- cgit v1.2.3