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 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 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 +++++++--------- src/db.rs | 4 +-- src/files.rs | 22 ++++++---------- src/lib.rs | 5 ++-- src/main.rs | 20 +++++++------- 11 files changed, 100 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 394a3e6..39a163c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -829,7 +829,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.14.2" +version = "0.15.0" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index a4b51bc..c0fca21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "modlist" -version = "0.14.2" +version = "0.15.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 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(()) } diff --git a/src/db.rs b/src/db.rs index 22085a5..1f3ad4c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -525,7 +525,7 @@ pub fn lists_remove(config: &Cfg, id: String) -> MLE<()> { Ok(()) } -pub fn lists_get(config: &Cfg, list_id: String) -> MLE { +pub fn lists_get(config: &Cfg, list_id: &str) -> MLE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); @@ -549,7 +549,7 @@ pub fn lists_get(config: &Cfg, list_id: String) -> MLE { for l in list_iter { let li = l?; list = List { - id: String::from(&list_id), + id: list_id.to_string(), mc_version: String::from(&li[0]), modloader: Modloader::from(&li[1])?, download_folder: String::from(&li[2]), diff --git a/src/files.rs b/src/files.rs index 2830a5f..814f06d 100644 --- a/src/files.rs +++ b/src/files.rs @@ -14,37 +14,29 @@ use crate::{ db::{mods_get_info, userlist_add_disabled_versions}, error::{ErrorType, MLError, MLE}, modrinth::Version, - List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_SPINNER, STYLE_BAR_BYTE, + List, PROGRESS_CHARS, STYLE_SPINNER, STYLE_BAR_BYTE, STYLE_BAR_POS, }; -pub async fn download_versions(list: List, config: Cfg, versions: Vec, progress: &MultiProgress, progress_before: Option<&ProgressBar>) -> MLE<()> { +pub async fn download_versions(list: List, config: Cfg, versions: Vec, progress: &MultiProgress, progress_before: &ProgressBar) -> MLE<()> { let cached = get_cached_versions(&config.cache); let mut js = JoinSet::new(); let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).unwrap(); - let all = match progress_before { - Some(p) => progress.insert_before(p, ProgressBar::new(versions.len().try_into().unwrap())), - None => progress.add(ProgressBar::new(versions.len().try_into().unwrap())), - - - }; + let all = progress.insert_before(progress_before, ProgressBar::new(versions.len().try_into().unwrap())); all.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); - all.set_message("Downloading"); + all.set_message(format!("✓Downloading {}", list.id)); for ver in versions { let p = progress.insert_before(&all, ProgressBar::new(1)); p.set_style(style_spinner.clone()); js.spawn(download_version(config.clone(), list.clone(), ver, cached.clone(), p)); - // std::thread::sleep(std::time::Duration::from_millis(200)); } while js.join_next().await.is_some() { all.inc(1) } - all.finish(); - - // mp.clear().unwrap(); + all.finish_with_message(format!("✓Downloading {}", list.id)); Ok(()) } @@ -56,10 +48,12 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: progress.set_message(format!("{} - {}", project_info.title, version.id)); + let mut cache_msg = ""; //Check cache if already downloaded let c = cached.remove(&version.id); if c.is_some() { progress.set_message(format!("Get {} from cache", version.id)); + cache_msg = " (cached)"; copy_cached_version(&c.unwrap(), &dl_path); } else { let files = version.files; @@ -95,7 +89,7 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: copy(dl_path_file, cache_path)?; } - progress.finish_with_message(format!("✓{} - {}", project_info.title, version.id)); + progress.finish_with_message(format!("✓{} - {}{}", project_info.title, version.id, cache_msg)); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index a7a34ac..7287660 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,10 @@ use error::{ErrorType, MLError, MLE}; use indicatif::{ProgressStyle, ProgressBar}; use serde::{Deserialize, Serialize}; -pub static STYLE_BAR_POS: &str = "{spinner:.green}{wide_msg}{pos}/{len} [{bar:.green/lime}]"; pub static STYLE_BAR_BYTE: &str = "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]"; +pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]"; pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}"; +pub static STYLE_OPERATION: &str = " {wide_msg}"; pub static STYLE_MESSAGE: &str = "{wide_msg}"; pub static PROGRESS_CHARS: &str = "#>-"; @@ -70,7 +71,7 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { let creation_time = fs::metadata(path)?.created()?; if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { return Ok(()); } - std::io::stdout().flush()?; + let versions = get_game_versions().await; remove_file(path)?; let mut file = File::create(path)?; diff --git a/src/main.rs b/src/main.rs index 7e00368..d03f88a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,10 +170,10 @@ async fn main() { lock, } => { let listf = match list { - Some(list) => lists_get(&config, list).unwrap(), + Some(list) => lists_get(&config, &list).unwrap(), None => lists_get( &config, - config_get_current_list(&config).unwrap(), + &config_get_current_list(&config).unwrap(), ) .unwrap(), }; @@ -189,10 +189,10 @@ async fn main() { } ModCommands::Remove { id, list } => { let listf = match list { - Some(list) => lists_get(&config, list).unwrap(), + Some(list) => lists_get(&config, &list).unwrap(), None => lists_get( &config, - config_get_current_list(&config).unwrap(), + &config_get_current_list(&config).unwrap(), ) .unwrap(), }; @@ -231,7 +231,7 @@ async fn main() { version, download, remove, - } => list_version(&config, id, version, download, remove).await, + } => list_version(&config, &id, version, download, remove).await, } } Commands::Update { @@ -245,11 +245,11 @@ async fn main() { if all { let list_ids = lists_get_all_ids(&config).unwrap(); for id in list_ids { - liststack.push(lists_get(&config, id).unwrap()); + liststack.push(lists_get(&config, &id).unwrap()); } } else { let current = match list { - Some(l) => lists_get(&config, l).unwrap(), + Some(l) => lists_get(&config, &l).unwrap(), None => get_current_list(&config).unwrap(), }; liststack.push(current) @@ -262,11 +262,11 @@ async fn main() { if all { let list_ids = lists_get_all_ids(&config).unwrap(); for id in list_ids { - liststack.push(lists_get(&config, id).unwrap()); + liststack.push(lists_get(&config, &id).unwrap()); } } else { let current = match list { - Some(l) => lists_get(&config, l).unwrap(), + Some(l) => lists_get(&config, &l).unwrap(), None => get_current_list(&config).unwrap(), }; liststack.push(current) @@ -285,7 +285,7 @@ async fn main() { .unwrap(), }; - import(&config, filestr, download).await + import(&config, &filestr, download).await } Commands::Export { list } => export(&config, list), Commands::Test => Ok(()), -- cgit v1.2.3