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

Method get_message_from_editor

atomic-cli/src/commands/revise.rs:372–428  ·  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

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

Callers 1

get_messageMethod · 0.45

Calls 6

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

Tested by

no test coverage detected