| 1 | //! Portable edit/diff/undo/retry policy. Hosts own I/O and mutation; typed |
| 2 | //! outcomes preserve refusal versus conversation-fallback without parsing text. |
| 3 | |
| 4 | use super::{CommandResult, DebugAction}; |
| 5 | use codewhale_command_contract::facets::*; |
| 6 | use codewhale_command_contract::handler::{ |
| 7 | CommandCapabilities as Caps, CommandContexts, CommandHandler, |
| 8 | }; |
| 9 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 10 | |
| 11 | macro_rules! registration { |
| 12 | ($ty:ident, $name:literal, $usage:literal, $aliases:expr, $key:literal, $caps:expr, $handler:ident) => { |
| 13 | pub struct $ty; |
| 14 | impl RegisterCommand<CommandResult> for $ty { |
| 15 | fn info() -> &'static CommandInfo { |
| 16 | &CommandInfo { |
| 17 | name: $name, |
| 18 | aliases: $aliases, |
| 19 | usage: $usage, |
| 20 | description_key: $key, |
| 21 | } |
| 22 | } |
| 23 | fn handler() -> CommandHandler<CommandResult> { |
| 24 | CommandHandler::Contextual { |
| 25 | capabilities: $caps, |
| 26 | handler: $handler, |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | }; |
| 31 | } |
| 32 | registration!( |
| 33 | EditCmd, |
| 34 | "edit", |
| 35 | "/edit", |
| 36 | &[], |
| 37 | "cmd_edit_description", |
| 38 | Caps::DEBUG_HISTORY, |
| 39 | edit |
| 40 | ); |
| 41 | registration!( |
| 42 | DiffCmd, |
| 43 | "diff", |
| 44 | "/diff", |
| 45 | &[], |
| 46 | "cmd_diff_description", |
| 47 | Caps::DEBUG_DIFF.union(Caps::PRESENTATION), |
| 48 | diff |
| 49 | ); |
| 50 | registration!( |
| 51 | UndoCmd, |
| 52 | "undo", |
| 53 | "/undo [force|select]", |
| 54 | &[], |
| 55 | "cmd_undo_description", |
| 56 | Caps::DEBUG_UNDO |
| 57 | .union(Caps::DEBUG_HISTORY) |
| 58 | .union(Caps::PRESENTATION), |
| 59 | undo |
| 60 | ); |
| 61 | registration!( |
| 62 | RetryCmd, |
| 63 | "retry", |
| 64 | "/retry", |
| 65 | &["chongshi"], |
| 66 | "cmd_retry_description", |
| 67 | Caps::DEBUG_HISTORY, |
| 68 | retry |
| 69 | ); |
| 70 | |
| 71 | pub(super) fn edit(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult { |
| 72 | let mut parts = contexts.into_parts(); |
| 73 | let Some(history) = parts.debug_history.as_deref_mut() else { |
| 74 | return CommandResult::error("Command capability unavailable: debug_history"); |
| 75 | }; |
| 76 | match history.last_user_input() { |
| 77 | Some(input) => { |
| 78 | history.load_composer(input); |
| 79 | CommandResult::message( |
| 80 | "Last message loaded into composer — edit and press Enter to resubmit", |
| 81 | ) |
| 82 | } |
| 83 | None => CommandResult::message("No previous message to edit"), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub(super) fn retry(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult { |
| 88 | let mut parts = contexts.into_parts(); |
| 89 | let Some(history) = parts.debug_history.as_deref_mut() else { |
| 90 | return CommandResult::error("Command capability unavailable: debug_history"); |
| 91 | }; |
| 92 | match history.last_user_input() { |
| 93 | Some(input) => { |
| 94 | let undone = history.undo_conversation(); |
| 95 | let display_input = if input.len() > 50 { |
| 96 | let truncate_at = input |
| 97 | .char_indices() |
| 98 | .take_while(|(i, _)| *i <= 50) |
| 99 | .last() |
| 100 | .map_or(0, |(i, _)| i); |
| 101 | format!("{}...", &input[..truncate_at]) |
| 102 | } else { |
| 103 | input.clone() |
| 104 | }; |
| 105 | CommandResult::with_message_and_action( |
| 106 | format!("Retrying: {display_input}"), |
| 107 | DebugAction::ConversationUndo { |
| 108 | sync: undone.sync, |
| 109 | restored_files: 0, |
| 110 | retry_input: Some(input), |
| 111 | }, |
| 112 | ) |
| 113 | } |
| 114 | None => CommandResult::error("No previous request to retry"), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub(super) fn diff(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult { |
| 119 | let mut parts = contexts.into_parts(); |
| 120 | let Some(diff) = parts.debug_diff.as_deref_mut() else { |
| 121 | return CommandResult::error("Command capability unavailable: debug_diff"); |
| 122 | }; |
| 123 | let Some(presentation) = parts.presentation.as_deref_mut() else { |
| 124 | return CommandResult::error("Command capability unavailable: presentation"); |
| 125 | }; |
| 126 | match diff_report(diff.diff(), presentation) { |
| 127 | Ok(result) => result, |
| 128 | Err(error) => CommandResult::error(error), |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn diff_report( |
| 133 | observation: DebugDiffObservation, |
| 134 | presentation: &dyn CommandPresentationContext, |
| 135 | ) -> Result<CommandResult, String> { |
| 136 | Ok(match observation { |
| 137 | DebugDiffObservation::GitUnavailable => { |
| 138 | CommandResult::error(presentation.translate("cmd_diff_git_unavailable", &[])?) |
| 139 | } |
| 140 | DebugDiffObservation::NoBaseline => { |
| 141 | CommandResult::message(presentation.translate("cmd_diff_no_baseline", &[])?) |
| 142 | } |
| 143 | DebugDiffObservation::Failed(error) => { |
| 144 | CommandResult::message(presentation.translate("cmd_diff_failed", &[("error", &error)])?) |
| 145 | } |
| 146 | DebugDiffObservation::Output { |
| 147 | names, |
| 148 | stat, |
| 149 | patch, |
| 150 | patch_truncated, |
| 151 | } => { |
| 152 | if names.trim().is_empty() { |
| 153 | return Ok(CommandResult::message( |
| 154 | presentation.translate("cmd_diff_no_changes", &[])?, |
| 155 | )); |
| 156 | } |
| 157 | |
| 158 | let files: Vec<&str> = names.lines().filter(|l| !l.is_empty()).collect(); |
| 159 | let file_count = files.len(); |
| 160 | let file_list = files.join("\n"); |
| 161 | |
| 162 | // Detect rename entries (e.g. "foo -> bar") and exclude them |
| 163 | // from the file-count header so the user sees only actual |
| 164 | // modifications. |
| 165 | let renamed_count = files.iter().filter(|f| f.contains(" -> ")).count(); |
| 166 | let summary = if renamed_count > 0 { |
| 167 | presentation.translate( |
| 168 | "cmd_diff_changed_files_renamed", |
| 169 | &[ |
| 170 | ("count", &file_count.to_string()), |
| 171 | ("renamed", &renamed_count.to_string()), |
| 172 | ("files", &file_list), |
| 173 | ], |
| 174 | )? |
| 175 | } else { |
| 176 | presentation.translate( |
| 177 | "cmd_diff_changed_files", |
| 178 | &[("count", &file_count.to_string()), ("files", &file_list)], |
| 179 | )? |
| 180 | }; |
| 181 | |
| 182 | let stat_str = stat.trim(); |
| 183 | let mut message = summary; |
| 184 | if !stat_str.is_empty() { |
| 185 | let stat_header = presentation.translate("cmd_diff_stat_header", &[])?; |
| 186 | message.push_str(&format!("\n\n── {stat_header} ──\n")); |
| 187 | message.push_str(stat_str); |
| 188 | } |
| 189 | if patch_truncated { |
| 190 | message.push_str("\n\n"); |
| 191 | message.push_str(&presentation.translate("cmd_diff_truncated", &[])?); |
| 192 | } |
| 193 | if patch.trim().is_empty() { |
| 194 | return Ok(CommandResult::message(message)); |
| 195 | } |
| 196 | CommandResult::with_message_and_action( |
| 197 | message, |
| 198 | DebugAction::OpenDiffPager { |
| 199 | title: presentation.translate("cmd_diff_title", &[])?, |
| 200 | diff: patch, |
| 201 | }, |
| 202 | ) |
| 203 | } |
| 204 | }) |
| 205 | } |
| 206 | |
| 207 | pub(super) fn undo(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult { |
| 208 | let mut parts = contexts.into_parts(); |
| 209 | if parts.debug_undo.is_none() { |
| 210 | return CommandResult::error("Command capability unavailable: debug_undo"); |
| 211 | } |
| 212 | if parts.debug_history.is_none() { |
| 213 | return CommandResult::error("Command capability unavailable: debug_history"); |
| 214 | } |
| 215 | let Some(presentation) = parts.presentation.as_deref_mut() else { |
| 216 | return CommandResult::error("Command capability unavailable: presentation"); |
| 217 | }; |
| 218 | match arg.map(str::trim).filter(|arg| !arg.is_empty()) { |
| 219 | None => CommandResult::action(DebugAction::UndoLatestRequest { force: false }), |
| 220 | Some(arg) if arg.eq_ignore_ascii_case("force") => { |
| 221 | CommandResult::action(DebugAction::UndoLatestRequest { force: true }) |
| 222 | } |
| 223 | Some(arg) if arg.eq_ignore_ascii_case("select") => { |
| 224 | CommandResult::action(DebugAction::OpenUndoSelector) |
| 225 | } |
| 226 | Some(other) => { |
| 227 | match presentation.translate("cmd_undo_unknown_option", &[("option", other)]) { |
| 228 | Ok(message) => CommandResult::error(message), |
| 229 | Err(error) => CommandResult::error(error), |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 |