From 2d7e0a2fbf1c8a4187e2bf3fdcd592631ab273a0 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 26 May 2023 17:40:27 +0200 Subject: added full progress? cargo fmt --- src/commands/io.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'src/commands/io.rs') diff --git a/src/commands/io.rs b/src/commands/io.rs index 45e363e..2501583 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -1,12 +1,16 @@ +use indicatif::{ProgressBar, ProgressStyle}; use serde::{Deserialize, Serialize}; use std::fs::File; use std::io::prelude::*; use crate::{ config::Cfg, - db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_set_version, userlist_get_all_ids, userlist_get_current_version}, + db::{ + lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, + userlist_get_current_version, userlist_get_set_version, + }, error::MLE, - mod_add, IDSelector, List, Modloader, AddMod, + mod_add, AddMod, IDSelector, List, Modloader, STYLE_OPERATION, }; #[derive(Debug, Serialize, Deserialize)] @@ -17,14 +21,14 @@ struct Export { #[derive(Debug, Serialize, Deserialize)] struct ExportVersion { version: String, - set: bool + set: bool, } impl ExportVersion { fn from(config: &Cfg, list_id: &str, mod_id: &str) -> MLE { Ok(Self { version: userlist_get_current_version(config, list_id, mod_id)?, - set: userlist_get_set_version(config, list_id, mod_id)? + set: userlist_get_set_version(config, list_id, mod_id)?, }) } } @@ -64,23 +68,36 @@ impl ExportList { } pub fn export(config: &Cfg, list: Option) -> MLE<()> { + let progress = ProgressBar::new_spinner(); + progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + let mut list_ids: Vec = vec![]; if list.is_none() { list_ids = lists_get_all_ids(config)?; } else { list_ids.push(lists_get(config, &list.unwrap())?.id); } + let mut lists: Vec = vec![]; for list_id in list_ids { + progress.set_message(format!("Export {}", list_id)); + //TODO download option/ new download on import lists.push(ExportList::from(config, &list_id, true)?); } let toml = toml::to_string(&Export { lists })?; - let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); + let filestr = dirs::home_dir() + .unwrap() + .join("mlexport.toml") + .into_os_string() + .into_string() + .unwrap(); - let mut file = File::create(filestr.into_os_string().into_string().unwrap().as_str())?; + progress.set_message("Create file"); + let mut file = File::create(&filestr)?; file.write_all(toml.as_bytes())?; + progress.finish_with_message(format!("Exported to {}", filestr)); Ok(()) } @@ -108,7 +125,10 @@ pub async fn import(config: &Cfg, file_str: &str, direct_download: bool) -> MLE< let mut ver_ids = vec![]; for id in exportlist.versions { - ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} ); + ver_ids.push(AddMod { + id: IDSelector::VersionID(id.version), + set_version: id.set, + }); } mod_add(config, ver_ids, list, direct_download).await?; } -- cgit v1.2.3