Open an editor to get the commit message.
(&self)
| 205 | |
| 206 | /// Open an editor to get the commit message. |
| 207 | pub(super) fn get_message_from_editor(&self) -> CliResult<String> { |
| 208 | // Create a temporary file for the message |
| 209 | let temp_dir = std::env::temp_dir(); |
| 210 | let temp_file = temp_dir.join("ATOMIC_EDITMSG"); |
| 211 | |
| 212 | // Write template to file |
| 213 | let template = r#" |
| 214 | # Enter your commit message above. |
| 215 | # Lines starting with '#' will be ignored. |
| 216 | # An empty message aborts the commit. |
| 217 | "#; |
| 218 | |
| 219 | std::fs::write(&temp_file, template).map_err(|e| { |
| 220 | CliError::Internal(anyhow::anyhow!("Failed to create temp file: {}", e)) |
| 221 | })?; |
| 222 | |
| 223 | // Get editor from environment |
| 224 | let editor = std::env::var("EDITOR") |
| 225 | .or_else(|_| std::env::var("VISUAL")) |
| 226 | .unwrap_or_else(|_| { |
| 227 | if cfg!(windows) { |
| 228 | "notepad".to_string() |
| 229 | } else { |
| 230 | "vi".to_string() |
| 231 | } |
| 232 | }); |
| 233 | |
| 234 | // Open editor |
| 235 | let status = std::process::Command::new(&editor) |
| 236 | .arg(&temp_file) |
| 237 | .status() |
| 238 | .map_err(|e| { |
| 239 | CliError::Internal(anyhow::anyhow!("Failed to open editor '{}': {}", editor, e)) |
| 240 | })?; |
| 241 | |
| 242 | if !status.success() { |
| 243 | return Err(CliError::Internal(anyhow::anyhow!( |
| 244 | "Editor exited with non-zero status: {:?}", |
| 245 | status.code() |
| 246 | ))); |
| 247 | } |
| 248 | |
| 249 | // Read the file back |
| 250 | let content = std::fs::read_to_string(&temp_file) |
| 251 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to read temp file: {}", e)))?; |
| 252 | |
| 253 | // Clean up |
| 254 | let _ = std::fs::remove_file(&temp_file); |
| 255 | |
| 256 | // Process content - remove comment lines and trim |
| 257 | let message: String = content |
| 258 | .lines() |
| 259 | .filter(|line| !line.starts_with('#')) |
| 260 | .collect::<Vec<_>>() |
| 261 | .join("\n") |
| 262 | .trim() |
| 263 | .to_string(); |
| 264 |