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