| 1 | use super::HistoryCell; |
| 2 | use codewhale_tui_kit::ui_text::line_to_plain; |
| 3 | |
| 4 | pub(crate) fn history_cell_to_text(cell: &HistoryCell, width: u16) -> String { |
| 5 | // Error detail/copy is a diagnostic boundary, not a screenshot of the |
| 6 | // live wrapping. Preserve the exact source message so narrow terminals do |
| 7 | // not insert newlines into hostnames, env vars, commands, or URLs. |
| 8 | if let HistoryCell::Error { message, code, .. } = cell { |
| 9 | if let Some(code) = super::display_error_code(message, code.as_deref()) { |
| 10 | return format!("[{code}] {message}"); |
| 11 | } |
| 12 | return message.clone(); |
| 13 | } |
| 14 | cell.transcript_lines(width) |
| 15 | .into_iter() |
| 16 | .map(|line| line_to_plain(&line)) |
| 17 | .collect::<Vec<_>>() |
| 18 | .join("\n") |
| 19 | } |
| 20 | |
| 21 | /// Serialize one complete history cell for Copy Message. |
| 22 | /// |
| 23 | /// User, assistant and system cells have canonical source text. Returning it directly |
| 24 | /// preserves authored Markdown and hard line breaks while keeping role glyphs, |
| 25 | /// continuation rails, and visual wrapping out of the clipboard. Transcript |
| 26 | /// drag selections reuse this same projection per intersected cell |
| 27 | /// (`selection_to_markdown`, #6156); only the rendered-text fallback still |
| 28 | /// serializes the live cache, where Markdown has already been transformed and |
| 29 | /// user-message soft wraps do not carry join metadata. |
| 30 | /// |
| 31 | /// Tool source rendering retains ANSI-stripped stored text independently of bounded display rows. |
| 32 | /// Thinking cells retain their full-transcript representation. |
| 33 | pub(crate) fn history_cell_to_clipboard_text(cell: &HistoryCell, width: u16) -> String { |
| 34 | match cell { |
| 35 | HistoryCell::User { content } |
| 36 | | HistoryCell::Assistant { content, .. } |
| 37 | | HistoryCell::System { content } => content.clone(), |
| 38 | HistoryCell::Tool(tool) => { |
| 39 | let mut output = String::new(); |
| 40 | for (index, line) in tool.source_lines(width).into_iter().enumerate() { |
| 41 | if index > 0 { |
| 42 | output.push('\n'); |
| 43 | } |
| 44 | for span in line.spans { |
| 45 | output.push_str(&span.content); |
| 46 | } |
| 47 | } |
| 48 | let mut normalized = String::with_capacity(output.len()); |
| 49 | codewhale_sanitize::sanitize::strip_ansi_into(&output, &mut normalized); |
| 50 | normalized |
| 51 | } |
| 52 | _ => history_cell_to_text(cell, width), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[cfg(test)] |
| 57 | mod tests { |
| 58 | use super::*; |
| 59 | |
| 60 | #[test] |
| 61 | fn clipboard_text_for_assistant_uses_source_without_visual_rails() { |
| 62 | let content = "A long assistant response that will wrap at a narrow width."; |
| 63 | let cell = HistoryCell::Assistant { |
| 64 | content: content.to_string(), |
| 65 | streaming: false, |
| 66 | }; |
| 67 | |
| 68 | let rendered = history_cell_to_text(&cell, 12); |
| 69 | assert_ne!(rendered, content, "test setup must exercise rendering"); |
| 70 | assert!( |
| 71 | rendered.contains('\n'), |
| 72 | "test setup must exercise visual wrapping: {rendered:?}" |
| 73 | ); |
| 74 | assert_eq!(history_cell_to_clipboard_text(&cell, 12), content); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn clipboard_text_preserves_authored_rail_glyphs() { |
| 79 | let content = "● literal role glyph\n▏ literal rail glyph"; |
| 80 | let cell = HistoryCell::Assistant { |
| 81 | content: content.to_string(), |
| 82 | streaming: false, |
| 83 | }; |
| 84 | |
| 85 | assert_eq!(history_cell_to_clipboard_text(&cell, 10), content); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn clipboard_text_preserves_markdown_source_and_hard_breaks() { |
| 90 | let content = r#"Heading |
| 91 | |
| 92 | ```rust |
| 93 | fn main() { |
| 94 | println!("hello"); |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | After code."#; |
| 99 | let cell = HistoryCell::Assistant { |
| 100 | content: content.to_string(), |
| 101 | streaming: false, |
| 102 | }; |
| 103 | |
| 104 | assert_eq!(history_cell_to_clipboard_text(&cell, 16), content); |
| 105 | let diagnostic = serde_json::json!({"prompt": "Use \"quoted\" input\nwith a second line"}); |
| 106 | let source = serde_json::to_string_pretty(&diagnostic).unwrap(); |
| 107 | let cell = HistoryCell::System { |
| 108 | content: source.clone(), |
| 109 | }; |
| 110 | let copied = history_cell_to_clipboard_text(&cell, 16); |
| 111 | assert_eq!(copied, source); |
| 112 | assert_eq!( |
| 113 | serde_json::from_str::<serde_json::Value>(&copied).unwrap(), |
| 114 | diagnostic |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn clipboard_text_for_user_uses_source_text() { |
| 120 | let content = "user text that wraps visually"; |
| 121 | let cell = HistoryCell::User { |
| 122 | content: content.to_string(), |
| 123 | }; |
| 124 | |
| 125 | let rendered = history_cell_to_text(&cell, 8); |
| 126 | assert_ne!(rendered, content, "test setup must exercise rendering"); |
| 127 | assert!( |
| 128 | rendered.contains('\n'), |
| 129 | "test setup must exercise visual wrapping: {rendered:?}" |
| 130 | ); |
| 131 | assert_eq!(history_cell_to_clipboard_text(&cell, 8), content); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn clipboard_text_for_thinking_keeps_full_transcript_semantics() { |
| 136 | let content = "First paragraph lede.\n\ |
| 137 | Second sentence of the first paragraph.\n\n\ |
| 138 | Second paragraph: deeper analysis follows.\n\ |
| 139 | More detail in paragraph two.\n\n\ |
| 140 | Third paragraph: even more reasoning.\n\ |
| 141 | With another line.\n\n\ |
| 142 | Fourth paragraph: the conclusion.\n\ |
| 143 | And one more line for good measure.\n\n\ |
| 144 | Fifth paragraph: final verification.\n\ |
| 145 | One last supporting detail."; |
| 146 | let cell = HistoryCell::Thinking { |
| 147 | content: content.to_string(), |
| 148 | streaming: false, |
| 149 | duration_secs: Some(3.2), |
| 150 | }; |
| 151 | |
| 152 | let live = cell |
| 153 | .lines_with_options( |
| 154 | 80, |
| 155 | crate::tui::history::TranscriptRenderOptions { |
| 156 | low_motion: true, |
| 157 | ..crate::tui::history::TranscriptRenderOptions::default() |
| 158 | }, |
| 159 | ) |
| 160 | .into_iter() |
| 161 | .map(|line| line_to_plain(&line)) |
| 162 | .collect::<Vec<_>>() |
| 163 | .join("\n"); |
| 164 | let transcript = history_cell_to_text(&cell, 80); |
| 165 | let copied = history_cell_to_clipboard_text(&cell, 80); |
| 166 | |
| 167 | assert!(!live.contains("Fifth paragraph"), "{live:?}"); |
| 168 | assert!(transcript.contains("Fifth paragraph"), "{transcript:?}"); |
| 169 | assert_eq!(copied, transcript); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn clipboard_text_for_tool_keeps_full_transcript_semantics() { |
| 174 | use crate::tui::history::{GenericToolCell, ToolCell, ToolStatus}; |
| 175 | |
| 176 | let cell = HistoryCell::Tool(ToolCell::Generic(GenericToolCell { |
| 177 | name: "exec_shell".to_string(), |
| 178 | status: ToolStatus::Success, |
| 179 | input_summary: Some("cargo test".to_string()), |
| 180 | output: Some("complete tool output".to_string()), |
| 181 | prompts: None, |
| 182 | spillover_path: None, |
| 183 | output_summary: None, |
| 184 | is_diff: false, |
| 185 | })); |
| 186 | |
| 187 | let transcript = history_cell_to_text(&cell, 80); |
| 188 | assert!( |
| 189 | transcript.contains("complete tool output"), |
| 190 | "{transcript:?}" |
| 191 | ); |
| 192 | assert_eq!(history_cell_to_clipboard_text(&cell, 80), transcript); |
| 193 | } |
| 194 | } |
| 195 |