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/files.rs | 77 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 26 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 814f06d..e874d9d 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,12 +1,13 @@ use futures_util::StreamExt; -use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use reqwest::Client; -use tokio::task::JoinSet; use std::{ + cmp::min, collections::HashMap, fs::{copy, read_dir, remove_file, rename, File}, - io::Write, cmp::min, + io::Write, }; +use tokio::task::JoinSet; use crate::{ cache::{copy_cached_version, get_cached_versions}, @@ -14,34 +15,61 @@ use crate::{ db::{mods_get_info, userlist_add_disabled_versions}, error::{ErrorType, MLError, MLE}, modrinth::Version, - List, PROGRESS_CHARS, STYLE_SPINNER, STYLE_BAR_BYTE, STYLE_BAR_POS, + List, PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER, }; -pub async fn download_versions(list: List, config: Cfg, versions: Vec, progress: &MultiProgress, progress_before: &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 = 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)); + 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(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)); + js.spawn(download_version( + config.clone(), + list.clone(), + ver, + cached.clone(), + p, + )); + } + + while js.join_next().await.is_some() { + all.inc(1) } - while js.join_next().await.is_some() { all.inc(1) } - all.finish_with_message(format!("✓Downloading {}", list.id)); Ok(()) } -async fn download_version(config: Cfg, list: List, version: Version, mut cached: HashMap, progress: ProgressBar) -> MLE<()> { +async fn download_version( + config: Cfg, + list: List, + version: Version, + mut cached: HashMap, + progress: ProgressBar, +) -> MLE<()> { let project_info = mods_get_info(&config, &version.project_id)?; let dl_path = String::from(&list.download_folder); @@ -59,7 +87,7 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: let files = version.files; let file = match files.clone().into_iter().find(|f| f.primary) { Some(f) => f, - None => files[0].clone() + None => files[0].clone(), }; let mut splitname: Vec<&str> = file.filename.split('.').collect(); let extension = match splitname.pop().ok_or("") { @@ -74,13 +102,7 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: extension ); - download_file( - &file.url, - &list.download_folder, - &filename, - &progress - ) - .await?; + download_file(&file.url, &list.download_folder, &filename, &progress).await?; progress.set_message(format!("Copy {} to cache", version.id)); let dl_path_file = format!("{}/{}", list.download_folder, filename); @@ -89,7 +111,10 @@ 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, cache_msg)); + progress.finish_with_message(format!( + "✓{} - {}{}", + project_info.title, version.id, cache_msg + )); Ok(()) } @@ -117,7 +142,7 @@ async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar // progress.inc(1); let chunk = item?; file.write_all(&chunk)?; - + // Progress bar let new = min(downloaded + (chunk.len() as u64), size); downloaded = new; @@ -136,7 +161,7 @@ pub fn disable_version( mod_id: String, ) -> MLE<()> { //println!("Disabling version {} for mod {}", versionid, mod_id); - let file = get_file_path(current_list.clone(), String::from(&versionid))?; + let file = get_file_path(¤t_list, String::from(&versionid))?; let disabled = format!("{}.disabled", file); rename(file, disabled)?; @@ -146,7 +171,7 @@ pub fn disable_version( Ok(()) } -pub fn delete_version(list: List, version: String) -> MLE<()> { +pub fn delete_version(list: &List, version: String) -> MLE<()> { let file = get_file_path(list, version)?; remove_file(file)?; @@ -154,9 +179,9 @@ pub fn delete_version(list: List, version: String) -> MLE<()> { Ok(()) } -pub fn get_file_path(list: List, versionid: String) -> MLE { +pub fn get_file_path(list: &List, versionid: String) -> MLE { let mut names: HashMap = HashMap::new(); - for file in read_dir(list.download_folder)? { + for file in read_dir(&list.download_folder)? { let path = file?.path(); if path.is_file() { let pathstr = match path.to_str().ok_or("") { -- cgit v1.2.3