open_editor/
lib.rs

1use std::env::temp_dir;
2
3use crate::errors::OpenEditorError;
4
5mod editor;
6pub mod editor_call_builder;
7mod editor_kind;
8pub mod errors;
9
10pub use editor_call_builder::EditorCallBuilder;
11
12static ENV_VARS: &[&str] = &["VISUAL", "EDITOR"];
13
14/// Open the default editor and allows editing of a string.
15///
16/// The default editor is determined by the `VISUAL` and `EDITOR` environment
17/// variables, in that order.
18///
19/// # Errors
20/// This function will return an error if the editor call fails, if the file cannot be read, or if the temporary file cleanup fails.
21pub fn edit_in_editor(string: &str) -> Result<String, OpenEditorError> {
22    edit_in_editor_with_env_vars(string, ENV_VARS)
23}
24
25/// Similar to [`edit_in_editor`], but allows specifying the environment
26/// variables to use to find the editor.
27pub fn edit_in_editor_with_env_vars(
28    string: &str,
29    env_vars: &[&str],
30) -> Result<String, OpenEditorError> {
31    let mut filename = temp_dir();
32    filename.push(String::from("open_editor_tmp_file"));
33
34    // Write the initial content to the temporary file
35    std::fs::write(&filename, string).map_err(OpenEditorError::FileManipulationFail)?;
36
37    EditorCallBuilder::new_with_env_vars(filename.clone(), env_vars)?.call_editor()?;
38    let result =
39        std::fs::read_to_string(&filename).map_err(OpenEditorError::FileManipulationFail)?;
40
41    // Clean up the temporary file after reading
42    std::fs::remove_file(&filename).map_err(|_| {
43        OpenEditorError::TempFileCleanupFail(filename.to_string_lossy().into_owned())
44    })?;
45
46    Ok(result)
47}
48
49/// Open the default editor and allows editing of a mutable string.
50///
51/// The default editor is determined by the `VISUAL` and `EDITOR` environment
52/// variables, in that order.
53///
54/// # Errors
55///
56/// This function will return an error if the editor call fails, if the file cannot be read, or if the temporary file cleanup fails.
57pub fn edit_mut_in_editor(string: &mut String) -> Result<(), OpenEditorError> {
58    edit_mut_in_editor_with_env_vars(string, ENV_VARS)
59}
60
61/// Similar to [`edit_mut_in_editor`], but allows specifying the environment
62/// variables to use to find the editor.
63pub fn edit_mut_in_editor_with_env_vars(
64    string: &mut String,
65    env_vars: &[&str],
66) -> Result<(), OpenEditorError> {
67    *string = edit_in_editor_with_env_vars(string, env_vars)?;
68    Ok(())
69}
70
71/// Open the default editor and returns what was written in it.
72///
73/// The default editor is determined by the `VISUAL` and `EDITOR` environment
74/// variables, in that order.
75///
76/// # Errors
77/// If the editor call fails, or if the file cannot be read, an error will be returned.
78pub fn open_editor() -> Result<String, OpenEditorError> {
79    open_editor_with_env_vars(ENV_VARS)
80}
81
82/// Similar to [`open_editor`], but allows specifying the environment variables
83/// to use to find the editor.
84pub fn open_editor_with_env_vars(env_vars: &[&str]) -> Result<String, OpenEditorError> {
85    edit_in_editor_with_env_vars("", env_vars)
86}