-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy patheditor.rs
More file actions
39 lines (31 loc) · 1.17 KB
/
editor.rs
File metadata and controls
39 lines (31 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::path::Path;
use std::process::Command;
/// Launch the user's preferred editor with the given file path.
///
/// This function properly parses the EDITOR environment variable to handle
/// editors that require arguments (e.g., "emacsclient -nw").
///
/// # Arguments
/// * `file_path` - Path to the file to open in the editor
///
/// # Returns
/// * `Ok(())` if the editor was launched successfully and exited with success
/// * `Err` if the editor failed to launch or exited with an error
pub fn launch_editor(file_path: &Path) -> eyre::Result<()> {
let editor_cmd = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
// Parse the editor command to handle arguments
let mut parts = shlex::split(&editor_cmd).ok_or_else(|| eyre::eyre!("Failed to parse EDITOR command"))?;
if parts.is_empty() {
eyre::bail!("EDITOR environment variable is empty");
}
let editor_bin = parts.remove(0);
let mut cmd = Command::new(editor_bin);
for arg in parts {
cmd.arg(arg);
}
let status = cmd.arg(file_path).status()?;
if !status.success() {
eyre::bail!("Editor process did not exit with success");
}
Ok(())
}