use crate::cli::handlers::CommandCtx;
use crate::cli::CompletionType;
use crate::models::TodoStatus;
use crate::services::{LinkService, RepoService, TaskService, TodoService};
use crate::utils::Result;
pub fn handle_completion(
_ctx: &CommandCtx,
shell: clap_complete::Shell,
dynamic: bool,
) -> Result<()> {
use clap::CommandFactory;
use clap_complete::generate;
use std::io;
if dynamic {
let script = match shell {
clap_complete::Shell::Bash => {
include_str!("../../../completions/track.bash.dynamic")
}
clap_complete::Shell::Zsh => {
include_str!("../../../completions/_track.dynamic")
}
_ => {
eprintln!("Dynamic completions are only available for bash and zsh.");
eprintln!("Falling back to static completions for {:?}.", shell);
let mut cmd = crate::cli::Cli::command();
let bin_name = cmd.get_name().to_string();
generate(shell, &mut cmd, bin_name, &mut io::stdout());
return Ok(());
}
};
print!("{}", script);
} else {
let mut cmd = crate::cli::Cli::command();
let bin_name = cmd.get_name().to_string();
generate(shell, &mut cmd, bin_name, &mut io::stdout());
}
Ok(())
}
pub fn handle_complete(ctx: &CommandCtx, completion_type: CompletionType) -> Result<()> {
match completion_type {
CompletionType::Tasks => {
let task_service = TaskService::new(ctx.db);
let tasks = task_service.list_tasks(false)?;
for task in tasks {
if let Some(ticket) = &task.ticket_id {
println!("{}:{}:{}", task.id, ticket, task.name);
} else {
println!("{}:{}", task.id, task.name);
}
}
}
CompletionType::Todos => {
let current_task_id = ctx.db.get_current_task_id()?;
if let Some(task_id) = current_task_id {
let todo_service = TodoService::new(ctx.db);
let todos = todo_service.list_todos(task_id)?;
for todo in todos {
if todo.status == TodoStatus::Pending {
println!("{}:{}", todo.task_index, todo.content);
}
}
}
}
CompletionType::Links => {
let current_task_id = ctx.db.get_current_task_id()?;
if let Some(task_id) = current_task_id {
let link_service = LinkService::new(ctx.db);
let links = link_service.list_links(task_id)?;
for link in links {
println!("{}:{}:{}", link.task_index, link.title, link.url);
}
}
}
CompletionType::Repos => {
let current_task_id = ctx.db.get_current_task_id()?;
if let Some(task_id) = current_task_id {
let repo_service = RepoService::new(ctx.db);
let repos = repo_service.list_repos(task_id)?;
for repo in repos {
println!("{}:{}", repo.task_index, repo.repo_path);
}
}
}
}
Ok(())
}