|
| 1 | +use forge_domain::Todo; |
| 2 | + |
| 3 | +/// Controls the styling applied to a rendered todo line. |
| 4 | +enum TodoLineStyle { |
| 5 | + /// Bold styling used for new or changed todos. |
| 6 | + Bold, |
| 7 | + /// Dim styling used for unchanged todos. |
| 8 | + Dim, |
| 9 | +} |
| 10 | + |
| 11 | +/// Renders one todo line with icon and ANSI styling. |
| 12 | +/// |
| 13 | +/// # Arguments |
| 14 | +/// |
| 15 | +/// * `todo` - Todo item to render. |
| 16 | +/// * `line_style` - Emphasis style for the line. |
| 17 | +fn format_todo_line(todo: &Todo, line_style: TodoLineStyle) -> String { |
| 18 | + use console::style; |
| 19 | + use forge_domain::TodoStatus; |
| 20 | + |
| 21 | + let checkbox = match todo.status { |
| 22 | + TodoStatus::Completed => "", |
| 23 | + TodoStatus::InProgress => "", |
| 24 | + TodoStatus::Pending => "", |
| 25 | + }; |
| 26 | + |
| 27 | + let content = match todo.status { |
| 28 | + TodoStatus::Completed => style(todo.content.as_str()).strikethrough().to_string(), |
| 29 | + _ => todo.content.clone(), |
| 30 | + }; |
| 31 | + |
| 32 | + let line = format!(" {checkbox} {content}"); |
| 33 | + let styled = match (&todo.status, line_style) { |
| 34 | + (TodoStatus::Pending, TodoLineStyle::Bold) => style(line).white().bold().to_string(), |
| 35 | + (TodoStatus::Pending, TodoLineStyle::Dim) => style(line).white().dim().to_string(), |
| 36 | + (TodoStatus::InProgress, TodoLineStyle::Bold) => style(line).cyan().bold().to_string(), |
| 37 | + (TodoStatus::InProgress, TodoLineStyle::Dim) => style(line).cyan().dim().to_string(), |
| 38 | + (TodoStatus::Completed, TodoLineStyle::Bold) => style(line).green().bold().to_string(), |
| 39 | + (TodoStatus::Completed, TodoLineStyle::Dim) => style(line).green().dim().to_string(), |
| 40 | + }; |
| 41 | + |
| 42 | + format!("{styled}\n") |
| 43 | +} |
| 44 | + |
| 45 | +/// Formats a todo diff showing all todos in `after` plus removed todos from |
| 46 | +/// `before`. |
| 47 | +/// |
| 48 | +/// # Arguments |
| 49 | +/// |
| 50 | +/// * `before` - Previous todo list state. |
| 51 | +/// * `after` - New todo list state. |
| 52 | +pub(crate) fn format_todos_diff(before: &[Todo], after: &[Todo]) -> String { |
| 53 | + use console::style; |
| 54 | + |
| 55 | + let before_map: std::collections::HashMap<&str, &Todo> = |
| 56 | + before.iter().map(|todo| (todo.id.as_str(), todo)).collect(); |
| 57 | + let after_ids: std::collections::HashSet<&str> = |
| 58 | + after.iter().map(|todo| todo.id.as_str()).collect(); |
| 59 | + |
| 60 | + let mut result = "\n".to_string(); |
| 61 | + |
| 62 | + for todo in after { |
| 63 | + let previous = before_map.get(todo.id.as_str()).copied(); |
| 64 | + let is_new = previous.is_none(); |
| 65 | + let is_changed = previous |
| 66 | + .map(|item| item.status != todo.status || item.content != todo.content) |
| 67 | + .unwrap_or(false); |
| 68 | + |
| 69 | + let line_style = if is_new || is_changed { |
| 70 | + TodoLineStyle::Bold |
| 71 | + } else { |
| 72 | + TodoLineStyle::Dim |
| 73 | + }; |
| 74 | + |
| 75 | + result.push_str(&format_todo_line(todo, line_style)); |
| 76 | + } |
| 77 | + |
| 78 | + for todo in before { |
| 79 | + if !after_ids.contains(todo.id.as_str()) { |
| 80 | + let content = style(todo.content.as_str()).strikethrough().to_string(); |
| 81 | + result.push_str(&format!(" {}\n", style(format!(" {content}")).red())); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + result |
| 86 | +} |
| 87 | + |
| 88 | +/// Formats todos as ANSI-styled checklist lines. |
| 89 | +/// |
| 90 | +/// # Arguments |
| 91 | +/// |
| 92 | +/// * `todos` - Todo list to format. |
| 93 | +pub(crate) fn format_todos(todos: &[Todo]) -> String { |
| 94 | + if todos.is_empty() { |
| 95 | + return String::new(); |
| 96 | + } |
| 97 | + |
| 98 | + let mut result = "\n".to_string(); |
| 99 | + |
| 100 | + for todo in todos { |
| 101 | + result.push_str(&format_todo_line(todo, TodoLineStyle::Dim)); |
| 102 | + } |
| 103 | + |
| 104 | + result |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod tests { |
| 109 | + use console::strip_ansi_codes; |
| 110 | + use forge_domain::{ChatResponseContent, Environment, Todo, TodoStatus}; |
| 111 | + use insta::assert_snapshot; |
| 112 | + |
| 113 | + use crate::fmt::content::FormatContent; |
| 114 | + use crate::operation::ToolOperation; |
| 115 | + |
| 116 | + fn fixture_environment() -> Environment { |
| 117 | + use fake::{Fake, Faker}; |
| 118 | + |
| 119 | + let max_bytes: f64 = 250.0 * 1024.0; |
| 120 | + let fixture: Environment = Faker.fake(); |
| 121 | + |
| 122 | + fixture |
| 123 | + .max_search_lines(25) |
| 124 | + .max_search_result_bytes(max_bytes.ceil() as usize) |
| 125 | + .fetch_truncation_limit(55) |
| 126 | + .max_read_size(10) |
| 127 | + .stdout_max_prefix_length(10) |
| 128 | + .stdout_max_suffix_length(10) |
| 129 | + .max_line_length(100) |
| 130 | + .max_file_size(0) |
| 131 | + } |
| 132 | + |
| 133 | + fn fixture_todo(content: &str, id: &str, status: TodoStatus) -> Todo { |
| 134 | + Todo::new(content).id(id).status(status) |
| 135 | + } |
| 136 | + |
| 137 | + fn fixture_todo_write_output(before: Vec<Todo>, after: Vec<Todo>) -> String { |
| 138 | + let setup = ToolOperation::TodoWrite { before, after }; |
| 139 | + let actual = setup.to_content(&fixture_environment()); |
| 140 | + |
| 141 | + if let Some(ChatResponseContent::ToolOutput(output)) = actual { |
| 142 | + strip_ansi_codes(output.as_str()).to_string() |
| 143 | + } else { |
| 144 | + panic!("Expected ToolOutput content") |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn test_todo_write_mixed_changes_snapshot() { |
| 150 | + let setup = ( |
| 151 | + vec![ |
| 152 | + fixture_todo("Task 1", "1", TodoStatus::Pending), |
| 153 | + fixture_todo("Task 2", "2", TodoStatus::InProgress), |
| 154 | + ], |
| 155 | + vec![ |
| 156 | + fixture_todo("Task 1", "1", TodoStatus::Completed), |
| 157 | + fixture_todo("Task 3", "3", TodoStatus::Pending), |
| 158 | + ], |
| 159 | + ); |
| 160 | + |
| 161 | + let actual = fixture_todo_write_output(setup.0, setup.1); |
| 162 | + assert_snapshot!(actual); |
| 163 | + } |
| 164 | +} |
0 commit comments