track/cli/handlers/
completion.rs1use crate::cli::handlers::CommandCtx;
2use crate::cli::CompletionType;
3use crate::models::TodoStatus;
4use crate::services::{LinkService, RepoService, TaskService, TodoService};
5use crate::utils::Result;
6
7pub fn handle_completion(
8 _ctx: &CommandCtx,
9 shell: clap_complete::Shell,
10 dynamic: bool,
11) -> Result<()> {
12 use clap::CommandFactory;
13 use clap_complete::generate;
14 use std::io;
15
16 if dynamic {
17 let script = match shell {
19 clap_complete::Shell::Bash => {
20 include_str!("../../../completions/track.bash.dynamic")
21 }
22 clap_complete::Shell::Zsh => {
23 include_str!("../../../completions/_track.dynamic")
24 }
25 _ => {
26 eprintln!("Dynamic completions are only available for bash and zsh.");
27 eprintln!("Falling back to static completions for {:?}.", shell);
28 let mut cmd = crate::cli::Cli::command();
29 let bin_name = cmd.get_name().to_string();
30 generate(shell, &mut cmd, bin_name, &mut io::stdout());
31 return Ok(());
32 }
33 };
34 print!("{}", script);
35 } else {
36 let mut cmd = crate::cli::Cli::command();
38 let bin_name = cmd.get_name().to_string();
39 generate(shell, &mut cmd, bin_name, &mut io::stdout());
40 }
41
42 Ok(())
43}
44
45pub fn handle_complete(ctx: &CommandCtx, completion_type: CompletionType) -> Result<()> {
46 match completion_type {
47 CompletionType::Tasks => {
48 let task_service = TaskService::new(ctx.db);
50 let tasks = task_service.list_tasks(false)?; for task in tasks {
53 if let Some(ticket) = &task.ticket_id {
55 println!("{}:{}:{}", task.id, ticket, task.name);
56 } else {
57 println!("{}:{}", task.id, task.name);
58 }
59 }
60 }
61 CompletionType::Todos => {
62 let current_task_id = ctx.db.get_current_task_id()?;
64 if let Some(task_id) = current_task_id {
65 let todo_service = TodoService::new(ctx.db);
66 let todos = todo_service.list_todos(task_id)?;
67
68 for todo in todos {
69 if todo.status == TodoStatus::Pending {
71 println!("{}:{}", todo.task_index, todo.content);
73 }
74 }
75 }
76 }
77 CompletionType::Links => {
78 let current_task_id = ctx.db.get_current_task_id()?;
80 if let Some(task_id) = current_task_id {
81 let link_service = LinkService::new(ctx.db);
82 let links = link_service.list_links(task_id)?;
83
84 for link in links {
85 println!("{}:{}:{}", link.task_index, link.title, link.url);
87 }
88 }
89 }
90 CompletionType::Repos => {
91 let current_task_id = ctx.db.get_current_task_id()?;
93 if let Some(task_id) = current_task_id {
94 let repo_service = RepoService::new(ctx.db);
95 let repos = repo_service.list_repos(task_id)?;
96
97 for repo in repos {
98 println!("{}:{}", repo.task_index, repo.repo_path);
100 }
101 }
102 }
103 }
104
105 Ok(())
106}