|
| 1 | +use clap::Subcommand; |
| 2 | +use crossterm::execute; |
| 3 | +use crossterm::style::{ |
| 4 | + self, |
| 5 | + Stylize, |
| 6 | +}; |
| 7 | +use dialoguer::FuzzySelect; |
| 8 | +use eyre::Result; |
| 9 | + |
| 10 | +use crate::cli::chat::tools::todo::{ |
| 11 | + TodoListState, |
| 12 | + delete_todo, |
| 13 | + get_all_todos, |
| 14 | +}; |
| 15 | +use crate::cli::chat::{ |
| 16 | + ChatError, |
| 17 | + ChatSession, |
| 18 | + ChatState, |
| 19 | +}; |
| 20 | +use crate::os::Os; |
| 21 | + |
| 22 | +/// Defines subcommands that allow users to view and manage todo lists |
| 23 | +#[derive(Debug, PartialEq, Subcommand)] |
| 24 | +pub enum TodoSubcommand { |
| 25 | + /// Delete all completed to-do lists |
| 26 | + ClearFinished, |
| 27 | + |
| 28 | + /// Resume a selected to-do list |
| 29 | + Resume, |
| 30 | + |
| 31 | + /// View a to-do list |
| 32 | + View, |
| 33 | + |
| 34 | + /// Delete a to-do list |
| 35 | + Delete { |
| 36 | + #[arg(long, short)] |
| 37 | + all: bool, |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +/// Used for displaying completed and in-progress todo lists |
| 42 | +pub struct TodoDisplayEntry { |
| 43 | + pub num_completed: usize, |
| 44 | + pub num_tasks: usize, |
| 45 | + pub description: String, |
| 46 | + pub id: String, |
| 47 | +} |
| 48 | + |
| 49 | +impl std::fmt::Display for TodoDisplayEntry { |
| 50 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 51 | + if self.num_completed == self.num_tasks { |
| 52 | + write!(f, "{} {}", "✓".green().bold(), self.description.clone(),) |
| 53 | + } else { |
| 54 | + write!( |
| 55 | + f, |
| 56 | + "{} {} ({}/{})", |
| 57 | + "✗".red().bold(), |
| 58 | + self.description.clone(), |
| 59 | + self.num_completed, |
| 60 | + self.num_tasks |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl TodoSubcommand { |
| 67 | + pub async fn execute(self, os: &mut Os, session: &mut ChatSession) -> Result<ChatState, ChatError> { |
| 68 | + TodoListState::init_dir(os) |
| 69 | + .await |
| 70 | + .map_err(|e| ChatError::Custom(format!("Could not create todos directory: {e}").into()))?; |
| 71 | + match self { |
| 72 | + Self::ClearFinished => { |
| 73 | + let (todos, errors) = match get_all_todos(os).await { |
| 74 | + Ok(res) => res, |
| 75 | + Err(e) => return Err(ChatError::Custom(format!("Could not get to-do lists: {e}").into())), |
| 76 | + }; |
| 77 | + let mut cleared_one = false; |
| 78 | + |
| 79 | + for todo_status in todos.iter() { |
| 80 | + if todo_status.tasks.iter().all(|b| b.completed) { |
| 81 | + match delete_todo(os, &todo_status.id).await { |
| 82 | + Ok(_) => cleared_one = true, |
| 83 | + Err(e) => { |
| 84 | + return Err(ChatError::Custom(format!("Could not delete to-do list: {e}").into())); |
| 85 | + }, |
| 86 | + }; |
| 87 | + } |
| 88 | + } |
| 89 | + if cleared_one { |
| 90 | + execute!( |
| 91 | + session.stderr, |
| 92 | + style::Print("✔ Cleared finished to-do lists!\n".green()) |
| 93 | + )?; |
| 94 | + } else { |
| 95 | + execute!(session.stderr, style::Print("No finished to-do lists to clear!\n"))?; |
| 96 | + } |
| 97 | + if !errors.is_empty() { |
| 98 | + execute!( |
| 99 | + session.stderr, |
| 100 | + style::Print(format!("* Failed to get {} todo list(s)\n", errors.len()).dark_grey()) |
| 101 | + )?; |
| 102 | + } |
| 103 | + }, |
| 104 | + Self::Resume => match Self::get_descriptions_and_statuses(os).await { |
| 105 | + Ok(entries) => { |
| 106 | + if entries.is_empty() { |
| 107 | + execute!(session.stderr, style::Print("No to-do lists to resume!\n"),)?; |
| 108 | + } else if let Some(index) = fuzzy_select_todos(&entries, "Select a to-do list to resume:") { |
| 109 | + if index < entries.len() { |
| 110 | + execute!( |
| 111 | + session.stderr, |
| 112 | + style::Print(format!( |
| 113 | + "{} {}", |
| 114 | + "⟳ Resuming:".magenta(), |
| 115 | + entries[index].description.clone() |
| 116 | + )) |
| 117 | + )?; |
| 118 | + return session.resume_todo_request(os, &entries[index].id).await; |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + Err(e) => return Err(ChatError::Custom(format!("Could not show to-do lists: {e}").into())), |
| 123 | + }, |
| 124 | + Self::View => match Self::get_descriptions_and_statuses(os).await { |
| 125 | + Ok(entries) => { |
| 126 | + if entries.is_empty() { |
| 127 | + execute!(session.stderr, style::Print("No to-do lists to view!\n"))?; |
| 128 | + } else if let Some(index) = fuzzy_select_todos(&entries, "Select a to-do list to view:") { |
| 129 | + if index < entries.len() { |
| 130 | + let list = TodoListState::load(os, &entries[index].id).await.map_err(|e| { |
| 131 | + ChatError::Custom(format!("Could not load current to-do list: {e}").into()) |
| 132 | + })?; |
| 133 | + execute!( |
| 134 | + session.stderr, |
| 135 | + style::Print(format!( |
| 136 | + "{} {}\n\n", |
| 137 | + "Viewing:".magenta(), |
| 138 | + entries[index].description.clone() |
| 139 | + )) |
| 140 | + )?; |
| 141 | + if list.display_list(&mut session.stderr).is_err() { |
| 142 | + return Err(ChatError::Custom("Could not display the selected to-do list".into())); |
| 143 | + } |
| 144 | + execute!(session.stderr, style::Print("\n"),)?; |
| 145 | + } |
| 146 | + } |
| 147 | + }, |
| 148 | + Err(e) => return Err(ChatError::Custom(format!("Could not show to-do lists: {e}").into())), |
| 149 | + }, |
| 150 | + Self::Delete { all } => match Self::get_descriptions_and_statuses(os).await { |
| 151 | + Ok(entries) => { |
| 152 | + if entries.is_empty() { |
| 153 | + execute!(session.stderr, style::Print("No to-do lists to delete!\n"))?; |
| 154 | + } else if all { |
| 155 | + for entry in entries { |
| 156 | + delete_todo(os, &entry.id) |
| 157 | + .await |
| 158 | + .map_err(|_e| ChatError::Custom("Could not delete all to-do lists".into()))?; |
| 159 | + } |
| 160 | + execute!(session.stderr, style::Print("✔ Deleted all to-do lists!\n".green()),)?; |
| 161 | + } else if let Some(index) = fuzzy_select_todos(&entries, "Select a to-do list to delete:") { |
| 162 | + if index < entries.len() { |
| 163 | + delete_todo(os, &entries[index].id).await.map_err(|e| { |
| 164 | + ChatError::Custom(format!("Could not delete the selected to-do list: {e}").into()) |
| 165 | + })?; |
| 166 | + execute!( |
| 167 | + session.stderr, |
| 168 | + style::Print("✔ Deleted to-do list: ".green()), |
| 169 | + style::Print(format!("{}\n", entries[index].description.clone().dark_grey())) |
| 170 | + )?; |
| 171 | + } |
| 172 | + } |
| 173 | + }, |
| 174 | + Err(e) => return Err(ChatError::Custom(format!("Could not show to-do lists: {e}").into())), |
| 175 | + }, |
| 176 | + } |
| 177 | + Ok(ChatState::PromptUser { |
| 178 | + skip_printing_tools: true, |
| 179 | + }) |
| 180 | + } |
| 181 | + |
| 182 | + /// Convert all to-do list state entries to displayable entries |
| 183 | + async fn get_descriptions_and_statuses(os: &Os) -> Result<Vec<TodoDisplayEntry>> { |
| 184 | + let mut out = Vec::new(); |
| 185 | + let (todos, _) = get_all_todos(os).await?; |
| 186 | + for todo in todos.iter() { |
| 187 | + out.push(TodoDisplayEntry { |
| 188 | + num_completed: todo.tasks.iter().filter(|t| t.completed).count(), |
| 189 | + num_tasks: todo.tasks.len(), |
| 190 | + description: todo.description.clone(), |
| 191 | + id: todo.id.clone(), |
| 192 | + }); |
| 193 | + } |
| 194 | + Ok(out) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +fn fuzzy_select_todos(entries: &[TodoDisplayEntry], prompt_str: &str) -> Option<usize> { |
| 199 | + FuzzySelect::new() |
| 200 | + .with_prompt(prompt_str) |
| 201 | + .items(entries) |
| 202 | + .report(false) |
| 203 | + .interact_opt() |
| 204 | + .unwrap_or(None) |
| 205 | +} |
0 commit comments