Launch the user's preferred editor with the given file path
(file_path: &std::path::Path)
| 86 | |
| 87 | /// Launch the user's preferred editor with the given file path |
| 88 | fn launch_editor(file_path: &std::path::Path) -> Result<(), ChatError> { |
| 89 | // Get the editor from environment variable or use a default |
| 90 | let editor_cmd = get_editor(); |
| 91 | |
| 92 | // Parse the editor command to handle arguments |
| 93 | let mut parts = |
| 94 | shlex::split(&editor_cmd).ok_or_else(|| ChatError::Custom("Failed to parse EDITOR command".into()))?; |
| 95 | |
| 96 | if parts.is_empty() { |
| 97 | return Err(ChatError::Custom("EDITOR environment variable is empty".into())); |
| 98 | } |
| 99 | |
| 100 | let editor_bin = parts.remove(0); |
| 101 | |
| 102 | // Open the editor with the parsed command and arguments |
| 103 | let mut cmd = std::process::Command::new(editor_bin); |
| 104 | // Add any arguments that were part of the EDITOR variable |
| 105 | for arg in parts { |
| 106 | cmd.arg(arg); |
| 107 | } |
| 108 | // Add the file path as the last argument |
| 109 | let status = cmd |
| 110 | .arg(file_path) |
| 111 | .status() |
| 112 | .map_err(|e| ChatError::Custom(format!("Failed to open editor: {}", e).into()))?; |
| 113 | |
| 114 | if !status.success() { |
| 115 | return Err(ChatError::Custom("Editor exited with non-zero status".into())); |
| 116 | } |
| 117 | |
| 118 | Ok(()) |
| 119 | } |
| 120 | |
| 121 | /// Opens the user's preferred editor to edit an existing file |
| 122 | pub fn open_editor_file(file_path: &std::path::Path) -> Result<(), ChatError> { |
no test coverage detected