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/modification.rs | 114 +++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 57 deletions(-) (limited to 'src/commands/modification.rs') 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(()) } -- cgit v1.2.3