From 529d52534c300aec4a6e3e9e08f9762a401f7086 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 25 May 2023 11:16:16 +0200 Subject: added more progress --- src/files.rs | 61 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index a4a1d3b..04b00f0 100644 --- a/src/files.rs +++ b/src/files.rs @@ -17,55 +17,50 @@ use crate::{ List, }; +const PROGRESS_CHARS: &str = "#>-"; + pub async fn download_versions(list: List, config: Cfg, versions: Vec) -> MLE<()> { let cached = get_cached_versions(&config.cache); - // println!("{:#?}", cached); - - // println!(" └Download mods to {}", dl_path); - let mp = MultiProgress::new(); let mut js = JoinSet::new(); - let style = ProgressStyle::with_template("{spinner:.green}{msg}\t[{bar:.green/lime}] {bytes}/{total_bytes}") - .unwrap() - .progress_chars("#>-"); + let style_spinner = ProgressStyle::with_template("{spinner:.green}{wide_msg}").unwrap(); + + let all = mp.add(ProgressBar::new(versions.len().try_into().unwrap())); + all.set_style(ProgressStyle::with_template("{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS)); + all.set_message("Downloading"); + for ver in versions { - let p = mp.add(ProgressBar::new(1)); - p.set_style(style.clone()); + let p = mp.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)); } - mp.clear().unwrap(); + while js.join_next().await.is_some() { all.inc(1) } + + all.finish(); - while js.join_next().await.is_some() {} + // mp.clear().unwrap(); Ok(()) } async fn download_version(config: Cfg, list: List, version: Version, mut cached: HashMap, progress: ProgressBar) -> MLE<()> { - let project_info = mods_get_info(config.clone(), &version.project_id)?; + let project_info = mods_get_info(&config, &version.project_id)?; let dl_path = String::from(&list.download_folder); - progress.set_message(String::from(&version.id)); + progress.set_message(format!("{} - {}", project_info.title, version.id)); //Check cache if already downloaded let c = cached.remove(&version.id); if c.is_some() { - print!( - "\t└({})Get version {} from cache", - project_info.title, version.id - ); - //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush()?; + progress.set_message(format!("Get {} from cache", version.id)); copy_cached_version(&c.unwrap(), &dl_path); - println!(" ✓"); } else { - // print!("\t└({})Download version {}", project_info.title, version.id); - //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush().unwrap(); let files = version.files; let file = match files.clone().into_iter().find(|f| f.primary) { Some(f) => f, @@ -91,19 +86,15 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: &progress ) .await?; - // println!(" ✓"); - //Copy file to cache - // print!("\t └Copy to cache"); - //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush().unwrap(); + + progress.set_message(format!("Copy {} to cache", version.id)); let dl_path_file = format!("{}/{}", list.download_folder, filename); let cache_path = format!("{}/{}", &config.clone().cache, filename); - // println!("{}:{}", dl_path_file, cache_path); + copy(dl_path_file, cache_path)?; - // println!(" ✓"); } - progress.finish_with_message(format!("✓{}", version.id)); + progress.finish_with_message(format!("✓{} - {}", project_info.title, version.id)); Ok(()) } @@ -114,8 +105,12 @@ async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar let size = res.content_length().expect("Couldn't get content length"); + let style_bar_byte = ProgressStyle::with_template("{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]") + .unwrap() + .progress_chars(PROGRESS_CHARS); + progress.set_length(size); - // progress.set_style(ProgressStyle::with_template("{spinner:.green}{msg}\t[{wide_bar:.green/lime}] {bytes}/{total_bytes}").unwrap().progress_chars("#>-")); + progress.set_style(style_bar_byte); // download chunks let mut file = File::create(&dl_path_file)?; @@ -124,7 +119,7 @@ async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar let mut downloaded: u64 = 0; while let Some(item) = stream.next().await { - progress.inc(1); + // progress.inc(1); let chunk = item?; file.write_all(&chunk)?; -- cgit v1.2.3