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/download.rs | 39 ++++++++++----- src/commands/io.rs | 34 ++++++++++--- src/commands/list.rs | 54 +++++++++++++------- src/commands/modification.rs | 114 +++++++++++++++++++++---------------------- src/commands/update.rs | 95 +++++++++++++++++++++++------------- 5 files changed, 209 insertions(+), 127 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 7aa0156..dd00ffb 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,7 +1,5 @@ +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; -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, @@ -11,12 +9,21 @@ use crate::{ }, modrinth::get_raw_versions, }; - -pub async fn download(config: &Cfg, liststack: Vec, clean: bool, delete_old: bool) -> MLE<()> { - +use crate::{PROGRESS_CHARS, STYLE_BAR_POS}; + +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)); + download_p.set_style( + ProgressStyle::with_template(STYLE_BAR_POS) + .unwrap() + .progress_chars(PROGRESS_CHARS), + ); for current_list in liststack { download_p.set_message(format!("Download in {}", current_list.id)); @@ -67,17 +74,27 @@ pub async fn download(config: &Cfg, liststack: Vec, clean: bool, delete_ol ) .await?; } else { - download_p.println(format!("There are no new versions to download for {}", current_list.id)); + 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)); + 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 { d_p.set_message(format!("Delete version {}", ver.1)); d_p.inc(1); - delete_version(current_list.clone(), ver.1)?; + delete_version(¤t_list, ver.1)?; } else { d_p.set_message(format!("Disable version {}", ver.1)); d_p.inc(1); diff --git a/src/commands/io.rs b/src/commands/io.rs index 45e363e..2501583 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -1,12 +1,16 @@ +use indicatif::{ProgressBar, ProgressStyle}; use serde::{Deserialize, Serialize}; use std::fs::File; use std::io::prelude::*; use crate::{ config::Cfg, - db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_set_version, userlist_get_all_ids, userlist_get_current_version}, + db::{ + lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, + userlist_get_current_version, userlist_get_set_version, + }, error::MLE, - mod_add, IDSelector, List, Modloader, AddMod, + mod_add, AddMod, IDSelector, List, Modloader, STYLE_OPERATION, }; #[derive(Debug, Serialize, Deserialize)] @@ -17,14 +21,14 @@ struct Export { #[derive(Debug, Serialize, Deserialize)] struct ExportVersion { version: String, - set: bool + set: bool, } impl ExportVersion { fn from(config: &Cfg, list_id: &str, mod_id: &str) -> MLE { Ok(Self { version: userlist_get_current_version(config, list_id, mod_id)?, - set: userlist_get_set_version(config, list_id, mod_id)? + set: userlist_get_set_version(config, list_id, mod_id)?, }) } } @@ -64,23 +68,36 @@ impl ExportList { } pub fn export(config: &Cfg, list: Option) -> MLE<()> { + let progress = ProgressBar::new_spinner(); + progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + let mut list_ids: Vec = vec![]; if list.is_none() { list_ids = lists_get_all_ids(config)?; } else { list_ids.push(lists_get(config, &list.unwrap())?.id); } + let mut lists: Vec = vec![]; for list_id in list_ids { + progress.set_message(format!("Export {}", list_id)); + //TODO download option/ new download on import lists.push(ExportList::from(config, &list_id, true)?); } let toml = toml::to_string(&Export { lists })?; - let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); + let filestr = dirs::home_dir() + .unwrap() + .join("mlexport.toml") + .into_os_string() + .into_string() + .unwrap(); - let mut file = File::create(filestr.into_os_string().into_string().unwrap().as_str())?; + progress.set_message("Create file"); + let mut file = File::create(&filestr)?; file.write_all(toml.as_bytes())?; + progress.finish_with_message(format!("Exported to {}", filestr)); Ok(()) } @@ -108,7 +125,10 @@ pub async fn import(config: &Cfg, file_str: &str, direct_download: bool) -> MLE< let mut ver_ids = vec![]; for id in exportlist.versions { - ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} ); + ver_ids.push(AddMod { + id: IDSelector::VersionID(id.version), + set_version: id.set, + }); } mod_add(config, ver_ids, list, direct_download).await?; } diff --git a/src/commands/list.rs b/src/commands/list.rs index 52f14f2..b0a082d 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,11 +1,13 @@ +use indicatif::{ProgressBar, ProgressStyle}; + use crate::{ config::Cfg, db::{ - config_change_current_list, config_get_current_list, lists_get, lists_insert, lists_remove, - lists_version, lists_get_all_ids, + config_change_current_list, config_get_current_list, lists_get, lists_get_all_ids, + lists_insert, lists_remove, lists_version, }, - error::{MLE, MLError, ErrorType}, - update, Modloader, + error::{ErrorType, MLError, MLE}, + update, Modloader, STYLE_OPERATION, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -28,20 +30,35 @@ pub fn list_add( modloader: &Modloader, directory: &str, ) -> MLE<()> { - lists_insert(config, id, mc_version, modloader, directory) + let p = ProgressBar::new_spinner(); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_message(format!("Create {}", id)); + lists_insert(config, id, mc_version, modloader, directory)?; + p.finish_with_message(format!("Created {}", id)); + Ok(()) } -pub fn list_change(config: &Cfg, id: String) -> MLE<()> { +pub fn list_change(config: &Cfg, id: &str) -> MLE<()> { + let p = ProgressBar::new_spinner(); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_message(format!("Change default list to {}", id)); + if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { return Err(MLError::new(ErrorType::ArgumentError, "List not found")); }; - println!("Change default list to: {}", id); - config_change_current_list(config, id) + config_change_current_list(config, id)?; + + p.finish_with_message(format!("Changed default list to {}", id)); + Ok(()) } -pub fn list_remove(config: &Cfg, id: String) -> MLE<()> { - //TODO add logging - lists_remove(config, id) +pub fn list_remove(config: &Cfg, id: &str) -> MLE<()> { + let p = ProgressBar::new_spinner(); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_message(format!("Remove {}", id)); + lists_remove(config, id)?; + p.finish_with_message(format!("Removed {}", id)); + Ok(()) } ///Changing the current lists version and updating it @@ -57,17 +74,20 @@ pub async fn list_version( download: bool, delete: bool, ) -> MLE<()> { - println!( + let p = ProgressBar::new_spinner(); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_message(format!( "Change version for list {} to minecraft version: {}", id, mc_version - ); + )); lists_version(config, id, &mc_version)?; - println!( - "\nCheck for updates for new minecraft version in list {}", - id - ); + p.finish_with_message(format!( + "Changed version for list {} to minecraft version: {}", + id, mc_version + )); + let list = lists_get(config, id)?; update(config, vec![list], true, download, delete).await } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 8abf913..fdb70c7 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,23 +1,23 @@ -use std::{io::Write, collections::HashMap}; +use std::collections::HashMap; -use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ config::Cfg, db::{ - lists_get_all_ids, mods_get_id, mods_insert, mods_remove, userlist_get_all_ids, - userlist_get_current_version, userlist_insert, userlist_remove, mods_get_info, + lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, mods_remove, + userlist_get_all_ids, userlist_get_current_version, userlist_insert, userlist_remove, }, 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, + List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; #[derive(Debug)] pub struct AddMod { pub id: IDSelector, - pub set_version: bool + pub set_version: bool, } #[derive(Debug, PartialEq, Eq)] @@ -47,9 +47,13 @@ pub async fn mod_add( let mut mod_ids: Vec<(String, bool)> = Vec::new(); let mut ver_ids: Vec<(String, bool)> = Vec::new(); - + 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_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 @@ -62,7 +66,7 @@ pub async fn mod_add( } add_p.set_message("Get infos"); - + let mut projectinfo: Vec = Vec::new(); if !mod_ids.is_empty() { projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?); @@ -80,11 +84,17 @@ pub async fn mod_add( let mut downloadstack: Vec = Vec::new(); //Adding each mod to the lists and downloadstack - 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)); + 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 { - project_p.set_message(format!("Add {}", project.title)); let current_version_id = if project.current_version.is_none() { @@ -116,12 +126,7 @@ pub async fn mod_add( Ok(..) => Ok(..), }?; - match mods_insert( - config, - &project.mod_id, - &project.slug, - &project.title, - ) { + match mods_insert(config, &project.mod_id, &project.slug, &project.title) { Err(e) => { if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) @@ -152,8 +157,11 @@ pub async fn mod_add( Ok(()) } -async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE> { - +async fn get_mod_infos( + config: &Cfg, + mod_ids: Vec<(String, bool)>, + list: List, +) -> MLE> { let mut setmap: HashMap = HashMap::new(); let mut ids = vec![]; @@ -197,19 +205,15 @@ async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) - .find(|v| v.id == current_id) .unwrap(), ); - + // match primary, if none? - let files = current_version - .clone() - .ok_or("") - .unwrap() - .files; + let files = current_version.clone().ok_or("").unwrap().files; file = match files.clone().into_iter().find(|f| f.primary) { - Some(f) => f, - None => { files[0].clone() } - } - .url; + Some(f) => f, + None => files[0].clone(), + } + .url; for ver in available_versions { available_versions_vec.push(ver.id); @@ -247,7 +251,6 @@ async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) - } async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE> { - let mut setmap: HashMap = HashMap::new(); let mut ids = vec![]; @@ -271,15 +274,13 @@ async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE f, - None => { files[0].clone() } - } - .url; + Some(f) => f, + None => files[0].clone(), + } + .url; projectinfo.push(ProjectInfo { mod_id: String::from(&project.id), @@ -300,34 +301,31 @@ async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE MLE<()> { +pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { + let progress = ProgressBar::new_spinner(); + progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + let mod_id = mods_get_id(&config.data, id)?; - println!("Remove mod {} from {}", mods_get_info(config, &mod_id)?.title, list.id); + let info = mods_get_info(config, &mod_id)?; + + progress.set_message(format!("Remove {} from {}", info.title, list.id)); + let version = userlist_get_current_version(config, &list.id, &mod_id)?; - print!(" └Remove from list"); - //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush()?; userlist_remove(config, &list.id, &mod_id)?; - println!(" ✓"); - print!(" └Delete file"); - //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush()?; + progress.set_message("Delete file"); match delete_version(list, version) { Ok(_) => (), Err(err) => { - if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { + if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { return Err(err); }; - }, + } }; - println!(" ✓"); - print!(" └Clean main db table"); - //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush()?; + progress.set_message("Check main list"); let list_ids = lists_get_all_ids(config)?; // Remove mod from main list if not used elsewhere @@ -336,11 +334,11 @@ pub fn mod_remove(config: &Cfg, id: &str, list: List) -> MLE<()> { let mods = match userlist_get_all_ids(config, &id) { Ok(m) => m, Err(err) => { - if err.to_string() == "Database: NO_MODS_USERLIST" { + if err.to_string() == "Database: NO_MODS_USERLIST" { println!(" ✓"); return Ok(()); }; - return Err(err) + return Err(err); } }; if mods.contains(&mod_id) { @@ -350,9 +348,11 @@ pub fn mod_remove(config: &Cfg, id: &str, list: List) -> MLE<()> { } if !mod_used { - mods_remove(config, mod_id)?; + progress.set_message("Remove from main list"); + mods_remove(config, &mod_id)?; }; - println!(" ✓"); + + progress.finish_with_message(format!("Removed {} from {}", info.title, list.id)); Ok(()) } 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