From 846b3d0edf0b5c6d903a022d6d13726f81e748e7 Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Mon, 3 Jun 2024 13:51:59 +0200 Subject: [PATCH] Added the list and remove command --- src/commands/list.rs | 20 +++++++----- src/commands/mod.rs | 1 + src/commands/remove.rs | 17 ++++++++++ src/main.rs | 3 +- src/modrinth/versions.rs | 12 +++---- src/pack.rs | 68 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 src/commands/remove.rs create mode 100644 src/pack.rs diff --git a/src/commands/list.rs b/src/commands/list.rs index 41c8a66..c349837 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,17 +1,23 @@ -use crate::{config, modrinth::project::get_multiple_projects}; +use crate::{config, pack::Modpack}; use colored::*; use comfy_table::{presets::NOTHING, Table}; pub async fn list() { let config = config::Config::get().unwrap(); - let projects = get_multiple_projects(config.projects.keys().to_owned().collect()) - .await - .unwrap(); + let modpack = Modpack::from_config(&config).await.unwrap(); - let rows: Vec> = projects - .iter() - .map(|p| vec![p.title.bold(), p.id.dimmed()]) + let rows: Vec> = modpack + .mods + .values() + .into_iter() + .map(|p| { + vec![ + p.title.bold(), + p.project_id.dimmed(), + p.version.as_ref().unwrap().version_number.normal(), + ] + }) .collect(); let mut table = Table::new(); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f256382..48ba65f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,4 @@ pub mod add; pub mod init; pub mod list; +pub mod remove; diff --git a/src/commands/remove.rs b/src/commands/remove.rs new file mode 100644 index 0000000..8d3e1f9 --- /dev/null +++ b/src/commands/remove.rs @@ -0,0 +1,17 @@ +use crate::{config, pack::Modpack}; +use colored::*; +use inquire::Select; + +pub async fn remove() { + let mut config = config::Config::get().unwrap(); + + let modpack = Modpack::from_config(&config).await.unwrap(); + + let project = Select::new("Select a mod to remove", modpack.mods.values().collect()) + .prompt() + .unwrap(); + + config.projects.remove(&project.project_id); + + config.save().unwrap(); +} diff --git a/src/main.rs b/src/main.rs index c54ac06..b570849 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod cli; mod commands; mod config; mod modrinth; +mod pack; use clap::Parser; @@ -17,7 +18,7 @@ async fn main() { commands::add::add().await; } cli::Commands::Remove => { - // config::Config::remove().await; + commands::remove::remove().await; } cli::Commands::List => { commands::list::list().await; diff --git a/src/modrinth/versions.rs b/src/modrinth/versions.rs index 391001d..d21614b 100644 --- a/src/modrinth/versions.rs +++ b/src/modrinth/versions.rs @@ -8,13 +8,13 @@ type Response = Vec; pub struct ProjectVersion { pub name: String, pub version_number: String, - game_versions: Vec, - version_type: String, - loaders: Vec, - featured: bool, + pub game_versions: Vec, + pub version_type: String, + pub loaders: Vec, + pub featured: bool, pub id: String, - project_id: String, - downloads: i64, + pub project_id: String, + pub downloads: i64, } pub async fn get_multiple_versions(version_ids: Vec<&String>) -> Result { diff --git a/src/pack.rs b/src/pack.rs new file mode 100644 index 0000000..1d8e903 --- /dev/null +++ b/src/pack.rs @@ -0,0 +1,68 @@ +use crate::{ + config, + modrinth::{ + project::{get_multiple_projects, Project}, + versions::{get_multiple_versions, ProjectVersion}, + }, +}; +use anyhow::Result; +use std::collections::HashMap; +use std::fmt; + +pub struct Modpack { + pub game_version: String, + pub loader: String, + pub mods: HashMap, +} + +pub struct Mod { + pub project_id: String, + pub title: String, + pub description: String, + pub version: Option, +} + +impl Mod { + fn from_project(project: Project) -> Self { + Self { + project_id: project.id, + title: project.title, + description: project.description, + version: None, + } + } +} + +impl fmt::Display for Mod { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.title) + } +} + +impl Modpack { + pub async fn from_config(config: &config::Config) -> Result { + let projects = get_multiple_projects(config.projects.keys().collect()) + .await + .unwrap(); + let versions = get_multiple_versions(config.projects.values().collect()) + .await + .unwrap(); + + let mut projects_map: HashMap = projects + .into_iter() + .map(|p| (p.id.clone(), Mod::from_project(p))) + .collect(); + + for version in versions { + if let Some(p) = projects_map.get_mut(&version.project_id) { + p.version = Some(version) + } + } + + Ok(Modpack { + game_version: config.game_version.clone(), + loader: config.loader.clone(), + mods: projects_map, + }) + } +}