use clap::{Parser, Subcommand};
use colored::Colorize;
use crate::commands::{self, task};
#[derive(Parser, Debug)]
#[command(
name = env!("CARGO_PKG_NAME"),
version = env!("CARGO_PKG_VERSION"),
about = format!(
"{}\nVersion: {}",
env!("CARGO_PKG_DESCRIPTION"),
env!("CARGO_PKG_VERSION").green()
)
)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
#[arg(short, long)]
pub interactive: bool,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
#[command(name = "project", visible_alias = "p")]
Project {
#[command(subcommand)]
command: Option<ProjectSubcommand>,
},
#[command(name = "task", visible_alias = "t")]
Task {
#[command(subcommand)]
command: Option<TaskSubcommand>,
},
Config,
}
#[derive(Subcommand, Debug)]
pub enum ProjectSubcommand {
#[command(name = "add", visible_alias = "a")]
Add { name: String },
#[command(name = "list", visible_alias = "l")]
List,
#[command(name = "use", visible_alias = "u")]
Use { name: String },
#[command(name = "del", visible_alias = "d")]
Del { name: String },
#[command(name = "current", visible_alias = "c")]
Current,
#[command(name = "edit", visible_alias = "e")]
Edit { name: String, new_name: String },
}
impl ProjectSubcommand {
pub fn execute_command(command: Option<Self>) -> anyhow::Result<bool> {
match command.unwrap_or(Self::List) {
ProjectSubcommand::Add { name } => commands::project::add(name),
ProjectSubcommand::List => commands::project::list(),
ProjectSubcommand::Use { name } => commands::project::use_project(name),
ProjectSubcommand::Del { name } => commands::project::delete(name),
ProjectSubcommand::Current => commands::project::get_current_project(),
ProjectSubcommand::Edit { name, new_name } => commands::project::edit(name, new_name),
}
}
}
#[derive(Subcommand, Debug)]
pub enum TaskSubcommand {
#[command(name = "add", visible_alias = "a")]
Add { title: String },
#[command(name = "edit", visible_alias = "e")]
Edit { id: String, title: String },
#[command(name = "list", visible_alias = "l")]
List,
#[command(name = "del", visible_alias = "d")]
Delete { id: String },
#[command(name = "start")]
Start { id: String },
#[command(name = "done")]
Done { id: String },
}
impl TaskSubcommand {
pub fn execute_command(command: Option<Self>) -> anyhow::Result<bool> {
match command.unwrap_or(Self::List) {
TaskSubcommand::Add{title} => task::add(title),
TaskSubcommand::List => task::list(),
TaskSubcommand::Edit { id, title } => task::edit(id, title),
TaskSubcommand::Delete { id } => task::delete(id),
TaskSubcommand::Start { id } => task::start(id),
TaskSubcommand::Done { id } => task::done(id),
}
}
}