summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2023-05-25 11:16:16 +0200
committerfxqnlr <[email protected]>2023-05-25 11:16:16 +0200
commit529d52534c300aec4a6e3e9e08f9762a401f7086 (patch)
tree463d3538dd295bbf6416ca3f141a1395d6cd1b76 /src/files.rs
parent016e1d8d760113a64afcc5d516f08010cb566d68 (diff)
downloadmodlist-529d52534c300aec4a6e3e9e08f9762a401f7086.tar
modlist-529d52534c300aec4a6e3e9e08f9762a401f7086.tar.gz
modlist-529d52534c300aec4a6e3e9e08f9762a401f7086.zip
added more progress
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs61
1 files changed, 28 insertions, 33 deletions
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::{
17 List, 17 List,
18}; 18};
19 19
20const PROGRESS_CHARS: &str = "#>-";
21
20pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<()> { 22pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<()> {
21 let cached = get_cached_versions(&config.cache); 23 let cached = get_cached_versions(&config.cache);
22 24
23 // println!("{:#?}", cached);
24
25 // println!(" └Download mods to {}", dl_path);
26
27 let mp = MultiProgress::new(); 25 let mp = MultiProgress::new();
28 26
29 let mut js = JoinSet::new(); 27 let mut js = JoinSet::new();
30 let style = ProgressStyle::with_template("{spinner:.green}{msg}\t[{bar:.green/lime}] {bytes}/{total_bytes}")
31 .unwrap()
32 .progress_chars("#>-");
33 28
29 let style_spinner = ProgressStyle::with_template("{spinner:.green}{wide_msg}").unwrap();
30
31 let all = mp.add(ProgressBar::new(versions.len().try_into().unwrap()));
32 all.set_style(ProgressStyle::with_template("{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS));
33 all.set_message("Downloading");
34
34 for ver in versions { 35 for ver in versions {
35 let p = mp.add(ProgressBar::new(1)); 36 let p = mp.insert_before(&all, ProgressBar::new(1));
36 p.set_style(style.clone()); 37 p.set_style(style_spinner.clone());
37 js.spawn(download_version(config.clone(), list.clone(), ver, cached.clone(), p)); 38 js.spawn(download_version(config.clone(), list.clone(), ver, cached.clone(), p));
39 // std::thread::sleep(std::time::Duration::from_millis(200));
38 } 40 }
39 41
40 mp.clear().unwrap(); 42 while js.join_next().await.is_some() { all.inc(1) }
43
44 all.finish();
41 45
42 while js.join_next().await.is_some() {} 46 // mp.clear().unwrap();
43 47
44 Ok(()) 48 Ok(())
45} 49}
46 50
47async fn download_version(config: Cfg, list: List, version: Version, mut cached: HashMap<String, String>, progress: ProgressBar) -> MLE<()> { 51async fn download_version(config: Cfg, list: List, version: Version, mut cached: HashMap<String, String>, progress: ProgressBar) -> MLE<()> {
48 let project_info = mods_get_info(config.clone(), &version.project_id)?; 52 let project_info = mods_get_info(&config, &version.project_id)?;
49 53
50 let dl_path = String::from(&list.download_folder); 54 let dl_path = String::from(&list.download_folder);
51 55
52 progress.set_message(String::from(&version.id)); 56 progress.set_message(format!("{} - {}", project_info.title, version.id));
53 57
54 //Check cache if already downloaded 58 //Check cache if already downloaded
55 let c = cached.remove(&version.id); 59 let c = cached.remove(&version.id);
56 if c.is_some() { 60 if c.is_some() {
57 print!( 61 progress.set_message(format!("Get {} from cache", version.id));
58 "\t└({})Get version {} from cache",
59 project_info.title, version.id
60 );
61 //Force flush of stdout, else print! doesn't print instantly
62 std::io::stdout().flush()?;
63 copy_cached_version(&c.unwrap(), &dl_path); 62 copy_cached_version(&c.unwrap(), &dl_path);
64 println!(" ✓");
65 } else { 63 } else {
66 // print!("\t└({})Download version {}", project_info.title, version.id);
67 //Force flush of stdout, else print! doesn't print instantly
68 std::io::stdout().flush().unwrap();
69 let files = version.files; 64 let files = version.files;
70 let file = match files.clone().into_iter().find(|f| f.primary) { 65 let file = match files.clone().into_iter().find(|f| f.primary) {
71 Some(f) => f, 66 Some(f) => f,
@@ -91,19 +86,15 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached:
91 &progress 86 &progress
92 ) 87 )
93 .await?; 88 .await?;
94 // println!(" ✓"); 89
95 //Copy file to cache 90 progress.set_message(format!("Copy {} to cache", version.id));
96 // print!("\t └Copy to cache");
97 //Force flush of stdout, else print! doesn't print instantly
98 std::io::stdout().flush().unwrap();
99 let dl_path_file = format!("{}/{}", list.download_folder, filename); 91 let dl_path_file = format!("{}/{}", list.download_folder, filename);
100 let cache_path = format!("{}/{}", &config.clone().cache, filename); 92 let cache_path = format!("{}/{}", &config.clone().cache, filename);
101 // println!("{}:{}", dl_path_file, cache_path); 93
102 copy(dl_path_file, cache_path)?; 94 copy(dl_path_file, cache_path)?;
103 // println!(" ✓");
104 } 95 }
105 96
106 progress.finish_with_message(format!("✓{}", version.id)); 97 progress.finish_with_message(format!("✓{} - {}", project_info.title, version.id));
107 98
108 Ok(()) 99 Ok(())
109} 100}
@@ -114,8 +105,12 @@ async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar
114 105
115 let size = res.content_length().expect("Couldn't get content length"); 106 let size = res.content_length().expect("Couldn't get content length");
116 107
108 let style_bar_byte = ProgressStyle::with_template("{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]")
109 .unwrap()
110 .progress_chars(PROGRESS_CHARS);
111
117 progress.set_length(size); 112 progress.set_length(size);
118 // progress.set_style(ProgressStyle::with_template("{spinner:.green}{msg}\t[{wide_bar:.green/lime}] {bytes}/{total_bytes}").unwrap().progress_chars("#>-")); 113 progress.set_style(style_bar_byte);
119 114
120 // download chunks 115 // download chunks
121 let mut file = File::create(&dl_path_file)?; 116 let mut file = File::create(&dl_path_file)?;
@@ -124,7 +119,7 @@ async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar
124 let mut downloaded: u64 = 0; 119 let mut downloaded: u64 = 0;
125 120
126 while let Some(item) = stream.next().await { 121 while let Some(item) = stream.next().await {
127 progress.inc(1); 122 // progress.inc(1);
128 let chunk = item?; 123 let chunk = item?;
129 file.write_all(&chunk)?; 124 file.write_all(&chunk)?;
130 125