EditorCallBuilder

Struct EditorCallBuilder 

Source
pub struct EditorCallBuilder { /* private fields */ }

Implementations§

Source§

impl EditorCallBuilder

Source

pub fn new() -> Self

Creates a new EditorCallBuilder. You can optionally set the line and column numbers later using the at_line and at_column methods.

Finally, you can call the editor with the open_editor, edit_string, or edit_string_mut methods.

§Errors

This function will return an error if the default editor cannot be found in the environment variables.

Examples found in repository?
examples/get_user_input.rs (line 4)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new().open_editor()?;
5    println!("User input:\n{user_input}");
6    Ok(())
7}
More examples
Hide additional examples
examples/custom_env_vars.rs (line 4)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new()
5        .with_env_vars(&["MY_EDITOR"])
6        .open_editor()?;
7    println!("User input:\n{user_input}");
8    Ok(())
9}
examples/custom_editor.rs (line 4)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new()
5        .with_editor(Editor::from_editor_kind(EditorKind::Nano))
6        .open_editor()?;
7    println!("User input:\n{user_input}");
8    Ok(())
9}
examples/interactive_edit.rs (line 11)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let (file_path, line, column) = get_parameters()?;
10
11    EditorCallBuilder::new()
12        .at_line(line)
13        .at_column(column)
14        .open_file(&file_path)?;
15    Ok(())
16}
examples/independant_editor.rs (line 11)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let filename = PathBuf::from_str("./test")?;
9
10    // Spawn editor without waiting
11    EditorCallBuilder::new()
12        .wait_for_editor(false)
13        .open_file(&filename)?;
14
15    println!("Editor launched. Press Ctrl+C to stop.\n");
16
17    loop {
18        let contents = fs::read_to_string(&filename).unwrap_or_default();
19
20        println!("--- Content of {} ---\n{}\n", filename.display(), contents);
21
22        sleep(Duration::from_secs(1));
23    }
24}
Source

pub fn at_line(self, line: usize) -> Self

Sets the line number for the editor to open at.

Examples found in repository?
examples/interactive_edit.rs (line 12)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let (file_path, line, column) = get_parameters()?;
10
11    EditorCallBuilder::new()
12        .at_line(line)
13        .at_column(column)
14        .open_file(&file_path)?;
15    Ok(())
16}
Source

pub fn at_column(self, line: usize) -> Self

Sets the column number for the editor to open at.

Examples found in repository?
examples/interactive_edit.rs (line 13)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let (file_path, line, column) = get_parameters()?;
10
11    EditorCallBuilder::new()
12        .at_line(line)
13        .at_column(column)
14        .open_file(&file_path)?;
15    Ok(())
16}
Source

pub fn wait_for_editor(self, value: bool) -> Self

Whether to wait for the editor to close before returning.

Examples found in repository?
examples/independant_editor.rs (line 12)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let filename = PathBuf::from_str("./test")?;
9
10    // Spawn editor without waiting
11    EditorCallBuilder::new()
12        .wait_for_editor(false)
13        .open_file(&filename)?;
14
15    println!("Editor launched. Press Ctrl+C to stop.\n");
16
17    loop {
18        let contents = fs::read_to_string(&filename).unwrap_or_default();
19
20        println!("--- Content of {} ---\n{}\n", filename.display(), contents);
21
22        sleep(Duration::from_secs(1));
23    }
24}
Source

pub fn with_env_vars(self, env_vars: &[&str]) -> Self

Add additional environment variables to look for the editor in. These variables will have higher priority than VISUAL and EDITOR.

Examples found in repository?
examples/custom_env_vars.rs (line 5)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new()
5        .with_env_vars(&["MY_EDITOR"])
6        .open_editor()?;
7    println!("User input:\n{user_input}");
8    Ok(())
9}
Source

pub fn with_editor(self, editor: Editor) -> Self

Sets a specific editor to use instead of the default one.

Examples found in repository?
examples/custom_editor.rs (line 5)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new()
5        .with_editor(Editor::from_editor_kind(EditorKind::Nano))
6        .open_editor()?;
7    println!("User input:\n{user_input}");
8    Ok(())
9}
Source

pub fn open_editor(&self) -> Result<String, OpenEditorError>

Open the default editor and returns what was written in it.

§Errors

If the editor call fails, or if the temporary file cannot be read or cleaned up, or if the editor call fails.

Examples found in repository?
examples/get_user_input.rs (line 4)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new().open_editor()?;
5    println!("User input:\n{user_input}");
6    Ok(())
7}
More examples
Hide additional examples
examples/custom_env_vars.rs (line 6)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new()
5        .with_env_vars(&["MY_EDITOR"])
6        .open_editor()?;
7    println!("User input:\n{user_input}");
8    Ok(())
9}
examples/custom_editor.rs (line 6)
3fn main() -> Result<(), OpenEditorError> {
4    let user_input = EditorCallBuilder::new()
5        .with_editor(Editor::from_editor_kind(EditorKind::Nano))
6        .open_editor()?;
7    println!("User input:\n{user_input}");
8    Ok(())
9}
Source

pub fn edit_string_mut( &self, string: &mut String, ) -> Result<(), OpenEditorError>

Open the default editor and allows editing of a mutable string.

§Errors

If the editor call fails, or if the temporary file cannot be read or cleaned up, or if the editor call fails.

Source

pub fn edit_string(&self, string: &str) -> Result<String, OpenEditorError>

Open the default editor and allows editing of a string which is then returned.

§Errors

If the editor call fails, or if the temporary file cannot be read or cleaned up, or if the editor call fails.

Source

pub fn open_file(&self, file_path: &Path) -> Result<(), OpenEditorError>

Opens the specified file in the editor.

§Errors

This function will return an error if the editor call fails or if the file cannot be read.

Examples found in repository?
examples/interactive_edit.rs (line 14)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let (file_path, line, column) = get_parameters()?;
10
11    EditorCallBuilder::new()
12        .at_line(line)
13        .at_column(column)
14        .open_file(&file_path)?;
15    Ok(())
16}
More examples
Hide additional examples
examples/independant_editor.rs (line 13)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let filename = PathBuf::from_str("./test")?;
9
10    // Spawn editor without waiting
11    EditorCallBuilder::new()
12        .wait_for_editor(false)
13        .open_file(&filename)?;
14
15    println!("Editor launched. Press Ctrl+C to stop.\n");
16
17    loop {
18        let contents = fs::read_to_string(&filename).unwrap_or_default();
19
20        println!("--- Content of {} ---\n{}\n", filename.display(), contents);
21
22        sleep(Duration::from_secs(1));
23    }
24}

Trait Implementations§

Source§

impl Default for EditorCallBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.