summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/download.rs31
-rw-r--r--src/commands/io.rs14
-rw-r--r--src/commands/list.rs8
-rw-r--r--src/commands/modification.rs62
-rw-r--r--src/commands/update.rs23
5 files changed, 75 insertions, 63 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs
index e9a96b5..7aa0156 100644
--- a/src/commands/download.rs
+++ b/src/commands/download.rs
@@ -1,6 +1,7 @@
1 1
2use indicatif::MultiProgress; 2use indicatif::{MultiProgress, ProgressStyle, ProgressBar};
3 3
4use crate::{STYLE_BAR_POS, PROGRESS_CHARS};
4use crate::{config::Cfg, List}; 5use crate::{config::Cfg, List};
5use crate::{ 6use crate::{
6 db::userlist_get_all_current_versions_with_mods, 7 db::userlist_get_all_current_versions_with_mods,
@@ -14,9 +15,12 @@ use crate::{
14pub async fn download(config: &Cfg, liststack: Vec<List>, clean: bool, delete_old: bool) -> MLE<()> { 15pub async fn download(config: &Cfg, liststack: Vec<List>, clean: bool, delete_old: bool) -> MLE<()> {
15 16
16 let mp = MultiProgress::new(); 17 let mp = MultiProgress::new();
18 let download_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap()));
19 download_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
17 20
18 for current_list in liststack { 21 for current_list in liststack {
19 println!("Downloading current versions of mods in {}", current_list.id); 22 download_p.set_message(format!("Download in {}", current_list.id));
23
20 let downloaded_versions = get_downloaded_versions(current_list.clone())?; 24 let downloaded_versions = get_downloaded_versions(current_list.clone())?;
21 let current_version_ids = match userlist_get_all_current_versions_with_mods( 25 let current_version_ids = match userlist_get_all_current_versions_with_mods(
22 config, 26 config,
@@ -59,24 +63,41 @@ pub async fn download(config: &Cfg, liststack: Vec<List>, clean: bool, delete_ol
59 config.clone(), 63 config.clone(),
60 get_raw_versions(&config.apis.modrinth, to_download).await, 64 get_raw_versions(&config.apis.modrinth, to_download).await,
61 &mp, 65 &mp,
62 None 66 &download_p,
63 ) 67 )
64 .await?; 68 .await?;
65 } else { 69 } else {
66 println!("There are no new versions to download"); 70 download_p.println(format!("There are no new versions to download for {}", current_list.id));
67 } 71 }
68 72
69 if !to_disable.is_empty() { 73 if !to_disable.is_empty() {
74 let d_p = mp.insert_before(&download_p, ProgressBar::new(to_disable.len().try_into().unwrap()));
75 d_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
70 for ver in to_disable { 76 for ver in to_disable {
71 if delete_old { 77 if delete_old {
72 // println!("Deleting version {} for mod {}", ver.1, ver.0); 78 d_p.set_message(format!("Delete version {}", ver.1));
79 d_p.inc(1);
73 delete_version(current_list.clone(), ver.1)?; 80 delete_version(current_list.clone(), ver.1)?;
74 } else { 81 } else {
82 d_p.set_message(format!("Disable version {}", ver.1));
83 d_p.inc(1);
75 disable_version(config, current_list.clone(), ver.1, ver.0)?; 84 disable_version(config, current_list.clone(), ver.1, ver.0)?;
76 }; 85 };
77 } 86 }
87
88 let del_msg = if delete_old {
89 "Deleted all old versions"
90 } else {
91 "Disabled all old versions"
92 };
93
94 d_p.finish_with_message(del_msg);
78 } 95 }
96
97 download_p.inc(1);
79 } 98 }
80 99
100 download_p.finish_with_message("Downloaded all lists");
101
81 Ok(()) 102 Ok(())
82} 103}
diff --git a/src/commands/io.rs b/src/commands/io.rs
index 43e642a..45e363e 100644
--- a/src/commands/io.rs
+++ b/src/commands/io.rs
@@ -39,18 +39,18 @@ struct ExportList {
39} 39}
40 40
41impl ExportList { 41impl ExportList {
42 pub fn from(config: &Cfg, list_id: String, download: bool) -> MLE<Self> { 42 pub fn from(config: &Cfg, list_id: &str, download: bool) -> MLE<Self> {
43 let list = lists_get(config, String::from(&list_id))?; 43 let list = lists_get(config, list_id)?;
44 44
45 let mut dl_folder = None; 45 let mut dl_folder = None;
46 if download { 46 if download {
47 dl_folder = Some(list.download_folder) 47 dl_folder = Some(list.download_folder)
48 }; 48 };
49 49
50 let mods = userlist_get_all_ids(config, &list_id)?; 50 let mods = userlist_get_all_ids(config, list_id)?;
51 let mut versions = vec![]; 51 let mut versions = vec![];
52 for m in mods { 52 for m in mods {
53 versions.push(ExportVersion::from(config, &list_id, &m)?) 53 versions.push(ExportVersion::from(config, list_id, &m)?)
54 } 54 }
55 55
56 Ok(Self { 56 Ok(Self {
@@ -68,11 +68,11 @@ pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> {
68 if list.is_none() { 68 if list.is_none() {
69 list_ids = lists_get_all_ids(config)?; 69 list_ids = lists_get_all_ids(config)?;
70 } else { 70 } else {
71 list_ids.push(lists_get(config, list.unwrap())?.id); 71 list_ids.push(lists_get(config, &list.unwrap())?.id);
72 } 72 }
73 let mut lists: Vec<ExportList> = vec![]; 73 let mut lists: Vec<ExportList> = vec![];
74 for list_id in list_ids { 74 for list_id in list_ids {
75 lists.push(ExportList::from(config, list_id, true)?); 75 lists.push(ExportList::from(config, &list_id, true)?);
76 } 76 }
77 77
78 let toml = toml::to_string(&Export { lists })?; 78 let toml = toml::to_string(&Export { lists })?;
@@ -85,7 +85,7 @@ pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> {
85 Ok(()) 85 Ok(())
86} 86}
87 87
88pub async fn import(config: &Cfg, file_str: String, direct_download: bool) -> MLE<()> { 88pub async fn import(config: &Cfg, file_str: &str, direct_download: bool) -> MLE<()> {
89 let mut file = File::open(file_str)?; 89 let mut file = File::open(file_str)?;
90 let mut content = String::new(); 90 let mut content = String::new();
91 file.read_to_string(&mut content)?; 91 file.read_to_string(&mut content)?;
diff --git a/src/commands/list.rs b/src/commands/list.rs
index 95f9927..52f14f2 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -18,7 +18,7 @@ pub struct List {
18 18
19pub fn get_current_list(config: &Cfg) -> MLE<List> { 19pub fn get_current_list(config: &Cfg) -> MLE<List> {
20 let id = config_get_current_list(config)?; 20 let id = config_get_current_list(config)?;
21 lists_get(config, id) 21 lists_get(config, &id)
22} 22}
23 23
24pub fn list_add( 24pub fn list_add(
@@ -52,7 +52,7 @@ pub fn list_remove(config: &Cfg, id: String) -> MLE<()> {
52/// * `args` - All args, to extract the new version 52/// * `args` - All args, to extract the new version
53pub async fn list_version( 53pub async fn list_version(
54 config: &Cfg, 54 config: &Cfg,
55 id: String, 55 id: &str,
56 mc_version: String, 56 mc_version: String,
57 download: bool, 57 download: bool,
58 delete: bool, 58 delete: bool,
@@ -62,7 +62,7 @@ pub async fn list_version(
62 id, mc_version 62 id, mc_version
63 ); 63 );
64 64
65 lists_version(config, &id, &mc_version)?; 65 lists_version(config, id, &mc_version)?;
66 66
67 println!( 67 println!(
68 "\nCheck for updates for new minecraft version in list {}", 68 "\nCheck for updates for new minecraft version in list {}",
@@ -75,7 +75,7 @@ pub async fn list_version(
75pub fn list_list(config: &Cfg) -> MLE<()> { 75pub fn list_list(config: &Cfg) -> MLE<()> {
76 let lists = lists_get_all_ids(config)?; 76 let lists = lists_get_all_ids(config)?;
77 for list in lists { 77 for list in lists {
78 let l = lists_get(config, list)?; 78 let l = lists_get(config, &list)?;
79 println!("{}: | {} | {}", l.id, l.mc_version, l.modloader) 79 println!("{}: | {} | {}", l.id, l.mc_version, l.modloader)
80 } 80 }
81 Ok(()) 81 Ok(())
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index d369c4b..8abf913 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -11,7 +11,7 @@ use crate::{
11 error::{ErrorType, MLError, MLE}, 11 error::{ErrorType, MLError, MLE},
12 files::{delete_version, download_versions}, 12 files::{delete_version, download_versions},
13 modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, 13 modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version},
14 List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_SPINNER, 14 List, PROGRESS_CHARS, STYLE_BAR_POS,
15}; 15};
16 16
17#[derive(Debug)] 17#[derive(Debug)]
@@ -43,57 +43,49 @@ pub async fn mod_add(
43 list: List, 43 list: List,
44 direct_download: bool, 44 direct_download: bool,
45) -> MLE<()> { 45) -> MLE<()> {
46 let mp = MultiProgress::new();
46 47
47 //TODO MultiProgress
48
49 let spinner_style = ProgressStyle::with_template(STYLE_SPINNER).unwrap();
50 let bar_style = ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS);
51 let mut mod_ids: Vec<(String, bool)> = Vec::new(); 48 let mut mod_ids: Vec<(String, bool)> = Vec::new();
52 let mut ver_ids: Vec<(String, bool)> = Vec::new(); 49 let mut ver_ids: Vec<(String, bool)> = Vec::new();
53 50
54 let p = ProgressBar::new(mods.len().try_into().unwrap()); 51 let add_p = mp.add(ProgressBar::new(mods.len().try_into().unwrap()));
55 p.set_style(spinner_style.clone()); 52 add_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
56 p.set_message("Sort ids"); 53 add_p.set_message("Sort ids");
57 54
58 //"Sort" project ids from version ids to be able to handle them differently but in a batch 55 //"Sort" project ids from version ids to be able to handle them differently but in a batch
59 for m in mods { 56 for m in mods {
60 p.inc(1); 57 add_p.inc(1);
61 match m.id { 58 match m.id {
62 IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), 59 IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)),
63 IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), 60 IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)),
64 } 61 }
65 } 62 }
63
64 add_p.set_message("Get infos");
66 65
67 p.finish_with_message("Sort ids done");
68
69 let info_p = ProgressBar::new(2);
70 info_p.set_message("Get infos");
71 info_p.set_style(bar_style.clone());
72 let mut projectinfo: Vec<ProjectInfo> = Vec::new(); 66 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
73 if !mod_ids.is_empty() { 67 if !mod_ids.is_empty() {
74 projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?); 68 projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?);
75 info_p.inc(1);
76 }; 69 };
77 if !ver_ids.is_empty() { 70 if !ver_ids.is_empty() {
78 projectinfo.append(&mut get_ver_info(config, ver_ids).await?); 71 projectinfo.append(&mut get_ver_info(config, ver_ids).await?);
79 info_p.inc(1);
80 }; 72 };
81 73
82 info_p.finish_with_message("Get infos done");
83
84 if projectinfo.is_empty() { 74 if projectinfo.is_empty() {
85 return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); 75 return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?"));
86 }; 76 };
87 77
78 add_p.set_message("Add mods to database");
79
88 let mut downloadstack: Vec<Version> = Vec::new(); 80 let mut downloadstack: Vec<Version> = Vec::new();
89 81
90 //Adding each mod to the lists and downloadstack 82 //Adding each mod to the lists and downloadstack
91 let add_p = ProgressBar::new(projectinfo.len().try_into().unwrap()); 83 let project_p = mp.insert_before(&add_p, ProgressBar::new(projectinfo.len().try_into().unwrap()));
92 add_p.set_style(bar_style); 84 project_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
93 85
94 for project in projectinfo { 86 for project in projectinfo {
95 87
96 add_p.set_message(format!("Add {}", project.title)); 88 project_p.set_message(format!("Add {}", project.title));
97 89
98 let current_version_id = if project.current_version.is_none() { 90 let current_version_id = if project.current_version.is_none() {
99 String::from("NONE") 91 String::from("NONE")
@@ -144,18 +136,19 @@ pub async fn mod_add(
144 downloadstack.push(project.current_version.unwrap()) 136 downloadstack.push(project.current_version.unwrap())
145 }; 137 };
146 138
147 // add_p.println(format!("Added {}", project.title)); 139 project_p.inc(1);
148 add_p.inc(1);
149 } 140 }
150 141
151 add_p.finish_with_message("Added all mods"); 142 project_p.finish_with_message("Added all mods to the database");
152 143
153 //Download all the added mods 144 //Download all the added mods
154 if direct_download { 145 if direct_download {
155 let mp = MultiProgress::new(); 146 add_p.set_message("Download mods");
156 download_versions(list.clone(), config.clone(), downloadstack, &mp, None).await?; 147 download_versions(list.clone(), config.clone(), downloadstack, &mp, &add_p).await?;
157 }; 148 };
158 149
150 add_p.finish_with_message("Added all mods");
151
159 Ok(()) 152 Ok(())
160} 153}
161 154
@@ -277,14 +270,17 @@ async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<Pro
277 270
278 for (i, project) in v_projects.into_iter().enumerate() { 271 for (i, project) in v_projects.into_iter().enumerate() {
279 let version = &v_versions[i]; 272 let version = &v_versions[i];
280 // println!("\tâ””{}({})", project.title, version.id); 273
281 let file = version 274 let files = version
282 .clone() 275 .clone()
283 .files 276 .files;
284 .into_iter() 277
285 .find(|f| f.primary) 278 let file = match files.clone().into_iter().find(|f| f.primary) {
286 .unwrap() 279 Some(f) => f,
280 None => { files[0].clone() }
281 }
287 .url; 282 .url;
283
288 projectinfo.push(ProjectInfo { 284 projectinfo.push(ProjectInfo {
289 mod_id: String::from(&project.id), 285 mod_id: String::from(&project.id),
290 slug: project.slug, 286 slug: project.slug,
diff --git a/src/commands/update.rs b/src/commands/update.rs
index bde6896..194bbe5 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -9,7 +9,7 @@ use crate::{
9 error::{ErrorType, MLError, MLE}, 9 error::{ErrorType, MLError, MLE},
10 files::{clean_list_dir, delete_version, disable_version, download_versions}, 10 files::{clean_list_dir, delete_version, disable_version, download_versions},
11 modrinth::{extract_current_version, versions, Version}, 11 modrinth::{extract_current_version, versions, Version},
12 List, PROGRESS_CHARS, STYLE_BAR_POS, 12 List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION,
13}; 13};
14 14
15pub async fn update( 15pub async fn update(
@@ -23,23 +23,21 @@ pub async fn update(
23 let mp = MultiProgress::new(); 23 let mp = MultiProgress::new();
24 24
25 let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); 25 let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap()));
26 let bar_style = ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS); 26 update_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
27 update_p.set_style(bar_style.clone());
28 update_p.set_message("Update");
29 27
30 for current_list in liststack { 28 for current_list in liststack {
29 update_p.set_message(format!("Update {}", current_list.id));
30
31 let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); 31 let list_p = mp.insert_before(&update_p, ProgressBar::new(2));
32 list_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); 32 list_p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap());
33 list_p.set_message(format!("Update {}", current_list.id)); 33 list_p.set_message("Update mods");
34 34
35 let mods = userlist_get_all_ids(config, &current_list.id)?; 35 let mods = userlist_get_all_ids(config, &current_list.id)?;
36 36
37 let list_u_p = mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into().unwrap())); 37 let list_u_p = mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into().unwrap()));
38 list_u_p.set_style(bar_style.clone()); 38 list_u_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
39 list_u_p.set_message(format!("Update {}", current_list.id));
40 39
41 let mut current_versions: Vec<(String, String)> = vec![]; 40 let mut current_versions: Vec<(String, String)> = vec![];
42
43 let mut updatestack: Vec<Version> = vec![]; 41 let mut updatestack: Vec<Version> = vec![];
44 42
45 for id in mods { 43 for id in mods {
@@ -84,7 +82,6 @@ pub async fn update(
84 } 82 }
85 83
86 list_u_p.finish_with_message(format!("Updated mods in {}", current_list.id)); 84 list_u_p.finish_with_message(format!("Updated mods in {}", current_list.id));
87 list_p.inc(1);
88 85
89 if clean { 86 if clean {
90 list_p.set_message("Cleaning"); 87 list_p.set_message("Cleaning");
@@ -92,12 +89,12 @@ pub async fn update(
92 }; 89 };
93 90
94 if direct_download && !updatestack.is_empty() { 91 if direct_download && !updatestack.is_empty() {
95 download_versions(current_list.clone(), config.clone(), updatestack, &mp, Some(&list_p)).await?; 92 download_versions(current_list.clone(), config.clone(), updatestack, &mp, &list_p).await?;
96 93
97 //Disable old versions 94 //Disable old versions
98 if !clean { 95 if !clean {
99 let d_p = mp.insert_before(&list_p, ProgressBar::new(current_versions.len().try_into().unwrap())); 96 let d_p = mp.insert_before(&list_p, ProgressBar::new(current_versions.len().try_into().unwrap()));
100 d_p.set_style(bar_style.clone()); 97 d_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS));
101 for ver in current_versions { 98 for ver in current_versions {
102 if delete_old { 99 if delete_old {
103 d_p.set_message(format!("Delete version {}", ver.0)); 100 d_p.set_message(format!("Delete version {}", ver.0));
@@ -119,14 +116,12 @@ pub async fn update(
119 d_p.finish_with_message(del_msg); 116 d_p.finish_with_message(del_msg);
120 } 117 }
121 }; 118 };
122 list_p.inc(1);
123 list_p.finish_with_message(format!("Updated {}", current_list.id)); 119 list_p.finish_with_message(format!("Updated {}", current_list.id));
124 update_p.inc(1); 120 update_p.inc(1);
125 } 121 }
126 122
127 update_p.finish_with_message("Updated all lists"); 123 update_p.finish_with_message("Updated all lists");
128 124
129
130 Ok(()) 125 Ok(())
131} 126}
132 127