Opens the user's preferred editor to compose a prompt
(initial_text: Option<String>)
| 125 | |
| 126 | /// Opens the user's preferred editor to compose a prompt |
| 127 | pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> { |
| 128 | // Create a temporary file with a unique name |
| 129 | let temp_dir = std::env::temp_dir(); |
| 130 | let file_name = format!("q_prompt_{}.md", Uuid::new_v4()); |
| 131 | let temp_file_path = temp_dir.join(file_name); |
| 132 | |
| 133 | // Write initial content to the file if provided |
| 134 | let initial_content = initial_text.unwrap_or_default(); |
| 135 | std::fs::write(&temp_file_path, &initial_content) |
| 136 | .map_err(|e| ChatError::Custom(format!("Failed to create temporary file: {}", e).into()))?; |
| 137 | |
| 138 | // Launch the editor |
| 139 | launch_editor(&temp_file_path)?; |
| 140 | |
| 141 | // Read the content back |
| 142 | let content = std::fs::read_to_string(&temp_file_path) |
| 143 | .map_err(|e| ChatError::Custom(format!("Failed to read temporary file: {}", e).into()))?; |
| 144 | |
| 145 | // Clean up the temporary file |
| 146 | let _ = std::fs::remove_file(&temp_file_path); |
| 147 | |
| 148 | Ok(content.trim().to_string()) |
| 149 | } |
no test coverage detected