返回 CodeWhale
copy.rs
根目录 / crates / tui / src / commands / groups / core / copy.rs
1 //! `/copy` command — copy the last completed assistant response.
2
3 use codewhale_command_contract::facets::CommandPresentationContext;
4 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
5 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
6 use codewhale_command_contract::navigation::ResponseCopyDelivery;
7 use codewhale_command_contract::outcome::CoreNavigationCommandResult as CommandResult;
8
9 pub const COMMAND_INFO: CommandInfo = CommandInfo {
10 name: "copy",
11 aliases: &[],
12 usage: "/copy",
13 description_key: "cmd_copy_description",
14 };
15
16 pub struct CopyCmd;
17
18 impl RegisterCommand<CommandResult> for CopyCmd {
19 fn info() -> &'static CommandInfo {
20 &COMMAND_INFO
21 }
22
23 fn handler() -> CommandHandler<CommandResult> {
24 CommandHandler::Contextual {
25 capabilities: CommandCapabilities::RESPONSE_COPY
26 .union(CommandCapabilities::PRESENTATION),
27 handler: copy,
28 }
29 }
30 }
31
32 fn copy(mut contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
33 let Some(response_copy) = contexts.take_response_copy() else {
34 return CommandResult::error("Command capability unavailable: response copy");
35 };
36 let Some(presentation) = contexts.into_parts().presentation else {
37 return CommandResult::error("Command capability unavailable: presentation");
38 };
39 let result = match response_copy.copy_completed_response() {
40 Some(delivery) => delivery_message(presentation, delivery),
41 None => presentation.translate("cmd_copy_no_output", &[]),
42 };
43 let result = match result {
44 Ok(message) => CommandResult::message(message),
45 Err(message) => CommandResult::error(message),
46 };
47 if let Some(message) = &result.message {
48 response_copy.show_copy_receipt(message, result.is_error);
49 }
50 result
51 }
52
53 fn delivery_message(
54 presentation: &dyn CommandPresentationContext,
55 delivery: ResponseCopyDelivery,
56 ) -> Result<String, String> {
57 let path = delivery.recovery_path.map(|p| p.display().to_string());
58 let error = delivery.clipboard_result.err();
59 match (
60 error.as_deref(),
61 path.as_deref(),
62 delivery.requires_terminal_paste,
63 ) {
64 (None, Some(path), true) => presentation.translate("cmd_copy_queued", &[("path", path)]),
65 (None, None, true) => presentation.translate("cmd_copy_queued_no_backup", &[]),
66 (None, Some(path), false) => presentation.translate("cmd_copy_success", &[("path", path)]),
67 (None, None, false) => presentation.translate("cmd_copy_success_no_backup", &[]),
68 (Some(error), Some(path), _) => {
69 Err(presentation.translate("cmd_copy_failed", &[("error", error), ("path", path)])?)
70 }
71 (Some(error), None, _) => {
72 Err(presentation.translate("cmd_copy_failed_no_backup", &[("error", error)])?)
73 }
74 }
75 }
76
76 lines RUST