summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/download.rs4
-rw-r--r--src/commands/io.rs2
-rw-r--r--src/commands/list.rs2
-rw-r--r--src/commands/modification.rs70
-rw-r--r--src/commands/update.rs79
5 files changed, 109 insertions, 48 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs
index ebfb4eb..6831714 100644
--- a/src/commands/download.rs
+++ b/src/commands/download.rs
@@ -1,3 +1,4 @@
1
1use crate::{config::Cfg, List}; 2use crate::{config::Cfg, List};
2use crate::{ 3use crate::{
3 db::userlist_get_all_current_versions_with_mods, 4 db::userlist_get_all_current_versions_with_mods,
@@ -9,7 +10,6 @@ use crate::{
9}; 10};
10 11
11pub async fn download(config: Cfg, liststack: Vec<List>, clean: bool, delete_old: bool) -> MLE<()> { 12pub async fn download(config: Cfg, liststack: Vec<List>, clean: bool, delete_old: bool) -> MLE<()> {
12
13 for current_list in liststack { 13 for current_list in liststack {
14 println!("Downloading current versions of mods in {}", current_list.id); 14 println!("Downloading current versions of mods in {}", current_list.id);
15 let downloaded_versions = get_downloaded_versions(current_list.clone())?; 15 let downloaded_versions = get_downloaded_versions(current_list.clone())?;
@@ -63,7 +63,7 @@ pub async fn download(config: Cfg, liststack: Vec<List>, clean: bool, delete_old
63 if !to_disable.is_empty() { 63 if !to_disable.is_empty() {
64 for ver in to_disable { 64 for ver in to_disable {
65 if delete_old { 65 if delete_old {
66 println!("Deleting version {} for mod {}", ver.1, ver.0); 66 // println!("Deleting version {} for mod {}", ver.1, ver.0);
67 delete_version(current_list.clone(), ver.1)?; 67 delete_version(current_list.clone(), ver.1)?;
68 } else { 68 } else {
69 disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; 69 disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?;
diff --git a/src/commands/io.rs b/src/commands/io.rs
index dd294bc..2a26f1d 100644
--- a/src/commands/io.rs
+++ b/src/commands/io.rs
@@ -24,7 +24,7 @@ impl ExportVersion {
24 fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE<Self> { 24 fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE<Self> {
25 Ok(Self { 25 Ok(Self {
26 version: userlist_get_current_version(config.clone(), list_id, mod_id)?, 26 version: userlist_get_current_version(config.clone(), list_id, mod_id)?,
27 set: userlist_get_set_version(config.clone(), list_id, mod_id)? 27 set: userlist_get_set_version(config, list_id, mod_id)?
28 }) 28 })
29 } 29 }
30} 30}
diff --git a/src/commands/list.rs b/src/commands/list.rs
index 4aa4306..c07823b 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -32,7 +32,7 @@ pub fn list_add(
32} 32}
33 33
34pub fn list_change(config: Cfg, id: String) -> MLE<()> { 34pub fn list_change(config: Cfg, id: String) -> MLE<()> {
35 if lists_get_all_ids(config.clone())?.into_iter().find(|l| l == &id).is_none() { 35 if !lists_get_all_ids(config.clone())?.into_iter().any(|l| l == id) {
36 return Err(MLError::new(ErrorType::ArgumentError, "List not found")); 36 return Err(MLError::new(ErrorType::ArgumentError, "List not found"));
37 }; 37 };
38 println!("Change default list to: {}", id); 38 println!("Change default list to: {}", id);
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 @@
1use std::{io::Write, collections::HashMap}; 1use std::{io::Write, collections::HashMap};
2 2
3use indicatif::{ProgressBar, ProgressStyle};
4
3use crate::{ 5use 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
17const PROGRESS_CHARS: &str = "#>-";
18
15#[derive(Debug, Clone)] 19#[derive(Debug, Clone)]
16pub struct AddMod { 20pub 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
282pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { 309pub 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!(" ✓");
diff --git a/src/commands/update.rs b/src/commands/update.rs
index d3a282b..2de13f3 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,3 +1,5 @@
1use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
2
1use crate::{ 3use crate::{
2 config::Cfg, 4 config::Cfg,
3 db::{ 5 db::{
@@ -10,6 +12,8 @@ use crate::{
10 List, 12 List,
11}; 13};
12 14
15const PROGRESS_CHARS: &str = "#>-";
16
13pub async fn update( 17pub async fn update(
14 config: Cfg, 18 config: Cfg,
15 liststack: Vec<List>, 19 liststack: Vec<List>,
@@ -17,20 +21,39 @@ pub async fn update(
17 direct_download: bool, 21 direct_download: bool,
18 delete_old: bool, 22 delete_old: bool,
19) -> MLE<()> { 23) -> MLE<()> {
24
25 let mp = MultiProgress::new();
26
27 let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap()));
28 let bar_style = ProgressStyle::with_template("{spinner:.green}{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS);
29 let spinner_style = ProgressStyle::with_template("{spinner:.green}{msg}").unwrap();
30 update_p.set_style(bar_style.clone());
31 update_p.set_message("Update");
32
20 for current_list in liststack { 33 for current_list in liststack {
21 println!("Update mods in {}", current_list.id); 34
35 // println!("Update mods in {}", current_list.id);
22 let mods = userlist_get_all_ids(config.clone(), &current_list.id)?; 36 let mods = userlist_get_all_ids(config.clone(), &current_list.id)?;
23 37
38 let list_p = mp.insert_before(&update_p, ProgressBar::new(mods.len().try_into().unwrap()));
39 list_p.set_style(bar_style.clone());
40 list_p.set_message(format!("Update {}", current_list.id));
41
24 let mut current_versions: Vec<(String, String)> = vec![]; 42 let mut current_versions: Vec<(String, String)> = vec![];
25 43
26 let mut updatestack: Vec<Version> = vec![]; 44 let mut updatestack: Vec<Version> = vec![];
27 45
28 for id in mods { 46 for id in mods {
29 let info = mods_get_info(config.clone(), &id)?; 47 let mod_p = mp.insert_before(&list_p, ProgressBar::new(1));
30 println!(" ├{}", info.title); 48 mod_p.set_style(spinner_style.clone());
49
50 let info = mods_get_info(&config, &id)?;
51 mod_p.set_message(format!("Update {}", info.title));
52 // println!(" ├{}", info.title);
31 53
32 if userlist_get_set_version(config.clone(), &current_list.id, &id)? { 54 if userlist_get_set_version(config.clone(), &current_list.id, &id)? {
33 println!(" │ └Set version, skipping update"); 55 // println!(" │ └Set version, skipping update");
56 list_p.inc(1);
34 continue; 57 continue;
35 } 58 }
36 59
@@ -38,12 +61,15 @@ pub async fn update(
38 let disable_version = 61 let disable_version =
39 userlist_get_current_version(config.clone(), &current_list.id, &id)?; 62 userlist_get_current_version(config.clone(), &current_list.id, &id)?;
40 63
64 mod_p.inc(1);
65
41 updatestack.push( 66 updatestack.push(
42 match specific_update( 67 match specific_update(
43 config.clone(), 68 config.clone(),
44 clean, 69 clean,
45 current_list.clone(), 70 current_list.clone(),
46 String::from(&id), 71 String::from(&id),
72 &mod_p
47 ) 73 )
48 .await 74 .await
49 { 75 {
@@ -53,19 +79,25 @@ pub async fn update(
53 } 79 }
54 Err(e) => { 80 Err(e) => {
55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { 81 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
56 println!( 82 // println!(
57 " │ └No new version found for the specified minecraft version" 83 // " │ └No new version found for the specified minecraft version"
58 ); 84 // );
59 } else { 85 } else {
60 return Err(e); 86 return Err(e);
61 }; 87 };
88 list_p.inc(1);
62 continue; 89 continue;
63 } 90 }
64 }, 91 },
65 ) 92 );
93 list_p.inc(1);
66 } 94 }
67 95
96 list_p.finish_with_message(format!("Updated {}", current_list.id));
97
68 if clean { 98 if clean {
99 update_p.set_message("Cleaning");
100 update_p.inc(1);
69 clean_list_dir(&current_list)?; 101 clean_list_dir(&current_list)?;
70 }; 102 };
71 103
@@ -85,12 +117,16 @@ pub async fn update(
85 } 117 }
86 } 118 }
87 }; 119 };
120 update_p.inc(1);
88 } 121 }
89 122
123 update_p.finish_with_message("Updated all lists");
124
125
90 Ok(()) 126 Ok(())
91} 127}
92 128
93async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> { 129async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progress: &ProgressBar) -> MLE<Version> {
94 let applicable_versions = 130 let applicable_versions =
95 versions(&config.apis.modrinth, String::from(&id), list.clone()).await; 131 versions(&config.apis.modrinth, String::from(&id), list.clone()).await;
96 132
@@ -116,10 +152,11 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
116 let current_str = extract_current_version(applicable_versions.clone())?; 152 let current_str = extract_current_version(applicable_versions.clone())?;
117 153
118 if clean { 154 if clean {
119 println!("\t └Add version to downloadstack"); 155 // println!("\t └Add version to downloadstack");
120 } else { 156 } else {
121 println!("\t └Get versions for specified minecraft versions"); 157 progress.println(format!("Found new version for {}", mods_get_info(&config, &id).unwrap().title));
122 println!("\t └New current version: {}", current_str); 158 // println!("\t └Get versions for specified minecraft versions");
159 // println!("\t └New current version: {}", current_str);
123 }; 160 };
124 161
125 //get new versions 162 //get new versions
@@ -133,16 +170,14 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
133 }?; 170 }?;
134 current.push(current_ver.clone()); 171 current.push(current_ver.clone());
135 172
136 let link = match current_ver 173 let files = &current_ver.files;
137 .files 174
138 .into_iter() 175 let link = match files.clone().into_iter().find(|f| f.primary) {
139 .find(|f| f.primary) 176 Some(f) => f,
140 .ok_or("!no primary in links") 177 None => { files[0].clone() }
141 { 178 }
142 Ok(p) => Ok(p), 179 .url;
143 Err(e) => Err(MLError::new(ErrorType::Other, e)), 180
144 }?
145 .url;
146 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; 181 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?;
147 } 182 }
148 183