diff options
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index d4c49d6..730583d 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | use std::{io::Write, collections::HashMap}; | 1 | use std::{io::Write, collections::HashMap}; |
2 | 2 | ||
3 | use indicatif::{ProgressBar, ProgressStyle}; | ||
4 | |||
3 | use crate::{ | 5 | use crate::{ |
4 | config::Cfg, | 6 | config::Cfg, |
5 | db::{ | 7 | db::{ |
@@ -12,6 +14,8 @@ use crate::{ | |||
12 | List, | 14 | List, |
13 | }; | 15 | }; |
14 | 16 | ||
17 | const PROGRESS_CHARS: &str = "#>-"; | ||
18 | |||
15 | #[derive(Debug, Clone)] | 19 | #[derive(Debug, Clone)] |
16 | pub struct AddMod { | 20 | pub struct AddMod { |
17 | pub id: IDSelector, | 21 | pub id: IDSelector, |
@@ -41,28 +45,45 @@ pub async fn mod_add( | |||
41 | list: List, | 45 | list: List, |
42 | direct_download: bool, | 46 | direct_download: bool, |
43 | ) -> MLE<()> { | 47 | ) -> MLE<()> { |
44 | println!("Add mods to {}", list.id); | 48 | let spinner_style = ProgressStyle::with_template("{spinner:.green}{msg}").unwrap(); |
45 | println!(" └Add mods:"); | 49 | let bar_style = ProgressStyle::with_template("{spinner:.green}{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS); |
50 | |||
51 | // println!("Add mods to {}", list.id); | ||
52 | // println!(" └Add mods:"); | ||
46 | 53 | ||
47 | let mut mod_ids: Vec<(String, bool)> = Vec::new(); | 54 | let mut mod_ids: Vec<(String, bool)> = Vec::new(); |
48 | let mut ver_ids: Vec<(String, bool)> = Vec::new(); | 55 | let mut ver_ids: Vec<(String, bool)> = Vec::new(); |
49 | 56 | ||
57 | let p = ProgressBar::new(mods.len().try_into().unwrap()); | ||
58 | p.set_style(spinner_style.clone()); | ||
59 | p.set_message("Sort ids"); | ||
60 | |||
50 | //"Sort" project ids from version ids to be able to handle them differently but in a batch | 61 | //"Sort" project ids from version ids to be able to handle them differently but in a batch |
51 | for m in mods { | 62 | for m in mods { |
63 | p.inc(1); | ||
52 | match m.id { | 64 | match m.id { |
53 | IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), | 65 | IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), |
54 | IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), | 66 | IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), |
55 | } | 67 | } |
56 | } | 68 | } |
57 | 69 | ||
70 | p.finish_with_message("Sort ids done"); | ||
71 | |||
72 | let info_p = ProgressBar::new(2); | ||
73 | info_p.set_message("Get infos"); | ||
74 | info_p.set_style(bar_style.clone()); | ||
58 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); | 75 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); |
59 | if !mod_ids.is_empty() { | 76 | if !mod_ids.is_empty() { |
60 | projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) | 77 | projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?); |
78 | info_p.inc(1); | ||
61 | }; | 79 | }; |
62 | if !ver_ids.is_empty() { | 80 | if !ver_ids.is_empty() { |
63 | projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) | 81 | projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?); |
82 | info_p.inc(1); | ||
64 | }; | 83 | }; |
65 | 84 | ||
85 | info_p.finish_with_message("Get infos done"); | ||
86 | |||
66 | if projectinfo.is_empty() { | 87 | if projectinfo.is_empty() { |
67 | return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); | 88 | return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); |
68 | }; | 89 | }; |
@@ -70,18 +91,19 @@ pub async fn mod_add( | |||
70 | let mut downloadstack: Vec<Version> = Vec::new(); | 91 | let mut downloadstack: Vec<Version> = Vec::new(); |
71 | 92 | ||
72 | //Adding each mod to the lists and downloadstack | 93 | //Adding each mod to the lists and downloadstack |
73 | if projectinfo.len() == 1 { | 94 | let add_p = ProgressBar::new(projectinfo.len().try_into().unwrap()); |
74 | println!(" └Insert mod in list {} and save infos", list.id); | 95 | add_p.set_style(bar_style); |
75 | } else { | ||
76 | println!(" └Insert mods in list {} and save infos", list.id); | ||
77 | } | ||
78 | 96 | ||
79 | for project in projectinfo { | 97 | for project in projectinfo { |
98 | |||
99 | add_p.set_message(format!("Add {}", project.title)); | ||
100 | |||
80 | let current_version_id = if project.current_version.is_none() { | 101 | let current_version_id = if project.current_version.is_none() { |
81 | String::from("NONE") | 102 | String::from("NONE") |
82 | } else { | 103 | } else { |
83 | project.current_version.clone().unwrap().id | 104 | project.current_version.clone().unwrap().id |
84 | }; | 105 | }; |
106 | |||
85 | match userlist_insert( | 107 | match userlist_insert( |
86 | config.clone(), | 108 | config.clone(), |
87 | &list.id, | 109 | &list.id, |
@@ -124,8 +146,13 @@ pub async fn mod_add( | |||
124 | if project.current_version.is_some() { | 146 | if project.current_version.is_some() { |
125 | downloadstack.push(project.current_version.unwrap()) | 147 | downloadstack.push(project.current_version.unwrap()) |
126 | }; | 148 | }; |
149 | |||
150 | // add_p.println(format!("Added {}", project.title)); | ||
151 | add_p.inc(1); | ||
127 | } | 152 | } |
128 | 153 | ||
154 | add_p.finish_with_message("Added all mods"); | ||
155 | |||
129 | //Download all the added mods | 156 | //Download all the added mods |
130 | if direct_download { | 157 | if direct_download { |
131 | download_versions(list.clone(), config.clone(), downloadstack).await?; | 158 | download_versions(list.clone(), config.clone(), downloadstack).await?; |
@@ -140,7 +167,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> | |||
140 | 167 | ||
141 | let mut ids = vec![]; | 168 | let mut ids = vec![]; |
142 | 169 | ||
143 | println!("{:?}", mod_ids); | 170 | // println!("{:?}", mod_ids); |
144 | 171 | ||
145 | for id in mod_ids { | 172 | for id in mod_ids { |
146 | setmap.insert(id.0.to_string(), id.1); | 173 | setmap.insert(id.0.to_string(), id.1); |
@@ -156,8 +183,8 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> | |||
156 | _ => panic!("PANIC"), | 183 | _ => panic!("PANIC"), |
157 | }; | 184 | }; |
158 | for project in m_projects { | 185 | for project in m_projects { |
159 | println!("\t└{}", project.title); | 186 | // println!("\t└{}", project.title); |
160 | println!("\t └Get versions"); | 187 | // println!("\t └Get versions"); |
161 | let available_versions = versions( | 188 | let available_versions = versions( |
162 | &config.apis.modrinth, | 189 | &config.apis.modrinth, |
163 | String::from(&project.id), | 190 | String::from(&project.id), |
@@ -170,7 +197,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> | |||
170 | let file: String; | 197 | let file: String; |
171 | if !available_versions.is_empty() { | 198 | if !available_versions.is_empty() { |
172 | let current_id = extract_current_version(available_versions.clone())?; | 199 | let current_id = extract_current_version(available_versions.clone())?; |
173 | println!("\t └Current version: {}", current_id); | 200 | // println!("\t └Current version: {}", current_id); |
174 | 201 | ||
175 | current_version = Some( | 202 | current_version = Some( |
176 | available_versions | 203 | available_versions |
@@ -197,7 +224,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> | |||
197 | available_versions_vec.push(ver.id); | 224 | available_versions_vec.push(ver.id); |
198 | } | 225 | } |
199 | 226 | ||
200 | println!("{:?}", setmap); | 227 | // println!("{:?}", setmap); |
201 | 228 | ||
202 | projectinfo.push(ProjectInfo { | 229 | projectinfo.push(ProjectInfo { |
203 | mod_id: String::from(&project.id), | 230 | mod_id: String::from(&project.id), |
@@ -206,10 +233,10 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> | |||
206 | current_version, | 233 | current_version, |
207 | applicable_versions: available_versions_vec, | 234 | applicable_versions: available_versions_vec, |
208 | download_link: file, | 235 | download_link: file, |
209 | set_version: setmap.get(&project.slug).unwrap().clone(), | 236 | set_version: *setmap.get(&project.slug).unwrap(), |
210 | }) | 237 | }) |
211 | } else { | 238 | } else { |
212 | println!("\t └There's currently no mod version for your specified target"); | 239 | // println!("\t └There's currently no mod version for your specified target"); |
213 | current_version = None; | 240 | current_version = None; |
214 | file = String::from("NONE"); | 241 | file = String::from("NONE"); |
215 | available_versions_vec.push(String::from("NONE")); | 242 | available_versions_vec.push(String::from("NONE")); |
@@ -220,7 +247,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> | |||
220 | current_version, | 247 | current_version, |
221 | applicable_versions: available_versions_vec, | 248 | applicable_versions: available_versions_vec, |
222 | download_link: file, | 249 | download_link: file, |
223 | set_version: setmap.get(&project.id).unwrap().clone(), | 250 | set_version: *setmap.get(&project.id).unwrap(), |
224 | }) | 251 | }) |
225 | } | 252 | } |
226 | } | 253 | } |
@@ -252,7 +279,7 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<Proj | |||
252 | 279 | ||
253 | for (i, project) in v_projects.into_iter().enumerate() { | 280 | for (i, project) in v_projects.into_iter().enumerate() { |
254 | let version = &v_versions[i]; | 281 | let version = &v_versions[i]; |
255 | println!("\t└{}({})", project.title, version.id); | 282 | // println!("\t└{}({})", project.title, version.id); |
256 | let file = version | 283 | let file = version |
257 | .clone() | 284 | .clone() |
258 | .files | 285 | .files |
@@ -267,7 +294,7 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<Proj | |||
267 | current_version: Some(version.clone()), | 294 | current_version: Some(version.clone()), |
268 | applicable_versions: vec![String::from(&version.id)], | 295 | applicable_versions: vec![String::from(&version.id)], |
269 | download_link: file, | 296 | download_link: file, |
270 | set_version: setmap.get(&version.id).unwrap().clone(), | 297 | set_version: *setmap.get(&version.id).unwrap(), |
271 | }) | 298 | }) |
272 | } | 299 | } |
273 | Ok(projectinfo) | 300 | Ok(projectinfo) |
@@ -282,7 +309,7 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<Proj | |||
282 | pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { | 309 | pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { |
283 | let mod_id = mods_get_id(&config.data, id)?; | 310 | let mod_id = mods_get_id(&config.data, id)?; |
284 | 311 | ||
285 | println!("Remove mod {} from {}", mods_get_info(config.clone(), &mod_id)?.title, list.id); | 312 | println!("Remove mod {} from {}", mods_get_info(&config, &mod_id)?.title, list.id); |
286 | let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; | 313 | let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; |
287 | 314 | ||
288 | print!(" └Remove from list"); | 315 | print!(" └Remove from list"); |
@@ -300,7 +327,6 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { | |||
300 | if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { | 327 | if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { |
301 | return Err(err); | 328 | return Err(err); |
302 | }; | 329 | }; |
303 | () | ||
304 | }, | 330 | }, |
305 | }; | 331 | }; |
306 | println!(" ✓"); | 332 | println!(" ✓"); |