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

Method get_message_from_editor

atomic-cli/src/commands/record/builder.rs:153–216  ·  view source on GitHub ↗

Open an editor to get the commit message.

(&self)

Source from the content-addressed store, hash-verified

151
152 /// Open an editor to get the commit message.
153 pub(super) fn get_message_from_editor(&self) -> CliResult<String> {
154 // Create a temporary file for the message
155 let temp_dir = std::env::temp_dir();
156 let temp_file = temp_dir.join("ATOMIC_EDITMSG");
157
158 // Write template to file
159 let template = r#"
160# Enter your commit message above.
161# Lines starting with '#' will be ignored.
162# An empty message aborts the commit.
163"#;
164
165 std::fs::write(&temp_file, template).map_err(|e| {
166 CliError::Internal(anyhow::anyhow!("Failed to create temp file: {}", e))
167 })?;
168
169 // Get editor from environment
170 let editor = std::env::var("EDITOR")
171 .or_else(|_| std::env::var("VISUAL"))
172 .unwrap_or_else(|_| {
173 if cfg!(windows) {
174 "notepad".to_string()
175 } else {
176 "vi".to_string()
177 }
178 });
179
180 // Open editor
181 let status = std::process::Command::new(&editor)
182 .arg(&temp_file)
183 .status()
184 .map_err(|e| {
185 CliError::Internal(anyhow::anyhow!("Failed to open editor '{}': {}", editor, e))
186 })?;
187
188 if !status.success() {
189 return Err(CliError::Internal(anyhow::anyhow!(
190 "Editor exited with non-zero status: {:?}",
191 status.code()
192 )));
193 }
194
195 // Read the file back
196 let content = std::fs::read_to_string(&temp_file)
197 .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to read temp file: {}", e)))?;
198
199 // Clean up
200 let _ = std::fs::remove_file(&temp_file);
201
202 // Process content - remove comment lines and trim
203 let message: String = content
204 .lines()
205 .filter(|line| !line.starts_with('#'))
206 .collect::<Vec<_>>()
207 .join("\n")
208 .trim()
209 .to_string();
210

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