MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / get_message_from_editor

Method get_message_from_editor

atomic-cli/src/commands/revise.rs:386–442  ·  view source on GitHub ↗

Open an editor for the user to edit the message.

(&self, original_message: &str)

Source from the content-addressed store, hash-verified

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

Callers 1

get_messageMethod · 0.45

Calls 7

temp_dirFunction · 0.85
openFunction · 0.85
successMethod · 0.80
writeFunction · 0.50
statusMethod · 0.45
linesMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected