Added the list and remove command

This commit is contained in:
xeovalyte 2024-06-03 13:51:59 +02:00
parent 56f659a182
commit 846b3d0edf
No known key found for this signature in database
6 changed files with 107 additions and 14 deletions

View File

@ -1,17 +1,23 @@
use crate::{config, modrinth::project::get_multiple_projects}; use crate::{config, pack::Modpack};
use colored::*; use colored::*;
use comfy_table::{presets::NOTHING, Table}; use comfy_table::{presets::NOTHING, Table};
pub async fn list() { pub async fn list() {
let config = config::Config::get().unwrap(); let config = config::Config::get().unwrap();
let projects = get_multiple_projects(config.projects.keys().to_owned().collect()) let modpack = Modpack::from_config(&config).await.unwrap();
.await
.unwrap();
let rows: Vec<Vec<ColoredString>> = projects let rows: Vec<Vec<ColoredString>> = modpack
.iter() .mods
.map(|p| vec![p.title.bold(), p.id.dimmed()]) .values()
.into_iter()
.map(|p| {
vec![
p.title.bold(),
p.project_id.dimmed(),
p.version.as_ref().unwrap().version_number.normal(),
]
})
.collect(); .collect();
let mut table = Table::new(); let mut table = Table::new();

View File

@ -1,3 +1,4 @@
pub mod add; pub mod add;
pub mod init; pub mod init;
pub mod list; pub mod list;
pub mod remove;

17
src/commands/remove.rs Normal file
View File

@ -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();
}

View File

@ -2,6 +2,7 @@ mod cli;
mod commands; mod commands;
mod config; mod config;
mod modrinth; mod modrinth;
mod pack;
use clap::Parser; use clap::Parser;
@ -17,7 +18,7 @@ async fn main() {
commands::add::add().await; commands::add::add().await;
} }
cli::Commands::Remove => { cli::Commands::Remove => {
// config::Config::remove().await; commands::remove::remove().await;
} }
cli::Commands::List => { cli::Commands::List => {
commands::list::list().await; commands::list::list().await;

View File

@ -8,13 +8,13 @@ type Response = Vec<ProjectVersion>;
pub struct ProjectVersion { pub struct ProjectVersion {
pub name: String, pub name: String,
pub version_number: String, pub version_number: String,
game_versions: Vec<String>, pub game_versions: Vec<String>,
version_type: String, pub version_type: String,
loaders: Vec<String>, pub loaders: Vec<String>,
featured: bool, pub featured: bool,
pub id: String, pub id: String,
project_id: String, pub project_id: String,
downloads: i64, pub downloads: i64,
} }
pub async fn get_multiple_versions(version_ids: Vec<&String>) -> Result<Response> { pub async fn get_multiple_versions(version_ids: Vec<&String>) -> Result<Response> {

68
src/pack.rs Normal file
View File

@ -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<String, Mod>,
}
pub struct Mod {
pub project_id: String,
pub title: String,
pub description: String,
pub version: Option<ProjectVersion>,
}
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<Self> {
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<String, Mod> = 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,
})
}
}