Open an editor for the user to edit the message.
(&self, original_message: &str)
| 375 | |
| 376 | /// Open an editor for the user to edit the message. |
| 377 | fn get_message_from_editor(&self, original_message: &str) -> CliResult<String> { |
| 378 | use std::io::Read; |
| 379 | |
| 380 | // Create a temporary file with the original message |
| 381 | let temp_dir = std::env::temp_dir(); |
| 382 | let temp_file = temp_dir.join("ATOMIC_REVISE_MSG"); |
| 383 | |
| 384 | // Write original message with instructions |
| 385 | let content = format!( |
| 386 | "{}\n\n# Revising change. Lines starting with '#' will be ignored.\n# An empty message aborts the revision.\n", |
| 387 | original_message |
| 388 | ); |
| 389 | |
| 390 | std::fs::write(&temp_file, &content).map_err(|e| { |
| 391 | CliError::Internal(anyhow::anyhow!("Failed to create temp file: {}", e)) |
| 392 | })?; |
| 393 | |
| 394 | // Get editor from environment |
| 395 | let editor = std::env::var("EDITOR") |
| 396 | .or_else(|_| std::env::var("VISUAL")) |
| 397 | .unwrap_or_else(|_| "vi".to_string()); |
| 398 | |
| 399 | // Open editor |
| 400 | let status = std::process::Command::new(&editor) |
| 401 | .arg(&temp_file) |
| 402 | .status() |
| 403 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to open editor: {}", e)))?; |
| 404 | |
| 405 | if !status.success() { |
| 406 | return Err(CliError::Cancelled); |
| 407 | } |
| 408 | |
| 409 | // Read the edited message |
| 410 | let mut edited = String::new(); |
| 411 | std::fs::File::open(&temp_file) |
| 412 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to read temp file: {}", e)))? |
| 413 | .read_to_string(&mut edited) |
| 414 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to read temp file: {}", e)))?; |
| 415 | |
| 416 | // Clean up |
| 417 | let _ = std::fs::remove_file(&temp_file); |
| 418 | |
| 419 | // Filter out comment lines and trim |
| 420 | let message: String = edited |
| 421 | .lines() |
| 422 | .filter(|line| !line.starts_with('#')) |
| 423 | .collect::<Vec<_>>() |
| 424 | .join("\n") |
| 425 | .trim() |
| 426 | .to_string(); |
| 427 | |
| 428 | if message.is_empty() { |
| 429 | return Err(CliError::Cancelled); |
| 430 | } |
| 431 | |
| 432 | Ok(message) |
| 433 | } |
| 434 |